Time Limit: 2 Sec Memory Limit: 32 MB
Description
Pythagorean Theorem is “humanity’s greatest scientific discoveries of the ten” is a basic elementary geometry theorems. “This theorem has a very long history, almost all ancient civilizations (Greece, China, Egypt, Babylon, India, etc.) have studied this theorem. Pythagorean Theorem in the West known as the Pythagorean Theorem, are said to Ancient Greek mathematician and philosopher Pythagoras (Pythagoras, BC572 ~ BC497) was first discovered in BC550.
Pythagorean Theorem in China known as the Gougu/勾股 Theorem. Around 1100 BC, the Western Zhou period, the ancient Chinese mathematician, Shanggao, first described the Gougu Theorem. In the famous ancient mathematics book, 《九章算术》,the proof was given (left). The Gougu Theorem is that “in the right triangle, the sum of the squares of two right sides is equal to the square of the hypotenuse.” In other words, the three sides (a, b, c) of right-angled triangle satisfies the following equation:
a2+b2=c2
Where a is called 勾/Gou, b is 股/Gu, and c is 弦/Xian.
For given c, how many different positive integer solutions are there? ((a,b,c)are relatively-prime. ) It is an interesting problem.
Now, your task is to solve it.
Input
There are several test cases; each test case contains one positive integer c (0 < c < 230 ) in a line. c = 0 is the end of input and need not to process.
Output
For each test case, your program should output by following format:
Case #:
There are n solution(s).
a^2 + b^2 = c^2
…..(total n line(s) )….
Where # is the test case number starting from 1, two consecutive cases should be separated by a single blank line.
For each given c, output all solutions satisfying: a2+b2=c2
Each solution should be in one line, and in order a
Sample Input
65
20
0
Sample Output
Case 1:
There are 2 solution(s).
16^2 + 63^2 = 65^2
33^2 + 56^2 = 65^2
Case 2:
There are 0 solution(s).
题目链接:HZNU-1480
题目大意:给出一个c,问满足a^2+b^2=c^2的(a,b)有几对,并且a,b,c三者互质。
题目思路:
勾股数的一个性质。
设a,b,c是直角三角形的三条边,a,b是直角边,c是斜边,则有:
a = 2 * m * n;
b = m^2 - n^2;
c = m^2 + n^2;
该公式只能求出基本的勾股数,无法求出派生勾股数,如6,8,10无法求出。还需要注意两点:
1)由于两边之和要大于第三边,所以a*m*n+m^2-m^2 > m^2+n^2.即 m > n
2)a,b,c必须互质,否则无法得到派生的勾股数
另外输出注意,从小到大输出!!(WA了2个小时/(ㄒoㄒ)/~~)
以下是代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
#define ll long long
ll c;
struct node
{
ll a,b;
friend bool operator<( node x, node y ){
return x.a<y.a;
}
}p[42770];
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a%b);
}
bool cmp(node x,node y)
{
return x.a < y.a;
}
int main()
{
int ok = 0;
int loy = 1;
while(cin >> c && c)
{
if (ok) cout << endl;
ok = 1;
printf("Case %d:\n",loy++);
int k = 0;
ll len = (ll)sqrt(c + 0.5);
for (ll m = 1;m <= len;m++)
{
ll n = sqrt(c - m * m + 0.5);
ll a = 2 * n * m;
ll b = abs(m * m - n * n);
ll g = gcd(gcd(a,b),c);
if(m < n && m * m + n * n == c && g == 1)
{
p[k].a = min(a,b);
p[k++].b = max(a,b);
}
}
printf("There are %d solution(s).\n",k);
sort(p,p + k, cmp);
for (int i = 0; i < k; i++)
{
printf("%lld^2 + %lld^2 = %lld^2\n",p[i].a,p[i].b,c);
}
}
return 0;
}