Under construction

Still being built. Things may change

POPCOUNT

Time limit1sMemory limit1024 MB

Summary
Given N, A, and B, the task is to sum f(i) for i from 1 to N, where f(i) is the maximum of A times popcount(a) plus B times popcount(b) over all splits a+b=i.
Level

Medium6 of 10

Topics
Dynamic programming, Bit manipulation, Math
Solved
No attempts yet

Problem

For a nonnegative integer kk, popcount(k)\mathrm{popcount}(k) is the number of 1s in the binary representation of kk.

For positive integers AA, BB, and xx, define f(x)f(x) as follows.

f(x)=max⁡a+b=x{A×popcount(a)+B×popcount(b)}f(x)=\max_{a+b=x}\{A\times \mathrm{popcount}(a) + B\times \mathrm{popcount}(b)\}

(aa and bb are nonnegative integers)

Given an integer NN, find the value of ∑i=1Nf(i)\sum_{i=1}^N f(i).

Input

The first line contains the number of test cases TT. (1≤T≤100001 \leq T \leq 10000)

Each of the next TT lines contains a test case, given as three positive integers NN, AA, and BB separated by spaces. (1≤N,A,B≤5000001 \leq N, A, B \leq 500000)

Output

For each test case, print the answer on its own line. The answer does not exceed the range of a 64-bit integer.

Examples1

  1. Example 1

    Input
    3
    2 5 5
    12 4 7
    10000 500000 500000
    
    Expected output
    15
    244
    84131000000