A Simple Problem: Find how many integers k in 0~n satisfy (2 * k) XOR (3 * k) == k, where XOR is an exclusive OR operation...

Title description

Find how many integers k in 0~n satisfy (2 * k) XOR (3 * k) == k, where XOR is an exclusive OR operation.

Input data

The first line of input data is a positive integer T (T<=30), which represents the number of groups of test data. In the following T rows, each row contains a set of test data, which contains a positive integer n (n<=1000000000).

Output Data

For each group of input data, output a line of result "Case #id: M", which means that the result of the id group data is M, and id starts from 1.

Sample input

5
0
1
2
3
4

Sample output

Case #1: 1
Case #2: 2
Case #3: 3
Case #4: 3
Case #5: 4

Experience: This is hard to think about, refer to other big guys code

def getM(n):      
    binaryL = []      
    while n:   # 转二进制          
        binaryL.append(n%2)          
        n = n//2      
    count = 0      
    pre = -1      
    while binaryL:          
        temp = binaryL.pop()          
        if temp == 1:                 
            count += fixedL[len(binaryL)][0] + fixedL[len(binaryL)][1]          
        if (temp == 1 and pre == 1):    # 两个1相邻则结束              
            break          
        pre = temp      
    return count    

fixedL = [[0,0] for row in range(50)]  
fixedL[0][0] = 1  
for i in range(49):      
    fixedL[i + 1][1] += fixedL[i][0]      
    fixedL[i + 1][0] += fixedL[i][1] + fixedL[i][0]    

T = int(input())  
for t in range(T):      
    n = int(input())+1  # 加上0这种情况      
    print("Case #{}: {}".format(t+1,getM(n)))  

Guess you like

Origin blog.csdn.net/tianxiefenxiang/article/details/107696094
k