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

来源:互联网 发布:淘宝店铺红包怎么领取 编辑:程序博客网 时间:2024/04/28 01:16

毕达哥斯拉定理其实就是勾股定理,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;}



0 0
原创粉丝点击