UVA12716:GCD XOR

UVA12716:GCD XOR

Meaning of the questions:

Given \ (T \ leq10 ^ 4 \ ) set of test samples, ask \ (\ sum_ {I}. 1 = n-^ \ ^ n-sum_j [GCD (A, B) = A \ XOR \ B] \) .

\(n\leq3*10^7\)

Ideas:


\[ gcd(a,b)=a\ xor\ b=c \]

  • \ (. 1: A \ XOR \ B = C \) , then there is \ (A \ XOR \ C = B \) .
  • \(2:a-b\leq a\ xor\ b\).

  • \(3:gcd(a,b)=a\ xor\ b=c\),令\(a=k_1c,b=k_2c,(k_1\geq k_2)\)
    • \(a-b=(k_1-k_2)*c\)
    • And because \ (ab \ Leq c \) .
    • So there \ (ab = c \) .

So the result is:
\ [ab & = A \ XOR \ B \]
to count yes.

Time complexity
\ [n + \ frac {n } {2} + \ frac {n} {3} + ... = n (1+ \ frac {1} {2} + \ frac {1} {2} + ...) = n-(LNN + C) \]
\ (LNN + C \) is the summation of harmonic series.

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e7+10;
int ans[maxn];

void init(int n)
{
    int k = n>>1;
    for(int b = 1; b <= k; b++)
        for(int a = b+b; a <= n; a += b)
        if((a^b) == a-b) ans[a]++;
    for(int i = 1; i <= n; i++)
        ans[i] += ans[i-1];
}

int cas;
inline void solve()
{
    int n; scanf("%d", &n);
    printf("Case %d: %d\n", ++cas, ans[n]);
}

int main()
{
    init(maxn-5);
    int T; scanf("%d", &T);
    while(T--) solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/zxytxdy/p/12287831.html
XOR