Fermat vs. Pythagoras
Time Limit: 2000MS |
|
Memory Limit: 10000K |
Total 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).
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^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 1000000007
using 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++;
else
continue;
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;
}