uva106 - Fermat vs. Pythagoras 互素勾股数
来源:互联网 发布:java远程调用dll 编辑:程序博客网 时间:2024/06/11 11:52
Fermat vs. Pythagoras
Background
Computer generated and assisted proofs and verification occupy a small niche in the realm of Computer Science. The first proof of the four-color problem was completed with the assistance of a computer program and current efforts in verification have succeeded in verifying the translation of high-level code down to the chip level.
This problem deals with computing quantities relating to part of Fermat's Last Theorem: that there are no integer solutions of forn > 2.
The Problem
Given a positive integer N, you are to write a program that computes two quantities regarding the solution of
where x, y, and z are constrained to be positive integers less than or equal toN. You are to compute the number of triples (x,y,z) such thatx<y<z, and they are relatively prime, i.e., have no common divisor larger than 1. You are also to compute the number of values such thatp is not part of any triple (not just relatively prime triples).
The Input
The input consists of a sequence of positive integers, one per line. Each integer in the input file will be less than or equal to 1,000,000. Input is terminated by end-of-file.
The Output
For each integer N in the input file print two integers separated by a space. The first integer is the number of relatively prime triples (such that each component of the triple is ). The second number is the number of positive integers that are not part of any triple whose components are all . There should be one output line for each input line.
Sample Input
1025100
Sample Output
1 44 916 27
给一个N,求出满足x^2+y^2=z^2,x<y<z<=N,且x,y,z互素这样的x,y,z有多少对,还要求1到N有多少数没有出现在x^2+y^2=z^2,x<y<z<=N,x,y,z不一定互素。
因为x,y,z互素,所以x,y不可能都是偶数。
假设x,y都是奇数,设x=2*a+1,y=2*b+1,x^2是奇数,y^2是奇数,则z^2是偶数,z是偶数。因为偶数的平方一定能被4整除,但是z^2=x^2+y^2=4(a² + b² + a + b) + 2 不能被4整除,所以x,y不可能都是奇数。
于是x,y只能是一奇一偶,z只能是奇数,设x为奇数,y为偶数。则z+x和z-x都是偶数。设z+x=2*u,z-x=2*v。
z=u+v
x=u-v
因为z和x互素,所以u和v也一定互素(假设u和v不互素,u=w*k1,v=w*k2,z=w*(k1+k2),x=w*(k1-k2),z和x不互素,矛盾).。
把z和x代入x^2+y^2=z^2得,y^2=4*u*v,也就是(y/2)^2=u*v。由于u和v互素,乘积又是一个平方数,所以u和v都是一个平方数(因为(u,v)=1,u=u*(u,v)=(u^2,uv)=(u^2,(y/2)^2)=(u,y/2)^2,u是平方数,同理v也是平方数)。
设u=a^2,v=b^2,a,b互素并且是一奇一偶(若a,b不互素,则u,v不互素,矛盾。由于u,v互素,则至少其中一个是奇数,又因为y是偶数,所以u*v是偶数,所以u,v一奇一偶,奇数的平方是奇数,偶数的平方是偶数,所以a,b也一奇一偶)。
最后就有 z=a^2+b^2,x=a^2-b^2,y=2*a*b (a,b互素,一奇一偶,a>b)。
第二问只要把第一问求出的三元组的倍数都标记就行了,因为勾股定理的三个数要么两两互素,要么两两都不互素,所以不互素的都是互素的倍数得来的。
#include<cstring>#include<cstdio>#include<iostream>#include<cmath>#include<algorithm>#include<queue>#define INF 0x3f3f3f3fusing namespace std;int vis[1000010];int gcd(int x,int y){ return x%y?gcd(y,x%y):y;}int main(){ // freopen("in.txt","r",stdin); int N; while(scanf("%d",&N)!=EOF){ memset(vis,0,sizeof(vis)); int L=(floor)(sqrt(N)); int i,j,k,x,y,z,ans1=0,ans2=0; for(i=1;i<=L;i++) for(j=i+1;j<=L;j+=2){ if((z=i*i+j*j)<=N&&gcd(i,j)==1){ ans1++; x=j*j-i*i; y=2*i*j; for(k=1;k*z<=N;k++) vis[x*k]=vis[y*k]=vis[z*k]=1; } } for(i=1;i<=N;i++) if(!vis[i]) ans2++; printf("%d %d\n",ans1,ans2); } return 0;}
- uva106 - Fermat vs. Pythagoras 互素勾股数
- uva106 - Fermat vs. Pythagoras()
- UVA106- Fermat vs. Pythagoras(素勾股数)
- UVA106 - Fermat vs. Pythagoras(素勾股数)
- POJ1305 UVA106 Fermat vs. Pythagoras 毕达哥斯拉三元组 特殊的不定方程
- 106 - Fermat vs. Pythagoras
- 106 - Fermat vs. Pythagoras
- UVaOJ106 - Fermat vs. Pythagoras
- Fermat vs. Pythagoras
- POJ.Fermat vs. Pythagoras
- uva 106 - Fermat vs. Pythagoras
- poj1305 Fermat vs. Pythagoras----勾股数
- UVA 106 - Fermat vs. Pythagoras
- UVA 106 Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras
- UVa 106 - Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras
- UVa 106 - Fermat vs. Pythagoras
- IOS 开发之设置UIButton的title
- java 从零开始,学习笔记之基础入门<SQL_Server>(二十一)
- Canvas笔画向量交互动画效果,随着鼠标描绘轨迹
- 熬之滴水成石:最想深入了解的内容--windows内核机制(8)
- 查询集合记录作一张表使用
- uva106 - Fermat vs. Pythagoras 互素勾股数
- Hadoop Streaming命令
- tintxml 操作封装,包括CDATA使用
- linux -- aptitude
- Java基础 之软引用、弱引用、虚引用
- CListCtrl使用技巧
- 九度题目:字符串排序(1135)
- poj1080
- 如何获得iframe 里面的对象和操作iframe里面的对象