uva12716 gcd
题意:给出N,1<=b<=a<=N,求满足gcd(a,b)=a xor b的pair (a,b)的个数
有个重要的结论:若gcd(a,b)=a xor b=c,那么b=a-c
如果一个个求gcd肯定不行。
令f[i]表示满足条件的pair (a,b)中,a=i的个数
枚举c,令a是c的所有倍数,求出b=a-c。若b=a xor c那么f[a]++
最后求f[]的前缀和S[],那么答案就是S[N](要求a<=N啦~)
1 #include <stdio.h> 2 #include <string.h> 3 #define LL long long 4 #define MX 30000005 5 6 int N,T; 7 LL S[MX],f[MX]; 8 9 int main() 10 { 11 memset(f,0,sizeof(f)); 12 for (int c=1;c<=MX;c++) 13 for (int a=2*c;a<=MX;a+=c) 14 { 15 int b=a-c; 16 if ((a^b)==c) 17 { 18 //printf("%d %d %d %d\n",a,b,a^b,c); 19 f[a]++; 20 } 21 } 22 23 S[1]=f[1]; 24 for (int i=2;i<=MX;i++) 25 { 26 //printf("%d ",f[i]); 27 S[i]=S[i-1]+f[i]; 28 } 29 30 scanf("%d",&T); 31 for (int times=1;times<=T;times++) 32 { 33 scanf("%d",&N); 34 printf("Case %d: %lld\n",times,S[N]); 35 } 36 37 return 0; 38 }
reference:http://blog.csdn.net/u013451221/article/details/38512091
posted on 2015-03-02 18:05 Pentium.Labs 阅读(223) 评论(0) 编辑 收藏 举报