毕达哥拉斯三元组:x*x+y*y=z*z
定理:正整数x,y,z构成一个本原毕达哥拉斯三元组且y为偶数 当且仅当存在互素的正整数m,n(m>n)
其中m为奇数n为偶数或者m为偶数n为奇数(奇偶性不同)并且满足:
x=m*m-n*n
y=2*m*n
z=m*m+n*n
poj 1305
Fermat vs. Pythagoras
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 1381 | Accepted: 800 |
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
本原毕达哥拉斯三元组的个数 是根据m,n的值确定的
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1000010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char ch;
int a = 0;
while((ch = getchar()) == ' ' | ch == '\n');
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return a;
}
void Print(int a) //输出外挂
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
int gcd(int a,int b)
{
if(b==0) return a;
return gcd(b,a%b);
}
bool flag[MAXN];
int main()
{
//fread;
int p;
while(scanf("%d",&p)!=EOF)
{
int num1=0,num2=0;//本原个数 涉及到数的个数
MEM(flag,0);
int m,n,x,y,z;
int tmp=sqrt((double)p);
for(n=1;n<=tmp;n++)
{
for(m=n+1;m<=tmp;m++)
{
if(m*m+n*n>p)
break;
if(n%2!=m%2)
{
if(gcd(m,n)==1)
{
x=m*m-n*n;
y=2*m*n;
z=m*m+n*n;
num1++;
for(int i=1;;i++)
{
if(i*z>p)
break;
flag[i*x]=1;
flag[i*y]=1;
flag[i*z]=1;
}
}
}
}
}
for(int i=1;i<=p;i++)
if(!flag[i])
num2++;
printf("%d %d\n",num1,num2);
}
return 0;
}
fzu 1669
Problem 1669 Right-angled Triangle
Accept: 67 Submit: 135
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
- A right triangle (or right-angled triangle, formerly called a rectangled triangle) has one 90° internal angle (a right angle). The side opposite to the right angle is the hypotenuse; it is the longest side in the right triangle. The other two sides are the legs or catheti (singular: cathetus) of the triangle. Right triangles conform to the Pythagorean Theorem, wherein the sum of the squares of the two legs is equal to the square of the hypotenuse, i.e., a^2 + b^2 = c^2, where a and b are the legs and c is the hypotenuse.
- An oblique triangle has no internal angle equal to 90°.
- An obtuse triangle is an oblique triangle with one internal angle larger than 90° (an obtuse angle).
- An acute triangle is an oblique triangle with internal angles all smaller than 90° (three acute angles). An equilateral triangle is an acute triangle, but not all acute triangles are equilateral triangles.
Input
Output
Sample Input
1240
Sample Output
15
Hint
There are five right-angled triangles where a + b + c ≤ 40. That are one right-angled triangle where a = 3, b = 4 and c = 5; one right-angled triangle where a = 6, b = 8 and c = 10; one right-angled triangle where a = 5, b = 12 and c = 13; one right-angled triangle where a = 9, b = 12 and c = 15; one right-angled triangle where a = 8, b = 15 and c = 17.
c*c=a*a+b*b 同样是满足毕达哥拉斯三元组的
根据满足的情况 在将三个值相加 确保满足大小要求
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 10000010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char ch;
int a = 0;
while((ch = getchar()) == ' ' | ch == '\n');
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return a;
}
void Print(int a) //输出外挂
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
int flag[MAXN];
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int main()
{
//fread;
int l;
while(scanf("%d",&l)!=EOF)
{
int num=0;
int tmp,m,n,x,y,z;
tmp=sqrt((double)l);
MEM(flag,0);
for(n=1;n<=tmp;n++)
{
for(m=n+1;m<=tmp;m++)
{
if(2*m*m+2*m*n>l)//x+y+z>l
break;
if(n%2!=m%2)
{
if(gcd(m,n)==1)
{
x=m*m-n*n;
y=2*n*m;
z=m*m+n*n;
for(int i=1;;i++)
{
if(i*(x+y+z)>l)
break;
num++;
}
}
}
}
}
printf("%d\n",num);
}
return 0;
}