CF2056F2.Xor of Median (Hard Version)

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. The difference between the versions is that in this version, the constraints on tt, kk, and mm are higher. You can hack only if you solved all versions of this problem.

A sequence aa of nn integers is called good if the following condition holds:

  • Let cntx\text{cnt}_x be the number of occurrences of xx in sequence aa. For all pairs 0≤i<j<m0 \le i \lt j \lt m, at least one of the following has to be true: cnti=0\text{cnt}_i = 0, cntj=0\text{cnt}_j = 0, or cnti≤cntj\text{cnt}_i \le \text{cnt}_j. In other words, if both ii and jj are present in sequence aa, then the number of occurrences of ii in aa is less than or equal to the number of occurrences of jj in aa.

You are given integers nn and mm. Calculate the value of the bitwise XOR of the median∗^{\text{∗}} of all good sequences aa of length nn with 0≤ai<m0\le a_i \lt m.

Note that the value of nn can be very large, so you are given its binary representation instead.

∗^{\text{∗}}The median of a sequence aa of length nn is defined as the ⌊n+12⌋\left\lfloor\frac{n + 1}{2}\right\rfloor-th smallest value in the sequence.

这是该问题的困难版本。两个版本的区别在于,本版本中对 tt、kk 和 mm 的约束更强。仅当您已解决该问题的所有版本时,才可进行 hack。

一个由 nn 个整数组成的序列 aa 被称为好序列,当且仅当满足以下条件:

  • 设 cntx\text{cnt}_x 表示 xx 在序列 aa 中出现的次数。对于所有满足 0≤i<j<m0 \le i < j < m 的数对 (i,j)(i, j),下列条件中至少有一个成立:cnti=0\text{cnt}_i = 0、cntj=0\text{cnt}_j = 0 或 cnti≤cntj\text{cnt}_i \le \text{cnt}_j。换言之,若 ii 和 jj 均在序列 aa 中出现,则 ii 在 aa 中的出现次数不超过 jj 在 aa 中的出现次数。

给定整数 nn 和 mm,请计算所有长度为 nn、且满足 0≤ai<m0 \le a_i < m 的好序列 aa 的中位数∗^{\text{∗}} 的异或和(bitwise XOR)。

注意:nn 的值可能非常大,因此您将获得其二进制表示形式而非十进制数值。

∗^{\text{∗}} 序列 aa(长度为 nn)的中位数定义为该序列中第 ⌊n+12⌋\left\lfloor\frac{n + 1}{2}\right\rfloor 小的值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers kk and mm (1≤k≤2⋅1051 \le k \le 2 \cdot 10^5, 1≤m≤1091 \le m \le 10^9) — the number of bits in nn and the upper bound on the elements in sequence aa.

The second line of each test case contains a binary string of length kk — the binary representation of nn with no leading zeros.

It is guaranteed that the sum of kk over all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 kk 和 mm(1≤k≤2⋅1051 \le k \le 2 \cdot 10^5,1≤m≤1091 \le m \le 10^9)—— 分别表示 nn 的二进制位数以及序列 aa 中元素的上界。

每个测试用例的第二行包含一个长度为 kk 的二进制字符串 —— 即 nn 的二进制表示,不含前导零。

保证所有测试用例的 kk 值之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output a single integer representing the bitwise XOR of the median of all good sequences aa of length nn where 0≤ai<m0\le a_i \lt m.

对于每个测试用例,输出一个整数,表示所有满足条件的序列 aa(长度为 nn,且对所有 ii 有 0≤ai<m0\le a_i \lt m)的中位数的按位异或(XOR)结果。

输入输出样例

  • 输入#1

    6
    2 3
    10
    2 3
    11
    5 1
    11101
    7 9
    1101011
    17 34
    11001010001010010
    1 1000000000
    1

    输出#1

    3
    2
    0
    8
    32
    0

说明/提示

In the first example, n=102=2n = 10_2 = 2 and m=3m = 3. All possible sequences with elements less than mm are: [0,0][0, 0], [0,1][0, 1], [0,2][0, 2], [1,0][1, 0], [1,1][1, 1], [1,2][1, 2], [2,0][2, 0], [2,1][2, 1], [2,2][2, 2]. All of them are good, so the answer is: 0⊕0⊕0⊕0⊕1⊕1⊕0⊕1⊕2=30 \oplus 0 \oplus 0 \oplus 0 \oplus 1 \oplus 1 \oplus 0 \oplus 1 \oplus 2 = 3.

In the second example, n=112=3n = 11_2 = 3 and m=3m = 3. Some good sequences are [2,2,2][2, 2, 2], [1,0,1][1, 0, 1], and [2,0,1][2, 0, 1]. However, a sequence [2,0,0][2, 0, 0] is not good, because cnt0=2\text{cnt}_0 = 2, cnt2=1\text{cnt}_2 = 1. Therefore, if we set i=0i = 0 and j=2j = 2, i<ji \lt j holds, but cnti≤cntj\text{cnt}_i \le \text{cnt}_j does not.

在第一个例子中,n=102=2n = 10_2 = 2,m=3m = 3。所有元素均小于 mm 的可能序列有:[0,0][0, 0]、[0,1][0, 1]、[0,2][0, 2]、[1,0][1, 0]、[1,1][1, 1]、[1,2][1, 2]、[2,0][2, 0]、[2,1][2, 1]、[2,2][2, 2]。它们全都是“好”的序列,因此答案为:0⊕0⊕0⊕0⊕1⊕1⊕0⊕1⊕2=30 \oplus 0 \oplus 0 \oplus 0 \oplus 1 \oplus 1 \oplus 0 \oplus 1 \oplus 2 = 3。

在第二个例子中,n=112=3n = 11_2 = 3,m=3m = 3。一些“好”的序列包括 [2,2,2][2, 2, 2]、[1,0,1][1, 0, 1] 和 [2,0,1][2, 0, 1]。然而,序列 [2,0,0][2, 0, 0] 并非“好”的序列,因为 cnt0=2\text{cnt}_0 = 2,cnt2=1\text{cnt}_2 = 1。因此,若取 i=0i = 0 且 j=2j = 2,则 i<ji \lt j 成立,但 cnti≤cntj\text{cnt}_i \le \text{cnt}_j 不成立。

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

首页