FZU1669 Right-angled Triangle 本原毕达哥斯拉三元组 特殊不定方程的应用

本文介绍了一种基于毕达哥拉斯定理的算法,用于找出所有满足特定条件的毕达哥拉斯三元组,即求解直角三角形的直角边长度a、b及斜边长度c,在限定条件下a+b+c≤L的所有可能组合。

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

毕达哥斯拉定理其实就是勾股定理,x^2 + y^2 = z^2,满足这个方程的正整数三元组被成为毕达哥斯拉三元组

定理:正整数x,y,z,构成一个本原毕达哥斯拉三元组且y为偶数,当且仅当存在互素的正整数n,m,(n<m),其中m与n的奇偶性不同,并且满足

x = m^2 - n^2

y = 2 * m * n;

i * z = m^2 + n^2

解析前小广告!总是做算法,不如来个陶冶情操的文章一篇: http://www.sanwen.net/subject/3628849/

题意:

求满足以a,b,为直角边的c为斜边,切满足方程a +b +c <=L的直角三角形的个数


根据三元组 

x = m^2 - n^2

y = 2 * m * n;

i * z = m^2 + n^2

对n,m,进行枚举,然后讲三元组乘以 i 倍,就能求出所有满足条件的毕达哥斯拉三元组,根据定理可知 求出满足要求的三元组 也就求出了个数


#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>

#define ll long long

#define eps 1e-8

#define inf 0xfffffff

//const ll INF = 1ll<<61;

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;

bool vis[1000000 + 5];

int ans = 0;

void clear() {
	memset(vis,false,sizeof(vis));
	ans = 0;
}

ll exgcd(ll a, ll b, ll &x0, ll &y0)  {
	if(!b) {
		x0 = 1; y0 = 0;
		return a;
	}
	ll r = exgcd(b, a%b, y0, x0);
	y0 -= a/b*x0;
	return r;
}

void cal(int t) {
	int tmpn = sqrt(t * 1.00);
	for(int n=1;n<=tmpn;n++) { 
		for(int m = n+1;m<=tmpn;m++) {
			if(m * m * 2+ 2 * n *m > t) break;
			if(n%2 != m%2) {
				ll x0,y0;
				if(exgcd(m,n,x0,y0) == 1) {
					int x = m * m - n * n;
					int y = 2 * m * n;
					int z = m * m + n * n;
					for(int i=1;;i++) {
						if(i * (x + y + z) > t)break;
						ans++;
					}
				}
			}
		}
	}
}


int main() {
	int n;
	while(scanf("%d",&n) == 1) {
		clear();
		cal(n);
		printf("%d\n",ans);
	}
	return EXIT_SUCCESS;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值