# UVA - 12716

UVA - 12716

Given an integer N, find 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 first 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 first 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).

题意

输入一个数字输出小于该数的,满A xor B == GCD(A , B)的无序数对的数量。数据范围,N < 3e7;

思路

直接把数据较小的答案全部打印出来观察,可以发现一个规律是满足条件的情况下,假设A xor B = C 可得 C == A - B.则 B = A - C,又由 C == GCD (A,B)可知,C为A的约数,枚举C 和
A ,判断一下 A xor (A- C) == C,打表存一下,查询直接输出。

#include<bits/stdc++.h>
using namespace std;
int s[30000005];
void solve()
{
    for (int c = 1;c < 30000005;c++)
    {
        for (int a = c+c;a < 30000005;a+=c)
        {
            if((a^(a-c))==c)
            {
                s[a]++;
            }
        }
       s[c] += s[c-1];
    }
}
int main(int argc, char const *argv[])
{
    int  t;
    int pp=1;
    cin >>t;
    solve();
    while(t--)
    {
        int n,c=1;
        int ans=0;
        cin>>n;
       printf("Case %d: %d\n",pp++,s[n] );
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/cifiyoo/p/9428883.html