http://acm.hdu.edu.cn/showproblem.php?pid=1496
View Code
1 #include<stdio.h> 2 #include<memory.h> 3 4 int pin[101]; 5 int hash[2000001]; 6 7 int main( ) 8 { 9 int a; 10 int b; 11 int c; 12 int d; 13 int i; 14 int j; 15 int sum; 16 for( i=1; i<101; i++ ) 17 pin[i] = i*i; 18 while( scanf("%d%d%d%d",&a,&b,&c,&d) != EOF ) 19 { 20 if( (a>0&&b>0&&c>0&&d>0) || (a<0&&b<0&&c<0&&d<0) ) 21 { 22 printf("0\n"); 23 continue; 24 } 25 memset(hash,0,sizeof(hash)); 26 for( i=1; i<=100; i++ ) 27 for( j=1; j<=100; j++ ) 28 hash[a*pin[i]+b*pin[j]+1000000]++; 29 sum = 0; 30 for( i=1; i<=100; i++ ) 31 for( j=1; j<=100; j++ ) 32 sum += hash[-(c*pin[i]+d*pin[j])+1000000]; 33 printf("%d\n",sum*16); 34 } 35 return 0; 36 } 37
“两层循环最多只可能产生10000个不同的结果,开200W的数组将会浪费很多初始化的时间,所以开小数组+处理冲突会比较好.”
View Code
1 #include<stdio.h> 2 #include<memory.h> 3 4 #define MAX 50001 5 6 int f[MAX]; 7 int g[MAX]; 8 int pin[101]; 9 10 int hash( int k ) 11 { 12 int t = k%MAX; 13 if( t<0 ) 14 t += MAX; 15 while( f[t]!=0 && g[t]!=k ) 16 t = (t+1)%MAX; 17 return t; 18 } 19 20 int main( ) 21 { 22 int a; 23 int b; 24 int c; 25 int d; 26 int i; 27 int j; 28 int sum; 29 int s; 30 int p; 31 for( i=1; i<101; i++ ) 32 pin[i] = i*i; 33 while( scanf("%d%d%d%d",&a,&b,&c,&d) != EOF ) 34 { 35 if( (a>0&&b>0&&c>0&&d>0) || (a<0&&b<0&&c<0&&d<0) ) 36 { 37 printf("0\n"); 38 continue; 39 } 40 memset(f,0,sizeof(f)); 41 for( i=1; i<=100; i++ ) 42 for( j=1; j<=100; j++ ) 43 { 44 s = a*pin[i]+b*pin[j]; 45 p = hash(s); 46 g[p] = s; 47 f[p]++; 48 } 49 sum = 0; 50 for( i=1; i<=100; i++ ) 51 for( j=1; j<=100; j++ ) 52 { 53 s = -(c*pin[i]+d*pin[j]); 54 p = hash(s); 55 sum += f[p]; 56 } 57 printf("%d\n",sum*16); 58 } 59 return 0; 60 } 61