题解:
md这鬼畜的时限让我卡了半天常,以后hash不能偷懒写vector了。。
对于这道题,我们构造转移矩阵:
显然这个矩阵的行列式为 −y − y ,首先特判掉 y=0 y = 0 的情况,之后这个矩阵存在逆元,也就是这个数列会形成一个环而不是 ρ ρ 形(否则乘逆元会有两个值)。
然后就相当于这个矩阵乘上初始向量等于最终向量,因为最终向量的两个已经确定,直接枚举剩下那个值来做拔山盖世。
即是: AnB=T A n B = T
因为 B B 不变,我们可以反着来,把 AiSB A i S B 存进去来做就好了。
顺便一提,这个 n n 的上界是 ,这个不好证明,不过从数列的角度来说很好理解,因为出现两个相同就肯定循环了。
时间复杂度为: p2S+pS p 2 S + p S ,取 S=q‾√ S = q 最优。
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
int T,x,y,a,b,z,c,q,mod;
inline int mul(int x,int y) {return x*y%mod;}
inline int add(int x,int y) {return (x+y>=mod)?x+y-mod:x+y;}
vector <int> cir;
int per;
struct mat {
int h,w;
int a[4][4];
inline void init(int h1,int w1) {
memset(a,0,sizeof(a));
h=h1; w=w1;
}
friend inline mat operator *(const mat &a,const mat &b) {
register mat c; c.init(a.h,b.w);
for(register int i=1;i<=a.h;i++)
for(register int k=1;k<=b.w;k++)
for(register int j=1;j<=a.w;j++)
c.a[i][k]=(c.a[i][k]+a.a[i][j]*b.a[j][k])%mod;
return c;
}
friend inline bool operator ==(const mat &a,const mat b) {
for(int i=1;i<=a.h;i++)
for(int j=1;j<=a.w;j++)
if(a.a[i][j]!=b.a[i][j]) return false;
return true;
}
}A,As,ori,tar;
const int H=2333333;
const int N=4e6+50;
const int base=10007;
struct data{int a,v;};
struct hash_table {
int g[H],nt[N],v1[N],v2[N],ec;
inline void clear() {
memset(g,0,sizeof(g)); ec=0;
}
inline int hash(mat &a) {
return (a.a[1][1]*base+a.a[2][1]);
}
inline void inc(int pos,int a1,int a2) {
nt[++ec]=g[pos]; g[pos]=ec; v1[ec]=a1; v2[ec]=a2;
}
inline int find(int a,int p) {
int v=a%H, k=-1;
for(int i=g[v];i;i=nt[i])
if(v2[i]>=p && v1[i]==a)
k=(k==-1)?v2[i]:min(k,v2[i]);
if(k!=-1) return k-p;
return -1;
}
}ht;
inline LL solve(LL v) {
if(!v) return 0;
if(v==1) return a==c;
v-=2;
LL cc=(v+1)/per;
LL t=cc*cir.size();
v-=cc*per;
if(v!=-1)
t+=(upper_bound(cir.begin(),cir.end(),v)-cir.begin());
return t+(a==c);
}
inline void solve() {
cir.clear(); ht.clear();
int tt=clock();
cin>>a>>b>>x>>y>>z>>mod>>c>>q;
if(!y && !x) {
int e=(z==c),r=(a==c),t=(b==c);
for(int i=1;i<=q;i++) {
LL a,b; cin>>a>>b; a--;
LL o=(b>=2)?(e*(b-2)+r+t):r;
b=a;
if(b) o-=(b>=2)?(e*(b-2)+r+t):r;
cout<<o<<'\n';
}
return;
}
else if(!y) {
if(b==c) cir.push_back(0);
int lst=b; per=0;
do {
lst=add(mul(lst,x),z);
++per;
if(lst==c && lst!=b) cir.push_back(per);
} while(lst!=b);
} else {
const int S=100;
A.init(3,3); As.init(3,3);
for(int i=1;i<=3;i++) As.a[i][i]=1;
A.a[1][2]=1; A.a[3][3]=1;
A.a[2][1]=y; A.a[2][2]=x; A.a[2][3]=1;
for(int i=1;i<=S;i++) As=A*As;
ori.init(3,1);
ori.a[1][1]=a; ori.a[2][1]=b; ori.a[3][1]=z;
for(int i=1;;i++) {
ori=As*ori;
ht.inc(ht.hash(ori)%H,ht.hash(ori),i*S);
if(i*S>=mod*mod) break;
}
for(int t=0;t<mod;t++) {
tar.init(3,1);
tar.a[1][1]=t; tar.a[2][1]=c; tar.a[3][1]=z;
int s=mod*mod+1;
for(int i=0;i<=S;i++) {
int p=ht.find(ht.hash(tar),i);
if(p!=-1) s=min(s,p);
tar=A*tar;
}
if(s<=mod*mod) cir.push_back(s);
}
tar.init(3,1);
tar.a[1][1]=a; tar.a[2][1]=b; tar.a[3][1]=z;
per=mod*mod+1;
for(int i=0;i<S;i++) {
int p=ht.find(ht.hash(tar),i);
if(p!=-1) per=min(per,p);
tar=A*tar;
}
sort(cir.begin(),cir.end());
}
while(q--) {
LL a,b; cin>>a>>b;
cout<<solve(b)-solve(a-1)<<'\n';
}
}
int main() {
cin>>T;
while(T--) solve();
}