Skip to main content
  1. Posts/

poj 1305 (毕达哥拉斯三元组,构造勾股数)

·3 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题意是说,能构造多少本元勾股数和勾股数,要求构造的数<=n

所谓本元勾股数,就是三个勾股数没有公因数,两两互质。

由本元勾股数扩大k倍,就可以得到其他勾股数。

而构造本元勾股数的方法如下:

***a=st,b=(s^2-t^2)/2,c=(s^2+t^2)/2

其中s>t>=1是任意没有公因数的奇数!

引用一段构造正确性的证明:

本原勾股数组(PPT)是一个三元组(a,b,c),其中a,b,c无公因数,且满足a² +b² =c²。

很明显存在无穷多个勾股数组(abc同乘以n),下面研究abc没有公因数的情况,先写出一些本原勾股数组:

case:(3,4,5) (5,12,13) (8,15,17) (7,24,25) (20,21,29)(9,40,41)(12,35,37)(11,60,61)(28,45,53) (33,56,65) (16,63,65)

观察可以看出a,b奇偶性不同且c总是奇数。(用一点技巧可以证明这是正确的)

3² = 5² - 4² = (5-4)(5+4) = 1 × 9

15² = 17²-8² = (17-8)(17+8) = 9 ×25

35² = 37² - 12² = (37-12)(37+12) = 25 ×49

……

很神奇的是似乎c-b与c+b总是平方数,并且c-b与c+b木有公因数。证明一下下:假设有公因数,设d是c-b与c+b的公因数,则d也整除(c+b)+(c-b)=2c, (c+b)-(c-b) = 2b,所以d整除2c,2b,但是b,c木有公因数,又假设了(a,b,c)是本原勾股数组,从而d等于1或2,又因为d整除(c-b)(c+b)=a².a²是奇数,所以d = 1,c-b与c+b木有公因数。,又因为(c-b)(c+b)=a²,所以c-b与c+b的积是平方数,只有二者都是平方数才会出现(可以把二者分解成素数乘积直观地看出),令c+b = s²,c-b=t²,解得

c=(s²+t²)/2, b=(s²-t²)/2,a = √(c-b)(c+b) = st.这就得出了勾股数组定理:

每个本原勾股数组(a,b,c)(a为奇数,b偶数)都可由如下公式得出:a=st,b=(s²-t²)/2, c = (s²+t²)/2, 其中s>t>=1是没有公因数的奇数。

当取t=1时就可以得到上面的许多例子。

** **

 1/*************************************************************************
 2	> File Name: code/poj/1305.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年08月22日 星期六 13时49分30秒
 6 ************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#define y0 abc111qqz
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define tm crazy111qqz
25#define lr dying111qqz
26using namespace std;
27#define REP(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef unsigned long long ULL;
30const int inf = 0x3f3f3f3f;
31const int N= 1E6+7;
32bool v[N];
33int n;
34int gcd(int a,int b){
35    if (a<b) return gcd(b,a);
36    if (a%b==0) return b;
37    return gcd(b,a%b);
38}
39int main()
40{
41    while (scanf("%d",&n)!=EOF){
42	memset(v,false,sizeof(v));
43	int ans = 0 ;
44	int cnt = 0 ;
45	for ( int t = 1 ; t <= n ;t = t + 2){
46	    for ( int s = t+2 ; s*t<= n; s = s + 2){
47		if (gcd(s,t)==1){
48		    int a = s*t;
49		    int b = (s*s-t*t)/2;
50		    int c = (s*s+t*t)/2;
51		    if (a<=n&&b<=n&&c<=n){
52			ans++;
53			for ( int i = 1 ; i*a<=n&&i*b<=n&&i*c<=n;i++){
54			    v[i*a] = true;
55			    v[i*b] = true;
56			    v[i*c] = true;
57			}
58		    }
59		}
60	    }
61	}
62	for ( int i = 1 ; i <= n ; i++){
63	    if (!v[i]) cnt++;
64	}
65	printf("%d %d\n",ans,cnt);
66    }
67	return 0;
68}

Related

codeforces 534 A. Exam

·2 mins
A. Exam time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.

codeforces 482 A. Diverse Permutation(构造)

·1 min
C - C **Time Limit:**1000MS **Memory Limit:**262144KB 64bit IO Format:%I64d & %I64u Submit Status Description Permutation_p_ is an ordered set of integers _p_1, p_2, …, p__n, consisting of n distinct positive integers not larger than n. We’ll denote as_n the length of permutation _p_1, _p_2, …, p__n.

hdu 2138 How many prime numbers

ACM STEPS里的…这题前面一道是求LCM….结果接下来就是这么一道。。。 朴素会超….筛法会爆….题目顺序真是按照难度来的? 于是想到 Miller-Rabin素数测试……. 这个方法是基于费马小定理 我的理解就是… 如果我要判断n是否为素数 只要取k个数 如果满足 a^(n-1)mod n =1 那么n就很可能为素数。 证明什么的…暂时还是算了吧…论文里貌似扯了一大堆 第一次用,竟然真的A了。。。。 感觉更好的办法也许是先打一个比较小的素数表,然后每次random选取若干个进行判断…那样应该更可靠些? 本来想WA掉之后再改的。。。没想到这么写就A掉了。。。。杭电数据略水?