您的位置:首页 > 运维架构

Diophantus of Alexandria

2016-07-06 19:47 316 查看
Diophantus of Alexandria

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2081 Accepted Submission(s): 788

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=n+k,所以可以解得y=(n*n/k)+n,所以只要k是n*n的因子,就满足题意。

有数学理论我们知道任意一个数可以由多个素数的次方积来表示。假设n=a^x*b^y*c^z,所以n的因子的个数为(1+x)(1+y)(1+z),为什么呢?因为x可以取到0~x,(y和z同理)。又因为我们要求的是n*n的因子数,n*n=a^2x*b^2y*c^2z,所以n*n的因子数为(1+2x)(1+2y)(1+2z),而n也是其因子,并且求解过程是求所以k的可能,而题目要求x<=y,所以结果要除以2。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 40000;
int p[maxn];

void table_p()
{
memset(p,0,sizeof(p));
for(int i=2;i<=maxn;i++)
{
if(!p[i])
{
for(int j=i*i;i<=maxn;j+=i)
p[j]=1;
}
}
}
int main()
{
int t,e=1,n;
scanf("%d",&t);
while(t--)
{
printf("Scenario #%d:\n",e);
scanf("%d",&n);
int sum=1,k;
for(int i=2;i<=maxn;i++)
{
if(n==1)
break;
if(!p[i])
{
k=0;
while(n%i==0)
{
k++;
n/=i;
}
sum*=2*k+1;
}
}
if(n>1)
sum*=3;
printf("%d\n\n",(sum+1)/2);
e++;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: