UVALive-6657-GCD XOR,uvalive-6657-gcd

來源:互聯網
上載者:User

UVALive-6657-GCD XOR,uvalive-6657-gcd
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


思路:相差為兩個i以上的兩個i的倍數異或值不可能等於i,可以自己推一下。有了這個結論就好辦了,直接枚舉最大公約數即可。


#include <stdio.h>int d[30000001];int main(){    int T,i,j,n=30000000;    for(i=1;i<=n;i++) for(j=i+i+i;j<=n;j+=i) if((j^(j-i))==i) d[j]++;//相差為兩個i以上的兩個i的倍數異或不可能等於i    for(i=1;i<=n;i++) d[i]+=d[i-1];    scanf("%d",&T);    for(i=1;i<=T;i++)    {        scanf("%d",&n);        printf("Case %d: %d\n",i,d[n]);    }}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.