UVA 12716 GCD XOR(猜想)

9 篇文章 0 订阅

题意:

输入整数n,能使a,b<= n 且gcd(a,b) = a ^ b的a, b 有多少对。

解题思路:

枚举,a, c, b = a -c, 验证是否有 c = a ^ b, 打表预处理。


Description

Given an integer N, nd how many pairs (A; B) are there such that: gcd(A; B) = A xor B where
1 B A N.
Here gcd(A; B) means the greatest common divisor of the numbers A and B. And A xor B is the
value of the bitwise xor operation on the binary representation of A and B.
Input
The rst line of the input contains an integer T (T 10000) denoting the number of test cases. The
following T lines contain an integer N (1 N 30000000).
Output
For each test case, print the case number rst in the format, `Case X:' (here, X is the serial of the
input) followed by a space and then the answer for that case. There is no new-line between cases.
Explanation
Sample 1: For N = 7, there are four valid pairs: (3, 2), (5, 4), (6, 4) and (7, 6)

Sample Input

2
7
20000000

Sample Output

Case 1: 4
Case 2: 34866117

#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)

int buf[10];
inline long long read()
{
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

inline void writenum(int i)
{
    int p = 0;
    if(i == 0) p++;
    else while(i)
        {
            buf[p++] = i % 10;
            i /= 10;
        }
    for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 30000001
const int INF = 0x3f3f3f3f;
int ans[MAX_N];

void init()
{
    memset(ans, 0 , sizeof(ans));
    for(int i = 1 ; i < MAX_N ; i++)
    {
        for(int j = i * 2 ; j <  MAX_N ; j += i)
        {
            if((j ^ (j - i)) == i)
            {
                ans[j]++;
            }
        }
    }

    for(int i = 2 ; i < MAX_N ; i++)
    {
        ans[i] += ans[i - 1];
    }
}

int main()
{
    init();
    int t =read();
    int cas = 1;
    while(t--)
    {
        int n =  read();
        printf("Case %d: %d\n", cas++, ans[n]);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值