2009-01-11 09:05:57來源不明
2008 NPSC A. 畢達哥拉斯之謎
/**********************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<math.h>
- int gcd(int a,int b)
- {
- int temp;
- while(a%b)
- {
- temp=a;
- a=b;
- b=temp%b;
- }
- return b;
- }
- int time[10000001]={0}; /*計錄素勾股數個數*/
- int main() /*先建立資料庫*/
- {
- int n,m,c; /*n m為下面公式所建立的*/
- for(n=1;n<2237;n++)
/*計算個別素勾股數個數 n的平方只要跑到5000000即可*/
- {
- for(m=n+1;m<3163;m=m+2)
/*處理的順序很重要3163的平方 最接近100000000*/
- {
- if (gcd(m,n)>1) continue; /*不互質則跳過 */
- c=m*m+n*n;
- if(c>=10000001) break;
- time[c]++;
- }
- }
- for (c=1;c<10000001;c++) /*加總*/
- time[c]=time[c]+time[c-1]; /*形成資料庫*/
- while (scanf("%d",&c)==1&&c!=0)
- {
- printf("%d\n",time[c]);
- }
- return 0;
- }
- /*利用此一性質,計算所有斜邊小於10000000的素勾股數,*/
- /* abc為三邊 m > n 、 m 和 n 均是正整*/
- /*a = m^2 -n^2, */
- /*b = 2mn, */
- /*c = m^2+n^2*/