Poj.1305 Fermat vs. Pythagoras【毕达哥拉斯三角形】 2015/04/21

来源:互联网 发布:尖锐湿疹多少钱知乎 编辑:程序博客网 时间:2024/06/05 07:51
Fermat vs. Pythagoras
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 1388 Accepted: 803

Description

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 a^n + b^n = c^n for n > 2. 
Given a positive integer N, you are to write a program that computes two quantities regarding the solution of x^2 + y^2 = z^2, where x, y, and z are constrained to be positive integers less than or equal to N. You are to compute the number of triples (x,y,z) such that x < 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 0 < p <= N such that p is not part of any triple (not just relatively prime triples). 

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

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 <=N). The second number is the number of positive integers <=N that are not part of any triple whose components are all <=N. There should be one output line for each input line.

Sample Input

1025100

Sample Output

1 44 916 27

Source

Duke Internet Programming Contest 1991,UVA 106
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;#define M 1000010bool flag[M];int gcd(int a,int b){return b?gcd(b,a%b):a;}int yi(int a,int b){if( (a&1) && !(b&1) )return 1;if( !(a&1) && (b&1) )return 1;return 0;}int main(){int n,i,j,k;while(~scanf("%d",&n)){memset(flag,false,sizeof(flag));int temp = (int)sqrt(n+0.0);int ans = 0;int ret = 0;for( i = 1 ; i <= temp ; ++i )for( j = i+1 ; i*i+j*j <= n ; ++j ){if( gcd(i,j) == 1 && yi(i,j) ){ans++;int x = 2*i*j;int y = j*j-i*i;int z = j*j+i*i;for( k = 1 ; k*z <= n ; ++k ){flag[k*x] = true;flag[k*y] = true;flag[k*z] = true;}}}for( i = 1 ; i <= n ; ++i )if( !flag[i] )ret++;printf("%d %d\n",ans,ret);}return 0;}
注:毕达哥拉斯三角形定理。x^2+y^2=z^2  即 (2*m*n)^2+(m*m-n*n)^2=(m*m+n*n)^2 (定理1) 即x=2*m*n  y=m*m-n*n  z=m*m+n*n    m,n互质 且 m与n只有一个为奇数
0 0