HDU 1299 Diophantus of Alexandria

题目大意:

给出一个N,求出1/X+1/Y=1/N有多少种。


解题思路:

我们可以清楚的知道Y>N ,设Y=N+K,将题里等式化简可得,X=(N^2/K)+N,又可知X一定为正数,题即转化为求N^2有多少个素因子。

又因为N很大,我们可以先用筛法求出素数表,然后分解素因子。


#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
int j,tot,cnt;
int pri[1000000],fac[1000];
bool f[10000000];

void prime(){
    cnt=0;
    memset(f,0,sizeof(f));
    for(int i=2;i<10000000;i++){
        if(!f[i]){
            pri[cnt++]=i;
            for(int j=i+i;j<10000000;j+=i) f[j]=1;
        }
    }
}
LL get(LL x){
	tot=0;
	memset(fac,0,sizeof(fac));
	for (int i=0;i<cnt && pri[i]*pri[i]<=x;i++){
		if (x%pri[i]==0) {
			while (x%pri[i]==0) fac[tot]++, x/=pri[i];
			tot++;
		}
	}
	//for (int i=0;i<=tot;i++) printf("%d ",pri[i]);
	//for (int i=0;i<=tot;i++) printf("%d ",fac[i]);
	if (x>1) fac[tot++]=1;
	LL ans=1;
	for (int i=0;i<tot;i++) ans=ans*(2*fac[i]+1);
	return ans;
}
int main(){
	int W,cas=1;
	LL n;
	prime();
	scanf("%d",&W);
	while (W--) {
		scanf("%lld",&n);
        printf("Scenario #%d:\n%I64d\n\n",cas++,(get(n)+1)/2);
	}
}


你可能感兴趣的:(HDU 1299 Diophantus of Alexandria)