TOJ 3863.Right Triangle(勾股数)

来源:互联网 发布:python简明教程pdf下载 编辑:程序博客网 时间:2024/06/04 18:47

题目链接:http://acm.tju.edu.cn/toj/showp3863.html


3863.   Right Triangle
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 nn > 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
Submit   List    Runs   Forum   Statistics



这道题是关于勾股定理的,所以首先记录一下复习到的关于勾股定理的一些知识:

简介

所谓勾股数,一般是指能够构成直角三角形三条边的三个正整数(例如a,b,c)

a^2+b^2=c^2,a,b,cN

又由于,任何一个勾股数组(a,b,c)内的三个数同时乘以一个整数n得到的新数组(na,nb,nc)仍然是勾股数,所以一般我们想找的是a,b,c互质的勾股数组。

关于这样的数组,比较常用也比较实用的套路有以下两种:


第一套路

a为大于1奇数2n+1时,b=2n^2+2n,c=2n^2+2n+1

实际上就是把a平方数拆成两个连续自然数,例如:

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为偶数时由于bc是两个连续奇数必然互质,所以该勾股数组互质。

所以如果你只想得到互质的数组,这条可以改成,对于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)

... ...

 


这道题的题目明确说了最小的值n是一个质数(n>2),故实际上可以构成勾股数的只有套路一了,所以根据套路一写就行,很水,通过这道题主要是复习一下勾股定理相关知识。

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


0 0
原创粉丝点击