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
求解满足 1 / x + 1 / y = 1 / n 的(x,y)的对数
令 y = n + k (k为任意整数),带入1 / x + 1 / y = 1 / n
那么 x = n2 / k + n,所以x的取值与 k 有关
有质因子分解我们知道,n = p1r1 × p2r2 × p3r3 × …… × pnrn(其中p为素数),那么 n 的素因子的个数 ans = (r1 + 1)× (r2 + 1)× …… × (rn + 1)
n2 = p12×r1 × p22×r2 × p32×r3 × …… × pn2×rn。那么 n2 的素因子的个数 ans = (2×r1 + 1)× (2×r2 + 1)× …… × (2×rn + 1)
因为(x,y)是成对存在,所以最终的答案:
if(ans&1) ans = ans/2+1;
else ans = ans/2;
也可以直接看成 ans = (ans + 1) / 2;
1、线性筛法(O(N)) 筛素数
int v[maxn],pri[maxn];
void primes(int n){
memset(v,0,sizeof(v)); //最小质因子
m = 0; //质数数量
for(int i = 2;i<=n;i++){
if(v[i]==0){
v[i] = i;
pri[++m] = i;
} //i是质数
for(int j = 1;j<=m;j++){
//i有比pri[j]更小的质因子,或者超出n的范围,停止循环
if(pri[j]>v[i]||pri[j]>n/i) break;
v[i*pri[j]] = pri[j];
}
}
for(int i = 1;i<=m;i++) cout<<pri[i]<<endl;
}
2、质因分解
for(int j=0;j<m;j++){
res = 0;
if(n==1) break;
fp = (int)sqrt(n*1.0) + 1;
if(pri[j]>fp) break;
while(n%pri[j]==0){
n /= pri[j];
res++;
}
ans *= (2*res+1);
}
if(n>1) ans *= (2*1 + 1);
AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
const int maxn = 250000;
using namespace std;
ll v[maxn],pri[maxn];
int m,fp;
void primes(){
memset(v,0,sizeof(v));
m = 0;
for(int i=2;i<=maxn;i++){
if(v[i]==0){
v[i] = i;
pri[++m] = i;
}
for(int j=1;j<=m;j++){
if(pri[j]>v[i]||pri[j]>maxn/i) break;
v[i*pri[j]] = pri[j];
}
}
}
int main(void)
{
int t,n,ans;
primes();
scanf("%d",&t);
for(int i=1;i<=t;i++){
ll ans = 1,res;
scanf("%d",&n);
for(int j=0;j<m;j++){
res = 0;
if(n==1) break;
fp = (int)sqrt(n*1.0) + 1;
if(pri[j]>fp) break;
while(n%pri[j]==0){
n /= pri[j];
res++;
}
ans *= (2*res+1);
}
if(n>1) ans *= 3;
printf("Scenario #%d:\n",i);
printf("%lld\n\n",(ans+1)/2);
}
return 0;
}