区间(分块)
Farmer FJ有一个长度为n的牛棚a,每个牛棚有对应的值a[i],下标从1开始,对于一个正整数k,b[i]表示a[i]*a[i+1]*……*a[i+k-1]再%p,我们需要输出所有b[i]的异或和。
输入:n,k,p,A,B,C,D a[1]=A a[i]=(a[i-1]*B+C)%D;
1<=n<=2*10^7, k,p,A,B,C,D<=10^9,k不一定为质数。
输入样例
1000 97 96998351
41 1668 505 2333
41 1668 505 2333
输出样例
1749769
正解:我们把序列分块,每个块的大小为k,最后一个块大小任意,对于每一个块维护块中元素到块头和块尾的经过的元素的乘积,对于一个询问,要么是整个块的乘积,要么是跨越块的前缀乘积和后缀乘积,O(n)预处理和O(1)处理询问,而且时间还开了2s,不虚。
CODE:
#include<iostream> #include<algorithm> #include<fstream> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; int n,k,ed; long long p,A,B,C,D; int a[20001000],lans[20001000],rans[20001000];//lans[i]表示i到块尾的乘积,rans[i]表示i到块头的成绩 int main() { freopen("2261.in","r",stdin); freopen("2261.out","w",stdout); scanf("%d%d%lld",&n,&k,&p); scanf("%lld%lld%lld%lld",&A,&B,&C,&D); a[1] = A; for (register int i=2; i<=n; i++) a[i] = (1ll*a[i-1]*B%D+C)%D; for (register int i=k; i<=n; i+=k) { lans[i] = a[i]%p; for (register int j=i-1; j>=i-k+1; j--) lans[j] = (1ll*lans[j+1]*a[j])%p; rans[i-k+1] = a[i-k+1]%p; for (register int j=i-k+2; j<=i; j++) rans[j] = (1ll*rans[j-1]*a[j])%p; ed = i; } ed = ed+1; lans[n] = a[n]%p; for (int i=n-1; i>=ed; i--) lans[i] = (1ll*lans[i+1]*a[i])%p; rans[ed] = a[ed]%p; for (int i=ed+1; i<=n; i++) rans[i] = (1ll*rans[i-1]*a[i])%p; long long ans = 0; for (register int i=1; i<=n-k+1; i++) { if (i%k==1) ans = ans^lans[i]; else ans = ans^(((long long)lans[i]*rans[i+k-1])%p); } printf("%lld\n",ans); return 0; }