CF1734F.Zeros and Ones

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Let SS be the Thue-Morse sequence. In other words, SS is the 00-indexed binary string with infinite length that can be constructed as follows:

  • Initially, let SS be "0".

  • Then, we perform the following operation infinitely many times: concatenate SS with a copy of itself with flipped bits.

    For example, here are the first four iterations:

    Iteration

    SS before iteration

    SS before iteration with flipped bits

    Concatenated SS

    1

    0

    1

    01

    2

    01

    10

    0110

    3

    0110

    1001

    01101001

    4

    01101001

    10010110

    0110100110010110

    …\ldots

    …\ldots

    …\ldots

    …\ldots

You are given two positive integers nn and mm. Find the number of positions where the strings S0S1…Sm−1S_0 S_1 \ldots S_{m-1} and SnSn+1…Sn+m−1S_n S_{n + 1} \ldots S_{n + m - 1} are different.

设 SS 为Thue-Morse 序列。换言之,SS 是一个从索引 00 开始的无限长二进制字符串,其构造方式如下:

  • 初始时,令 SS 为 "0"。

  • 然后,无限次执行以下操作:将 SS 与其各位取反(即 0 变 1,1 变 0)后的副本拼接。

    例如,前四次迭代过程如下:

    迭代次数

    迭代前的 SS

    迭代前的 SS(各位取反)

    拼接后的新 SS

    1

    0

    1

    01

    2

    01

    10

    0110

    3

    0110

    1001

    01101001

    4

    01101001

    10010110

    0110100110010110

    …\ldots

    …\ldots

    …\ldots

    …\ldots

给定两个正整数 nn 和 mm。求字符串 S0S1…Sm−1S_0 S_1 \ldots S_{m-1} 与 SnSn+1…Sn+m−1S_n S_{n + 1} \ldots S_{n + m - 1} 在多少个位置上字符不同。

输入格式

Each test contains multiple test cases. The first line of the input contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases. The description of the test cases follows.

The first and only line of each test case contains two positive integers, nn and mm respectively (1≤n,m≤10181 \leq n,m \leq 10^{18}).

每个测试包含多个测试用例。输入的第一行包含一个整数 tt(1≤t≤1001 \le t \le 100),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例仅有一行,包含两个正整数 nn 和 mm(1≤n,m≤10181 \leq n,m \leq 10^{18})。

输出格式

For each testcase, output a non-negative integer — the Hamming distance between the two required strings.

对于每个测试用例,输出一个非负整数——即两个指定字符串之间的汉明距离。

输入输出样例

  • 输入#1

    6
    1 1
    5 10
    34 211
    73 34
    19124639 56348772
    12073412269 96221437021

    输出#1

    1
    6
    95
    20
    28208137
    48102976088

说明/提示

The string SS is equal to 0110100110010110....

In the first test case, S0S_0 is "0", and S1S_1 is "1". The Hamming distance between the two strings is 11.

In the second test case, S0S1…S9S_0 S_1 \ldots S_9 is "0110100110", and S5S6…S14S_5 S_6 \ldots S_{14} is "0011001011". The Hamming distance between the two strings is 66.

字符串 SS 等于 0110100110010110……

在第一个测试用例中,S0S_0 为 "0",S1S_1 为 "1"。这两个字符串之间的汉明距离为 11。

在第二个测试用例中,S0S1…S9S_0 S_1 \ldots S_9 为 "0110100110",而 S5S6…S14S_5 S_6 \ldots S_{14} 为 "0011001011"。这两个字符串之间的汉明距离为 66。

输入解题思路,AI测评打分。不知道怎么写?

首页