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代码:
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; }
相关文章推荐
- WampServer环境搭建
- 连接linux上服务失败和打开防火墙对端口限制
- Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
- nginx静态代理配置
- Linux开发工具(gcc,gdb,make,shell)——三级优化分别做了什么(一)
- Linux下同步工具inotify+rsync使用详解
- MYSQL 测试性能测试网站-----dimitrik.free.fr
- <个人博客网站开通啦!!!>
- 整合Open vSwitch与DNSmasq为虚拟机提供DHCP功能
- windows--nginx关闭进程
- 初识gcc编译过程
- Linux下Web网站压力测试工具Webbench
- Apache Spark(转)
- 手机开热点,接收热点的有些网站无法连接的问题
- 在windows下运行Hadoop程序的环境配置
- linux中替换所有文件名称和文件内容特定字符命令
- 运维人员写项目方案及推进项目的基本流程思路
- Docker 学习记录(一)-docker安装使用
- hadoop实战之分布式模式
- Linux中.c文件到可执行文件的坎坷历程