题目链接:http://acm.tju.edu.cn/toj/showp3863.html
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 846 Accepted Runs: 259
When Ant studied in middle school, he liked math very much and did well in it. One day, his teacher taught him the pythagoras theorem (which was also called Pythagorean theorem(���ɶ���) in Chinese). Pythagorean number is a group of three integers a,b,c( a^2 + b^2 = c^2 ) which can constitue the edges of a right triangle. Ant was interested in pythagorean number. He thought every integer n( n > 2 ) could be the smallest integer of pythagorean number. He also found that, if the integer was n ( n>2 ) a prime, there would be only one group of pythagorean number whose smallest integer was n. Now here comes the question. Given you a prime n, caculate the pythagorean number whose smallest one is n.
Input
There are several test cases. For each test case, there is a single line contains a prime n ( n > 2 )
Output
For each test case, output all the three integers of the pythagorean number seperated by single spaces which is sorted in ascending order and n is the smallest of the three integers.
Sample Input
3
Sample Output
3 4 5
Source: TJU 2012 Team Selection
这道题是关于勾股定理的,所以首先记录一下复习到的关于勾股定理的一些知识:
简介
所谓勾股数,一般是指能够构成直角三角形三条边的三个正整数(例如a,b,c)。
即a^2+b^2=c^2,a,b,c∈N
又由于,任何一个勾股数组(a,b,c)内的三个数同时乘以一个整数n得到的新数组(na,nb,nc)仍然是勾股数,所以一般我们想找的是a,b,c互质的勾股数组。
关于这样的数组,比较常用也比较实用的套路有以下两种:
第一套路
当a为大于1的奇数2n+1时,b=2n^2+2n,c=2n^2+2n+1。
n=1时(a,b,c)=(3,4,5)
n=2时(a,b,c)=(5,12,13)
n=3时(a,b,c)=(7,24,25)
... ...
这是最经典的一个套路,而且由于两个连续自然数必然互质,所以用这个套路得到的勾股数组全部都是互质的。
第二套路
2、当a为大于4的偶数2n时,b=n^2-1,c=n^2+1
也就是把a的一半的平方分别减1和加1,例如:
n=3时(a,b,c)=(6,8,10)
n=4时(a,b,c)=(8,15,17)
n=5时(a,b,c)=(10,24,26)
n=6时(a,b,c)=(12,35,37)
... ...
这是第二经典的套路,当n为奇数时由于(a,b,c)是三个偶数,所以该勾股数组必然不是互质的;而n为偶数时由于b、c是两个连续奇数必然互质,所以该勾股数组互质。
所以如果你只想得到互质的数组,这条可以改成,对于a=4n (n>=2), b=4n²-1, c=4n²+1,例如:
n=2时(a,b,c)=(8,15,17)
n=3时(a,b,c)=(12,35,37)
n=4时(a,b,c)=(16,63,65)
... ...
#include <stdio.h>
int main(){
int m;
while(~scanf("%d",&m)){
int n=(m-1)/2;
printf("%d %d %d\n",m,2*n*n+2*n,2*n*n+2*n+1);
}
}