CF1707F.Bugaboo

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

A transformation of an array of positive integers a1,a2,…,ana_1,a_2,\dots,a_n is defined by replacing aa with the array b1,b2,…,bnb_1,b_2,\dots,b_n given by bi=ai⊕a(i mod n)+1b_i=a_i\oplus a_{(i\bmod n)+1}, where ⊕\oplus denotes the bitwise XOR operation.

You are given integers nn, tt, and ww. We consider an array c1,c2,…,cnc_1,c_2,\dots,c_n (0≤ci≤2w−10 \le c_i \le 2^w-1) to be bugaboo if and only if there exists an array a1,a2,…,ana_1,a_2,\dots,a_n such that after transforming aa for tt times, aa becomes cc.

For example, when n=6n=6, t=2t=2, w=2w=2, then the array [3,2,1,0,2,2][3,2,1,0,2,2] is bugaboo because it can be given by transforming the array [2,3,1,1,0,1][2,3,1,1,0,1] for 22 times:

\[2,3,1,1,0,1\]\\to \[2\\oplus 3,3\\oplus 1,1\\oplus 1,1\\oplus 0,0\\oplus 1,1\\oplus 2\]=\[1,2,0,1,1,3\]; \\\\ \[1,2,0,1,1,3\]\\to \[1\\oplus 2,2\\oplus 0,0\\oplus 1,1\\oplus 1,1\\oplus 3,3\\oplus 1\]=\[3,2,1,0,2,2\].

And the array [4,4,4,4,0,0][4,4,4,4,0,0] is not bugaboo because 4>22−14 \gt 2^2 - 1. The array [2,3,3,3,3,3][2,3,3,3,3,3] is also not bugaboo because it can't be given by transforming one array for 22 times.

You are given an array cc with some positions lost (only mm positions are known at first and the remaining positions are lost). And there are qq modifications, where each modification is changing a position of cc. A modification can possibly change whether the position is lost or known, and it can possibly redefine a position that is already given.

You need to calculate how many possible arrays cc (with arbitrary elements on the lost positions) are bugaboos after each modification. Output the ii-th answer modulo pip_i (pip_i is a given array consisting of qq elements).

对一个正整数数组 a1,a2,…,ana_1,a_2,\dots,a_n 的一种变换定义为:将其替换为数组 b1,b2,…,bnb_1,b_2,\dots,b_n,其中 bi=ai⊕a(i mod n)+1b_i = a_i \oplus a_{(i \bmod n) + 1},⊕\oplus 表示按位异或运算。

给定整数 nn、tt 和 ww。我们称一个数组 c1,c2,…,cnc_1,c_2,\dots,c_n(满足 0≤ci≤2w−10 \le c_i \le 2^w - 1)是 bugaboo 的,当且仅当存在某个数组 a1,a2,…,ana_1,a_2,\dots,a_n,使得对 aa 进行 tt 次上述变换后,结果恰好为 cc。

例如,当 n=6n=6、t=2t=2、w=2w=2 时,数组 [3,2,1,0,2,2][3,2,1,0,2,2] 是 bugaboo 的,因为它可通过将数组 [2,3,1,1,0,1][2,3,1,1,0,1] 变换两次得到:

[2,3,1,1,0,1]→[2⊕3,3⊕1,1⊕1,1⊕0,0⊕1,1⊕2]=[1,2,0,1,1,3];[1,2,0,1,1,3]→[1⊕2,2⊕0,0⊕1,1⊕1,1⊕3,3⊕1]=[3,2,1,0,2,2].[2,3,1,1,0,1] \to [2\oplus 3,3\oplus 1,1\oplus 1,1\oplus 0,0\oplus 1,1\oplus 2] = [1,2,0,1,1,3]; \\ [1,2,0,1,1,3] \to [1\oplus 2,2\oplus 0,0\oplus 1,1\oplus 1,1\oplus 3,3\oplus 1] = [3,2,1,0,2,2].

而数组 [4,4,4,4,0,0][4,4,4,4,0,0] 不是 bugaboo 的,因为 4>22−14 > 2^2 - 1;数组 [2,3,3,3,3,3][2,3,3,3,3,3] 也不是 bugaboo 的,因为它无法由任意一个数组经两次变换得到。

现给定一个数组 cc,其中部分位置丢失(初始仅有 mm 个位置已知,其余位置丢失)。另有 qq 次修改操作,每次修改会更改 cc 中某一个位置的值。一次修改可能改变该位置是“丢失”还是“已知”的状态,也可能重新定义一个原本已知的位置。

你需要在每次修改后,计算有多少种可能的数组 cc(丢失位置可填入任意合法值)是 bugaboo 的。输出第 ii 个答案对 pip_i 取模的结果(pip_i 是一个给定的、含 qq 个元素的数组)。

输入格式

The first line contains four integers nn, mm, tt and ww (2≤n≤1072\le n\le 10^7, 0≤m≤min⁡(n,105)0\le m\le \min(n, 10^5), 1≤t≤1091\le t\le 10^9, 1≤w≤301\le w\le 30).

The ii-th line of the following mm lines contains two integers did_i and eie_i (1≤di≤n1\le d_i\le n, 0≤ei<2w0\le e_i \lt 2^w). It means the position did_i of the array cc is given and cdi=eic_{d_i}=e_i. It is guaranteed that 1≤d1<d2<…<dm≤n1\le d_1 \lt d_2 \lt \ldots \lt d_m\le n.

The next line contains only one number qq (1≤q≤1051\le q\le 10^5) — the number of modifications.

The ii-th line of the following qq lines contains three integers fif_i, gig_i, pip_i (1≤fi≤n1\le f_i\le n, −1≤gi<2w-1\le g_i \lt 2^w, 11≤pi≤109+711\le p_i\le 10^9+7). The value gi=−1g_i=-1 means changing the position fif_i of the array cc to a lost position, otherwise it means changing the position fif_i of the array cc to a known position, and cfi=gic_{f_i}=g_i. The value pip_i means you need to output the ii-th answer modulo pip_i.

第一行包含四个整数 nn、mm、tt 和 ww(2≤n≤1072\le n\le 10^7,0≤m≤min⁡(n,105)0\le m\le \min(n, 10^5),1≤t≤1091\le t\le 10^9,1≤w≤301\le w\le 30)。

接下来的 mm 行中,第 ii 行包含两个整数 did_i 和 eie_i(1≤di≤n1\le d_i\le n,0≤ei<2w0\le e_i \lt 2^w)。这表示数组 cc 在位置 did_i 处的值已知,且 cdi=eic_{d_i}=e_i。保证满足 1≤d1<d2<…<dm≤n1\le d_1 \lt d_2 \lt \ldots \lt d_m\le n。

下一行仅包含一个整数 qq(1≤q≤1051\le q\le 10^5)—— 修改操作的次数。

接下来的 qq 行中,第 ii 行包含三个整数 fif_i、gig_i、pip_i(1≤fi≤n1\le f_i\le n,−1≤gi<2w-1\le g_i \lt 2^w,11≤pi≤109+711\le p_i\le 10^9+7)。若 gi=−1g_i=-1,表示将数组 cc 在位置 fif_i 处的值设为“未知”;否则表示将数组 cc 在位置 fif_i 处的值设为“已知”,且 cfi=gic_{f_i}=g_i。pip_i 表示需将第 ii 次查询的答案对 pip_i 取模后输出。

输出格式

The output contains qq lines, denoting your answers.

输出包含 qq 行,表示你的答案。

输入输出样例

  • 输入#1

    3 2 1 1
    1 1
    3 1
    4
    2 0 123456789
    2 1 111111111
    1 -1 987654321
    3 -1 555555555

    输出#1

    1
    0
    1
    2
  • 输入#2

    24 8 5 4
    4 4
    6 12
    8 12
    15 11
    16 7
    20 2
    21 9
    22 12
    13
    2 13 11
    3 15 12
    5 7 13
    9 3 14
    10 5 15
    11 15 16
    13 14 17
    14 1 18
    18 9 19
    19 6 20
    23 10 21
    24 8 22
    21 13 23

    输出#2

    1
    4
    9
    2
    1
    0
    1
    10
    11
    16
    16
    0
    16

说明/提示

In the first example, n=3n=3, t=1t=1, and w=1w=1. Let ?? denote a lost position of cc.

In the first query, c=[1,0,1]c=[1,0,1]. The only possible array [1,0,1][1,0,1] is bugaboo because it can be given by transforming [0,1,1][0,1,1] once. So the answer is 1 mod 123 456 789=11\bmod 123\,456\,789 = 1.

In the second query, c=[1,1,1]c=[1,1,1]. The only possible array [1,1,1][1,1,1] is not bugaboo. So the answer is 0 mod 111 111 111=00\bmod 111\,111\,111 = 0.

In the third query, c=[?,1,1]c=[?,1,1]. There are two possible arrays [1,1,1][1,1,1] and [0,1,1][0,1,1]. Only [0,1,1][0,1,1] is bugaboo because it can be given by transforming [1,1,0][1,1,0] once. So the answer is 1 mod 987 654 321=11\bmod 987\,654\,321=1.

In the fourth query, c=[?,1,?]c=[?,1,?]. There are four possible arrays. [0,1,1][0,1,1] and [1,1,0][1,1,0] are bugaboos. [1,1,0][1,1,0] can be given by performing [1,0,1][1,0,1] once. So the answer is 2 mod 555 555 555=22\bmod 555\,555\,555=2.

在第一个例子中,n=3n=3,t=1t=1,w=1w=1。令 ?? 表示 cc 中丢失的位置。

在第一次查询中,c=[1,0,1]c=[1,0,1]。唯一可能的数组 [1,0,1][1,0,1] 是“bugaboo”,因为它可通过将 [0,1,1][0,1,1] 变换一次得到。因此答案为 1 mod 123 456 789=11\bmod 123\,456\,789 = 1。

在第二次查询中,c=[1,1,1]c=[1,1,1]。唯一可能的数组 [1,1,1][1,1,1] 不是“bugaboo”。因此答案为 0 mod 111 111 111=00\bmod 111\,111\,111 = 0。

在第三次查询中,c=[?,1,1]c=[?,1,1]。存在两个可能的数组:[1,1,1][1,1,1] 和 [0,1,1][0,1,1]。其中仅 [0,1,1][0,1,1] 是“bugaboo”,因为它可通过将 [1,1,0][1,1,0] 变换一次得到。因此答案为 1 mod 987 654 321=11\bmod 987\,654\,321 = 1。

在第四次查询中,c=[?,1,?]c=[?,1,?]. 共有四个可能的数组。其中 [0,1,1][0,1,1] 和 [1,1,0][1,1,0] 是“bugaboo”。[1,1,0][1,1,0] 可通过将 [1,0,1][1,0,1] 变换一次得到。因此答案为 2 mod 555 555 555=22\bmod 555\,555\,555 = 2。

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

首页