代码改变世界

1531. Pythagorean Proposition II

2011-04-17 17:10  Min·zc  阅读(270)  评论(0编辑  收藏  举报

http://baike.baidu.com/view/148142.htm

有用的是最后面的公式

实现

//n目标数

//ans为答案

//所求为和为n的所有勾股数的情况

 1 #include <iostream>
 2 #include <math.h>
 3 using namespace std;
 4 int n;
 5 int ans;
 6 int gcd(int a,int b)
 7 {
 8     if(b==0)
 9         return a;
10     else
11         return gcd(b,a%b);
12 }
13 void work()
14 {
15     for(int i=1;i<sqrt(n);i++)
16         for(int j=i+1;2*j*j+2*j*i<=n;j++)
17         {
18             int x=2*i*j;
19             int y=j*j-i*i;
20             int z=j*j+i*i;
21             if(gcd(x,gcd(y,z))==1)
22             {
23                 int s=x+y+z;
24                 if(n%s==0)
25                     ans++;
26             }
27         }
28 }
29 int main()
30 {
31     int t;
32     cin>>t;
33     while(t--)
34     {
35         cin>>n;
36         ans=0;
37         work();
38         cout<<ans<<endl;
39     }
40     return 0;
41 }