hdu 1299 Diophantus of Alexandria

题意:给你一个n (1 <= n <= 10^9) ,问有多少种方案使得 1/a + 1/b = 1/n


首先由这个式子可以很快得到,a = (b*n) / (b-n) ,令b - n = m  则 a = n + (n*n)/k , b = n+k ,问题就转化成求n^2的因子数,最后这个因子数要处理下,因为有重复。求出n的所有素因子个数,n^2的所有素因子个数就是n的两倍,求因子数就是各个素因子的个数加一 相乘 。


#include <stdio.h>
#include <math.h>
#define LL __int64
const int N = sqrt(1000000000+0.5);
bool vis[111111];
int pri[111111];
int main () {
	int i, j;
	vis[1] = 1;
	for(i = 2;i*i <= N; i++) if(!vis[i])
		for(j = i*i;j <= N;j += i)
			vis[j] = 1;
	int num = 0;
	for(i = 2;i <= N; i++)
		pri[num++] = i;
	LL n;
	int t, cas = 1;
	scanf("%d", &t);
	while(t-- ){
		scanf("%I64d", &n); 
		printf("Scenario #%d:\n", cas++);
		LL ans = 1;
		for(i = 0;i < num && pri[i]*pri[i] <= n; i++)  {
			if(n%pri[i] == 0) {
				LL cnt = 0;
				while(n%pri[i] == 0)
					n /= pri[i],cnt++;
				ans = ans *(2*cnt+1);
			}
		}
		if(n != 1)
			ans = ans*3;
		printf("%I64d\n", ans);
		printf("%I64d\n\n", ans/2+1);
	}
	return 0;
}




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