您的位置:首页 > Web前端

POJ-1305-Fermat vs. Pythagoras-(本原勾股数)

2017-10-29 20:35 295 查看
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
10
25
100

Sample Output
1 4
4 9
16 27


题意:

让求一组数a,b,c满足a*a+b*b=c*c,这里a<b<c,且a,b,c<=n且a,b,c没有公约数,问能有多少组这样的数,并统计n以内未被这样的一组数包含的数

这里若有s,t,且其中s>t>=1是没有公因数的奇数

这里令a=st,b=(s²-t²)/2, c = (s²+t²)/2

勾股数组原理参考:http://www.cnblogs.com/PegasusWang/archive/2013/01/22/2872213.html

http://blog.csdn.net/toudsour/article/details/45010839

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define M 1000005
typedef long long ll;

int n;
int vis[M];
inline ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
ll i,j;
ll a,b,c;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));
int ans1=0,ans2=0;
for(i=3;i<=n;i+=2)
{
for(j=i+2;j<=n;j+=2)
{
if(gcd(i,j)==1)
{
a=i*j;
b=(j*j-i*i)/2;
c=(i*i+j*j)/2;
if(a==c||b==c||a==b)
continue;
if(c>n)
break;
if(a*a+b*b==c*c)
{
ans1++;
}
else
continue;
for(ll k=1;k*c<=n;k++)//对于勾股数组的倍数进行筛选
{
vis[a*k]=1;
vis[b*k]=1;
vis[c*k]=1;
}
}
}
}
for(i=n;i>=1;i--)
if(vis[i]==0) ans2++;
printf("%d %d\n",ans1,ans2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: