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 t, k, and m are higher. You can hack only if you solved all versions of this problem.
A sequence a of n integers is called good if the following condition holds:
- Let cntx be the number of occurrences of x in sequence a. For all pairs 0≤i<j<m, at least one of the following has to be true: cnti=0, cntj=0, or cnti≤cntj. In other words, if both i and j are present in sequence a, then the number of occurrences of i in a is less than or equal to the number of occurrences of j in a.
You are given integers n and m. Calculate the value of the bitwise XOR of the median∗ of all good sequences a of length n with 0≤ai<m.
Note that the value of n can be very large, so you are given its binary representation instead.
∗The median of a sequence a of length n is defined as the ⌊2n+1⌋-th smallest value in the sequence.
这是该问题的困难版本。两个版本的区别在于,本版本中对 t、k 和 m 的约束更强。仅当您已解决该问题的所有版本时,才可进行 hack。
一个由 n 个整数组成的序列 a 被称为好序列,当且仅当满足以下条件:
- 设 cntx 表示 x 在序列 a 中出现的次数。对于所有满足 0≤i<j<m 的数对 (i,j),下列条件中至少有一个成立:cnti=0、cntj=0 或 cnti≤cntj。换言之,若 i 和 j 均在序列 a 中出现,则 i 在 a 中的出现次数不超过 j 在 a 中的出现次数。
给定整数 n 和 m,请计算所有长度为 n、且满足 0≤ai<m 的好序列 a 的中位数∗ 的异或和(bitwise XOR)。
注意:n 的值可能非常大,因此您将获得其二进制表示形式而非十进制数值。
∗ 序列 a(长度为 n)的中位数定义为该序列中第 ⌊2n+1⌋ 小的值。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers k and m (1≤k≤2⋅105, 1≤m≤109) — the number of bits in n and the upper bound on the elements in sequence a.
The second line of each test case contains a binary string of length k — the binary representation of n with no leading zeros.
It is guaranteed that the sum of k over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 k 和 m(1≤k≤2⋅105,1≤m≤109)—— 分别表示 n 的二进制位数以及序列 a 中元素的上界。
每个测试用例的第二行包含一个长度为 k 的二进制字符串 —— 即 n 的二进制表示,不含前导零。
保证所有测试用例的 k 值之和不超过 2⋅105。
输出格式
For each test case, output a single integer representing the bitwise XOR of the median of all good sequences a of length n where 0≤ai<m.
对于每个测试用例,输出一个整数,表示所有满足条件的序列 a(长度为 n,且对所有 i 有 0≤ai<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=2 and m=3. All possible sequences with elements less than m are: [0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]. All of them are good, so the answer is: 0⊕0⊕0⊕0⊕1⊕1⊕0⊕1⊕2=3.
In the second example, n=112=3 and m=3. Some good sequences are [2,2,2], [1,0,1], and [2,0,1]. However, a sequence [2,0,0] is not good, because cnt0=2, cnt2=1. Therefore, if we set i=0 and j=2, i<j holds, but cnti≤cntj does not.
在第一个例子中,n=102=2,m=3。所有元素均小于 m 的可能序列有:[0,0]、[0,1]、[0,2]、[1,0]、[1,1]、[1,2]、[2,0]、[2,1]、[2,2]。它们全都是“好”的序列,因此答案为:0⊕0⊕0⊕0⊕1⊕1⊕0⊕1⊕2=3。
在第二个例子中,n=112=3,m=3。一些“好”的序列包括 [2,2,2]、[1,0,1] 和 [2,0,1]。然而,序列 [2,0,0] 并非“好”的序列,因为 cnt0=2,cnt2=1。因此,若取 i=0 且 j=2,则 i<j 成立,但 cnti≤cntj 不成立。
输入解题思路,AI测评打分。不知道怎么写?