您的位置:首页 > 其它

GCD XOR 求约数以及推论 UVA - 12716

2017-06-27 14:59 267 查看
题意:给你一个N,输出有多少对 < A,B>,满足A XOR B= GCD (A,B);

(1<=B<=A<=N)。

参考http://www.cnblogs.com/naturepengchen/articles/3952145.html

有几个结论:(1)若 a xor b = c,则 a xor c = b。

      (2)a - b <= a xor b,(a >= b)

      (3)若 gcd(a,b)= a xor b = c ,(a >= b),

       由(2)得:a - b <= c。

       再令 a = k1×c,b = k2 × c,(k1 >= k2),所以 a - b = (k1 - k2)× c,所以 a - b >= c。总:a - b = c

       (这里若k1 = k2,那么 a = b,那么 a xor b = 0)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>

using namespace std;
#define maxn 30000000
int s[maxn+10];

void init()
{
for(int c=1;c<=maxn;c++)
{
for(int a=c+c;a<=maxn;a+=c)//因为B是大于0,的,所以从2c开始
{
int b=a-c;
if((a^b)==c)
s[a]++;
}
}
for(int i=1;i<=maxn;i++)
s[i]+=s[i-1];
}
int main()
{
int T;
scanf("%d",&T);
int case1=1;
init();
while(T--)
{
int n;
scanf("%d",&n);
printf("Case %d: ",case1++);
printf("%d\n",s
);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: