Under construction

Still being built. Things may change

New Lottery Game (Large)

Time limit5sMemory limit512 MB

Summary
Count pairs x below A and y below B whose bitwise AND is below K for up to 100 test cases.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

The lottery is changing. It used to draw the winning number with a single machine, but after repeated cheating the lottery company added a second machine. The new winning number is the bitwise AND of the two random numbers the two machines produce.

To take the bitwise AND of XX and YY, write both of them in binary. A bit of the result is 1 when the matching bits of XX and YY are both 1, and 0 otherwise. Most programming languages write this operation as X & Y.

For example, suppose the old machine draws 7 = 0111 and the new machine draws 11 = 1011. The winning number is (0111 AND 1011) = 0011 = 3.

An employee of the lottery company leaked one more fact: the old machine always draws a non-negative integer smaller than AA, and the new machine always draws a non-negative integer smaller than BB.

Catalina wants to win, so she bought every non-negative integer smaller than KK.

You are given AA, BB and KK. Count the pairs of numbers the two machines can draw that make Catalina a winner.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains three integers AA, BB and KK, separated by spaces.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤A≤1091 \le A \le 10^9
  • 1≤B≤1091 \le B \le 10^9
  • 1≤K≤1091 \le K \le 10^9

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the number of pairs the two machines can draw that make Catalina a winner.

Note

Take A=3A = 3, B=4B = 4, K=2K = 2. Ten pairs make Catalina win: (0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0) and (2, 1). The first number is the draw of the old machine and the second one is the draw of the new machine, so (0, 1) and (1, 0) are different pairs. The machines can also draw (2, 2), but (2 AND 2) = 2 and Catalina bought only 0 and 1, so that pair does not win.

Examples6

  1. Example 1

    Input
    5
    3 4 2
    4 5 2
    7 8 5
    45 56 35
    103 143 88
    
    Expected output
    Case #1: 10
    Case #2: 16
    Case #3: 52
    Case #4: 2411
    Case #5: 14377
    
  2. Example 2

    Input
    1
    1 1 1
    
    Expected output
    Case #1: 1
    
  3. Example 3

    Input
    4
    1 1000000000 1
    1000000000 1 1
    1 1 1000000000
    1000000000 1000000000 1
    
    Expected output
    Case #1: 1000000000
    Case #2: 1000000000
    Case #3: 1
    Case #4: 200590421895675
    
  4. Example 4

    Input
    1
    1000000000 1000000000 1000000000
    
    Expected output
    Case #1: 1000000000000000000
    
  5. Example 5

    Input
    6
    1024 1024 1
    1024 1024 1024
    1023 1023 512
    512 512 511
    536870912 536870912 1
    999999999 999999999 1
    
    Expected output
    Case #1: 59049
    Case #2: 1048576
    Case #3: 785408
    Case #4: 262143
    Case #5: 68630377364883
    Case #6: 200590421894651
    
  6. Example 6

    Input
    5
    999999999 999999999 999999999
    999999937 1000000000 524288
    123456789 987654321 1
    1000000000 999999999 500000000
    2 2 1
    
    Expected output
    Case #1: 999999998000000001
    Case #2: 47439137585758208
    Case #3: 56922528032481
    Case #4: 784361898693033984
    Case #5: 3