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

本文介绍了一种高效计算特定范围内勾股数组数量及其非成员数的方法。通过枚举和筛选策略,利用数学公式直接计算勾股数组的组成部分,避免了传统枚举a、b、c的方式。该算法不仅计算了互质的勾股数组数量,还统计了不在任何勾股数组中的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值