每一组数据输出一行,满足条件的连续子序列数量。示例1
2 3 2 5 7 8 7 8 7 3 2 5 7 8 9 8 7
1 2
%运算是可以满足结合律的
移项就变成了差分运算
只要存在 (a[i] - a[i+1]) % k == -(b[i] - b[i+1) % k 就行了
在a的差分数组里kmp b的差分数组
//-------------------------代码---------------------------- //#define int ll const int N = 3e6+10,base = 1e9+10; int n,m,k; int a[N],b[N]; int ca[N],cb[N]; int ha[N],hb[N]; int nxt[N]; void solve() { // cin>>n>>m; cin>>n>>m>>k; fo(i,1,n) a[i] = b[i] = nxt[i] = 0; fo(i,1,n) cin>>a[i],a[i] %= k; fo(i,1,m) cin>>b[i],b[i] %= k; fo(i,1,n-1) a[i] = (a[i+1] - a[i] + k) % k; fo(i,1,m-1) b[i] = (b[i] - b[i+1] + k) % k; n -- ,m -- ; fo(i,2,m) { nxt[i] = nxt[i-1]; while(nxt[i] && b[nxt[i] + 1] != b[i]) nxt[i] = nxt[nxt[i]]; nxt[i] += (b[nxt[i] + 1] == b[i]); } fo(i,1,m) { // cout<<nxt[i]<<' '; } // cout<<endl; int j = 1,cnt = 0; fo(i,1,n) { while(j != 1 && b[j] != a[i]) j = nxt[j-1] + 1; if(a[i] == b[j]) j ++ ; if(j == m + 1) { cnt ++ ; j = nxt[j-1] + 1; } } cout<<cnt<<endl; } void main_init() {} signed main(){ AC();clapping();TLE; cout<<fixed<<setprecision(12); main_init(); // while(cin>>n,n) // while(cin>>n>>m,n,m) int t;cin>>t;while(t -- ) solve(); // {solve(); } return 0; } /*样例区 */ //------------------------------------------------------------