问题 L: Misaka Mikoto's Dynamic KMP Problem

内存限制:64 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:2 通过:1

题目描述

链接:https://ac.nowcoder.com/acm/contest/57361/L
来源:牛客网
Misaka Mikoto has a pattern string sss. Let ∣s∣|s|s be the length of ssssis_isi be the iii-th character of sss, and s[l∼r]s[l\sim r]s[lr] be the substring from the lll-th character to the rrr-th character, that is, slsl+1⋯srs_ls_{l+1}\cdots s_{r}slsl+1sr.
In this problem, all characters are represented by an integer.
You need to perform mmm operations. There are two types of operations:
  • 1 x c1\ x\ c1 x cchange sxs_xsx to ccc.
  • 2 t2\ t2 t: given a text string ttt, find the number of times sss occurs as a substring in ttt and the longest border of sss.
The longest border of sss means the largest number r(1≤r<∣s∣)r(1\le r<|s|)r(1r<s) that s[1∼r]s[1\sim r]s[1r] is the same as s[(∣s∣−r+1)∼∣s∣]s[(|s|-r+1)\sim|s|]s[(sr+1)s]. If such rrr doesn't exist, let the longest border of sss be 000.
Let's call the operation 222 "query".
Given two integers b,pb,pb,p, let xi,yix_i,y_ixi,yi be the answer to the first and the second question of the iii-th query, you only need to output (∑ xi×yi×bi)modp\left(\sum\ x_i \times y_i \times b^i\right) \bmod p( xi×yi×bi)modp.
For some reason, the operations are encrypted. Let zzz be the value of xi×yix_i \times y_ixi×yi of the last query (if there aren't any queries before, let zzz be 000). For operation 111, you are given two integers x′,c′x',c'x,c, which means x=(x′⊕z)mod∣s∣+1x=(x'\oplus z)\bmod |s|+1x=(xz)mods+1 and c=c′⊕zc=c'\oplus zc=cz. For operation 222, you are given an integer nnn and nnn integers t1′∼tn′t'_1\sim t'_nt1tn, which means ∣t∣=n|t|=nt=n and ti=ti′⊕zt_i=t'_i\oplus zti=tiz.

输入格式

The first line contains four integers ∣s∣,m,b,p|s|,m,b,ps,m,b,p.
The second line contains ∣s∣|s|s integers, which represent the string sss.
The iii-th line of the following mmm lines contains the iii-th operation: 1 x′ c′1\ x'\ c'1 x c or 2 n t1′ t2′ ⋯ tn′2\ n\ t'_1\ t'_2\ \cdots\ t'_n2 n t1 t2  tn.

输出格式

One integer that represents the answer.

输入样例 复制

3 3 10 1000000007
1 2 1
2 5 1 2 1 2 1
1 3 3
2 4 3 3 3 3

输出样例 复制

420

数据范围与提示

For all test cases,
1≤m,∣s∣,n≤1061\le m,|s|,n\le10^61m,s,n106.
∑n≤2×106\small\sum\normalsize n\le2\times10^6n2×106, where ∑n\small\sum\normalsize nn represents the sum of nnn of all queries.
1≤b,p,si,c,ti≤231−11\le b,p,s_i,c,t_i\le2^{31}-11b,p,si,c,ti2311.
0≤x′,c′,ti′≤10120\le x',c',t'_i\le10^{12}0x,c,ti1012.