TOJ 3863.Right Triangle(勾股数)
来源:互联网 发布:python简明教程pdf下载 编辑:程序博客网 时间:2024/06/04 18:47
题目链接: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。
实际上就是把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为偶数时由于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);}}
- TOJ 3863.Right Triangle(勾股数)
- TOJ 3502: Triangle (pick定理)
- TOJ Right Triangles II
- Isosceles Right Triangle
- FZU 1669 Right-angled Triangle(毕达哥拉斯三元组)
- 直角三角形判定(Sides of a Right Triangle)
- TOJ 1701: Face The Right Way
- TOJ 1650 Flying Right -- 贪心 + 线段树
- Sticks and Right Triangle HDU
- hdu 3939 Sticks and Right Triangle 勾股数+容斥原理+欧拉函数
- HDU3939Sticks and Right Triangle勾股数,容斥,欧拉筛,积性函数
- hdu 3939 (Sticks and Right Triangle)
- FZU1669 Right-angled Triangle【毕达哥拉斯三元组】
- FZU 1669 Right-angled Triangle 解毕达哥拉斯三元组
- FZU 1669 Right-angled Triangle (本原毕达哥拉斯三元组)
- FZU 1669 Right-angled Triangle 毕达哥拉斯三元组
- FZU 1669 Right-angled Triangle(解毕达哥拉斯三元组)
- TOJ 2896.Antimonotonicity(贪心)
- Merge k Sorted Lists 的分治法总结
- java基础——AtomicInteger的并发处理
- java NIO之socket channel
- 使用Clonezilla(再生龙)备份还原centos系统 linux 系统备份还原
- unity 3D远程唤醒计算机
- TOJ 3863.Right Triangle(勾股数)
- Python Flask 中用 SQLAlchemy 访问 Mysql 数据库
- 初次使用instrument leaks小结
- HDU 1525 Euclid's Game【博弈 PN找规律】
- sublime3 emmet用法
- 求数列和
- 变量和基本类型
- 旅行_纪中2569_dfs+强连通分量
- 【21】Merge Two Sorted Lists