POJ 1305 Fermat vs. Pythagoras (本原勾股数组)
来源:互联网 发布:js做加减乘除 编辑:程序博客网 时间:2024/05/21 11:10
Fermat vs. Pythagoras
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 1467 Accepted: 850
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
1025100
Sample Output
1 4
4 9
16 27
勾股数组就是能形成a^2+b^2=c^2的一组数(a,b,c),根据规律可发现,a,b,c三个数是互素的,对于任意勾股数组
a=s*t;
b=(s*s-t*t)/2;
c=(s*s+t*t)/2
其中s>t且s和t互素
题意:寻找n以内(包含n)的勾股数组的数量还有不是勾股数组的数的数量
思路:根据勾股数组的定义来枚举,为了省时,进行筛选,中间需要判断c就行了,根据定义可知,c是最大的
再者因为枚举过程中s*s+t*t>=2*s*t,那么c的值就是最大的
总结:刚开始想着枚举a,b,c,但是看了100w的数据量,还是算了,想到定义中可以根据s和t来求得a,b,c,那么枚举s和t就行了
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1010000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define mod 1000000007using namespace std;int v[MAXN];ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}int main(){ll t,n,m,i,j,num;int cas=0;while(scanf("%I64d",&n)!=EOF){ll cnt=0;mem(v);for(i=1;i<=n;i++){for(j=i+1;j<=n;j++){if(gcd(i,j)!=1)continue;ll a=i*j;ll b=(j*j-i*i)/2;ll c=(i*i+j*j)/2;//printf("a=%I64d b=%I64d c=%I64d\n",a,b,c);if(a==c||b==c||a==b)continue;if(c>n)break;if(a*a+b*b==c*c)cnt++;elsecontinue;for(ll k=1;k*c<=n;k++)//对于勾股数组的倍数进行筛选{v[a*k]=1;v[b*k]=1;v[c*k]=1;}}}ll ans=0;for(i=1;i<=n;i++)if(!v[i])ans++;printf("%I64d %I64d\n",cnt,ans);}return 0;}
0 0
- POJ 1305 Fermat vs. Pythagoras (本原勾股数组)
- POJ-1305-Fermat vs. Pythagoras-(本原勾股数)
- POJ 1305 Fermat vs. Pythagoras (本原毕达哥拉斯三元组)
- POJ 1305 Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras
- poj 1305 Fermat vs. Pythagoras
- POJ 1305-Fermat vs. Pythagoras(本原的毕达哥拉斯三元组+枚举)
- POJ.Fermat vs. Pythagoras
- poj 1305——Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras(勾股数)
- POJ 1305 Fermat vs. Pythagoras 笔记
- POJ1305 Fermat vs. Pythagoras 本原毕达哥拉斯三元组求解
- POJ-1305(勾股定理)(Fermat vs. Pythagoras)
- UVa 106/POJ 1305 Fermat vs. Pythagoras(数论&勾股数)
- POJ 1305 Fermat vs. Pythagoras 解原毕达哥拉斯三元组
- POJ 1305 Fermat vs. Pythagoras (构造 毕达哥拉斯三元组)
- POJ 1305:Fermat vs. Pythagoras - 勾股定理/毕达哥拉斯三元组
- Poj.1305 Fermat vs. Pythagoras【毕达哥拉斯三角形】 2015/04/21
- Fio 输出内容的解释
- CocoaPods一个Objective-C第三方库的管理利器
- iOS 计步器的实现
- java代码动态设置控件的颜色
- xml,json,http,socket的区别
- POJ 1305 Fermat vs. Pythagoras (本原勾股数组)
- IntelliJ IDEA 15激活
- 查询mysql事务隔离级别
- asp.net生成N组指定位数随机数都一样的解决办法
- IOS动画相关
- JBoss中的HTTP GZIP压缩
- [Python]网络爬虫(三):异常的处理和HTTP状态码的分类
- java 正则(3) matches() / find() / lookingAt / start end / replaceAll / appendReplacement / group(int)
- UI控件监听