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

Source: Internet
Author: User
Tags greatest common divisor

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
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.
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
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


Idea: The difference is that the multiple or value of the two I above two cannot be equal to I. You can push it by yourself. With this conclusion, you can simply enumerate the maximum common approx.


# Include <stdio. h> int d [30000001]; int main () {int T, I, j, n = 30000000; for (I = 1; I <= n; I ++) for (j = I + I; j <= n; j + = I) if (j ^ (j-I) = I) d [j] ++; // The difference is a multiple of two I above two I or cannot be equal to 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]) ;}}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.