(P1445) [Violet] Cherry Blossom (Prime Factorization)

Topic link: [Violet] Cherry Blossoms - Luogu

Analysis: Multiply both sides of the original equation by x*y*(n!) to get n!(x+y)=x*y, add (n!)^2 to both sides and subtract n!(x+y) to get (n!)^2=(n!)^2-n!(x+y)+x*y, carefully observe the right side of the equation and find that it is equal to (n!-x)*(n!-y), then The equation is converted to

(n!-x)*(n!-y)=(n!)^2
The problem is to find a positive integer solution. If x<=n! Since x and y are positive numbers, then there is n!>( n!-x)>=0, then n!>(n!-y)>=0, obviously there is no solution, the product of two numbers less than n! cannot be greater than (n!)^2, so there is x and y are both greater than n!

That is (xn!)*(yn!)=(n!)^2

With this equation, we can find that (xn!) and (yn!) are two factors of (n!)^2 respectively, then finding the integer solution of x and y becomes finding (n!)^2 The number of factors, friends who don't know how to find the number of factors can look here: The number of factors and the sum of factors

We can directly find the prime factor and its power contained in each number from 2 to n. Finally, we only need to multiply the power by 2 to get the prime factor and its power contained in n^2.

Here is the code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1e6+10,mod=1e9+7;
int prime[N],cnt,ans[N];
bool vis[N];
void init()
{
	for(int i=2;i<N;i++)
	{
		if(!vis[i]) prime[++cnt]=i;
		for(int j=1;j<=cnt&&i*prime[j]<N;j++)
		{
			vis[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
}
int main()
{
	int n;
	init();
	cin>>n;
//	for(int i=1;i<=cnt;i++) printf("%d\n",prime[i]);
	for(int i=2;i<=n;i++)
	{
		int t=i;
		for(int j=1;prime[j]*prime[j]<=i;j++)
			while(t%prime[j]==0)
			{
				ans[j]+=1;
				t/=prime[j];
			}
		if(t!=1)
			ans[(lower_bound(prime+1,prime+cnt+1,t)-prime)]++;//找到t对应的素数下标并将答案计入 
	}
	long long sum=1;
	for(int i=1;i<=cnt;i++)
		if(ans[i])
		sum=sum*(ans[i]*2+1)%mod;
	cout<<sum;
	return 0;
}

Guess you like

Origin blog.csdn.net/AC__dream/article/details/123914286