#abc283h. Ex - Popcount Sum

Ex - Popcount Sum

Score : 600600 points

问题描述

计算在11NN(包括两端)之间所有整数的按位计数总和,条件是这些整数除以MM的余数等于RR

这里,一个正整数XX的按位计数(popcount)是指其二进制表示中11的数量,即满足2k2^k位为11的非负整数kk的数量。

对于每次输入,请处理TT组测试用例。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Find the sum of popcounts of all integers between 11 and NN, inclusive, such that the remainder when divided by MM equals RR.
Here, the popcount of a positive integer XX is the number of 11s in the binary notation of XX, that is, the number of non-negative integers kk such that the 2k2^ks place is 11.
For each input, process TT test cases.

Constraints

  • 1T1051 \leq T \leq 10^5
  • 1MN1091 \leq M \leq N \leq 10^9
  • 0R<M0 \leq R < M
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format. The first line is as follows:

TT

TT test cases follow. Each of them is given in the following format:

NN MM RR

Output

Print TT lines. The ii-th line should contain the answer to the ii-th test case.

Sample Input 1

2
12 5 1
6 1 0

Sample Output 1

6
9

In the 11-st test case, the popcount of 11 is 11, the popcount of 66 is 22, and the popcount of 1111 is 33, so 1+2+3=61+2+3=6 should be printed.
In the 22-nd test case, the popcount of 11 is 11, 22 is 11, 33 is 22, 44 is 11, 55 is 22, and 66 is 22, so 1+1+2+1+2+2=91+1+2+1+2+2=9 should be printed.

update @ 2024/3/10 11:53:19