HDU-1299 Diophantus of Alexandria(分数划分 :因子数)
Problem Description
Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first mathematicians to study equations where variables were restricted to integral values. In honor of him, these equations are commonly called diophantine equations. One of the most famous diophantine equation is x^n + y^n = z^n. Fermat suggested that for n > 2, there are no solutions with positive integral values for x, y and z. A proof of this theorem (called Fermat’s last theorem) was found only recently by Andrew Wiles.
Consider the following diophantine equation:
1 / x + 1 / y = 1 / n where x, y, n ∈ N+ (1)
Diophantus is interested in the following question: for a given n, how many distinct solutions (i. e., solutions satisfying x ≤ y) does equation (1) have? For example, for n = 4, there are exactly three distinct solutions:
1 / 5 + 1 / 20 = 1 / 4
1 / 6 + 1 / 12 = 1 / 4
1 / 8 + 1 / 8 = 1 / 4
Clearly, enumerating these solutions can become tedious for bigger values of n. Can you help Diophantus compute the number of distinct solutions for big values of n quickly?
Input
The first line contains the number of scenarios. Each scenario consists of one line containing a single number n (1 ≤ n ≤ 10^9).
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Next, print a single line with the number of distinct solutions of equation (1) for the given value of n. Terminate each scenario with a blank line.
Sample Input
2
4
1260
Sample Output
Scenario #1:
3
Scenario #2:
113
题意:对于每个n,求满足1/x + 1/y = 1/n公式的个数(x 和 y)
模拟超时代码:
/*
本来想只有满足这个1/x + 1/y = 1/n
设x=n+1 化简y = n*x /(x-n)
遍历x就好了结果超时了
*/
long long num = 0;
for(LL x=n+1;x<=2*n;x++){//超时
// y = n*x /(x-n);
if((n*x)%(x-n)==0 && (n*x)/(x-n) >= x) {
num++;
}
}
数据量较大需要不遍历x根据n来求结果,因此开始来简化代码:
x = n+i (i是从1到n遍历)
y = n*x /(x-n) = (n+i)*n /i = n^2 / i + n ( i是从1到n遍历)
即 对于每个i, n^2只要能够被i整除就算入进去,此时我们需要知道y的个数也就是n^2的因子数
据公式:任意一个数 n=p1^e1*p2^e2*p3^e3…pr^er p1….(pi是小于n的素数)
因子数就是num=(1+e1)(1+e2)(1+e3)….(1+er)
于是n*n的因子数就是num2=(1+2*e1)(1+2*e2)…*(1+2*er)
AC代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long LL;
const int M = 501000;
bool visit[M];
int prime[M];
int cnt, t;
LL n;
void fun(){
memset(prime, 0, sizeof(prime));
memset(visit, true, sizeof(visit));
cnt = 0;
for (int i = 2; i <= M; ++i){
if (visit[i] == true){
prime[++cnt] = i;
}
for (int j = 1; ((j <= cnt) && (i * prime[j] <= M)); j++){
visit[i * prime[j]] = false;
if (i % prime[j] == 0) break; //点睛之笔
}
}
}
LL cou(LL m){
LL num=1;
for(LL j=1; j<=cnt && prime[j]<=m; j++){
int c = 0;
while (m%prime[j]==0){
c++;
m /= prime[j];
}
num*=(c*2+1); //n*n的因子数
}
if(m>1) num*=3; //余下的 n可能为 素因子 此时c为1,c*2+1 = 3
return num;
}
int main(){
fun();
scanf("%d",&t);
for(int j=1;j<=t;j++){
scanf("%lld",&n);
LL num = cou(n);
printf("Scenario #%d:\n",j);
printf("%d\n\n",++num/2); //一对是相同的
}
return 0;
}