数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras
Posted 既然选择了远方,便只顾风雨兼程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras相关的知识,希望对你有一定的参考价值。
Fermat vs. Pythagoras
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 1493 | Accepted: 865 |
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).
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
10 25 100
Sample Output
1 4 4 9 16 27
题意:给定一个n,输出三元组(a,b,c)其中a,b,c两两互质,且a²+b²=c²,以及1~n中没有在任何一个三元组中出现过的数的个数。
这道题需要知道勾股数的性质。
首先,对于一组勾股数,①a与b的奇偶性不同,②c一定为奇数。
证明①:若a与b同为偶数,则c也为偶数,与a,b,c两两互质矛盾;若a与b同为奇数,c一定为偶数,设a=2*i+1,b=2*j+1,c=2*k -> a²+b²=c²->2*i²+2*i+2*j²+2*j+1=2*k²,这个式子是矛盾的。
证明②:a,b一奇一偶,显然。
然后将 a²+b²=c² 变形,a²=(c+b)*(c-b),容易发现c-b与c+b互质,所以c-b与c+b都是平方数,设x²=c-b,y²=c+b,得a=x*y,b=(y²-x²)/2,c=(y²+x²)/2.
易得x,y都为奇数,直接枚举x,y就好了。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 using namespace std; 6 const int maxn=1000010; 7 bool vis[maxn]; 8 long long Gcd(long long a,long long b){ 9 return b?Gcd(b,a%b):a; 10 } 11 int main(){ 12 int n,m,ans,tot; 13 while(~scanf("%d",&n)){ 14 m=(int)sqrt(n+0.5);ans=tot=0; 15 memset(vis,0,sizeof(vis)); 16 for(int t=1;t<=m;t+=2) 17 for(int s=t+2;(s*s+t*t)/2<=n;s+=2) 18 if(Gcd(s,t)==1){ 19 int a=s*t; 20 int b=(s*s-t*t)/2; 21 int c=(s*s+t*t)/2 22 ans++; 23 for(int k=1;k*c<=n;k++){ 24 vis[k*a]=true; 25 vis[k*b]=true; 26 vis[k*c]=true; 27 } 28 } 29 for(int i=1;i<=n;i++) 30 if(!vis[i]) 31 tot++; 32 printf("%d %d\n",ans,tot); 33 } 34 }
以上是关于数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras的主要内容,如果未能解决你的问题,请参考以下文章
POJ 1426 Find The Multiple(数论——中国同余定理)
poj 1006Biorhythms(数论--中国剩余定理 模版题){附中国剩余定理 }