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).
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
- Poj.1305 Fermat vs. Pythagoras【毕达哥拉斯三角形】 2015/04/21
- 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 1035 Fermat vs. Pythagoras(毕达哥拉斯三元组)
- POJ 1305 Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras
- poj 1305 Fermat vs. Pythagoras
- UVa 106 && POJ 1305 - Fermat vs. Pythagoras(毕达哥拉斯三元组)
- POJ 1305-Fermat vs. Pythagoras(本原的毕达哥拉斯三元组+枚举)
- POJ.Fermat vs. Pythagoras
- POJ1305 Fermat vs. Pythagoras【毕达哥拉斯三元组】
- poj 1305——Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras(勾股数)
- POJ 1305 Fermat vs. Pythagoras 笔记
- ZOJ Problem Set - 3499||Median
- Structs2 HelloWorld-09 UI标签
- 【华为OJ】按单词将句子逆序
- 【codechef】Gcd Queries(灵活题,分类)
- JavaScript与Objective-C之间的通信
- Poj.1305 Fermat vs. Pythagoras【毕达哥拉斯三角形】 2015/04/21
- 正则表达式
- 黑马程序员------------IO流
- TCL脚本学习
- Xcode6.2如何关闭特定文件的ARC功能
- BZOJ 3998 TJOI2015 弦论 后缀自动机
- C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
- Android能否实现一键清理后台程序后程序自启动(类似QQ微信不被清理软件杀死)
- ZOJ Problem Set - 3487||Ordinal Numbers