题解:
现在看挺水的啊……我当初还不会做呢……就找一下规律,矩阵乘法一下,然后用exgcd求一下同余方程的解的个数就好了,但还是有几个地方要注意的:1、方程的负号可以直接忽略。2、判断无解。3、要注意%,比如 a1 a 1 ,上来就要%一下。
代码:
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x*f;
}
LL a1,l,r,k,p,m;
struct Matrix{LL v[3][3];}M,st;
Matrix operator *(Matrix a,Matrix b)
{
Matrix c;
c.v[1][1]=c.v[1][2]=c.v[2][1]=c.v[2][2]=0;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
for(int k=1;k<=2;k++)
c.v[i][j]=(c.v[i][j]+a.v[k][j]*b.v[i][k]%p)%p;
return c;
}
Matrix Pow(Matrix x,LL y)
{
if(y==1)return x;
Matrix t=Pow(x,y>>1),re=t*t;
if(y&1)re=re*x;return re;
}
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!a){x=0,y=1;return b;}
LL tx,ty,d=exgcd(b%a,a,tx,ty);
x=ty-(b/a)*tx;y=tx;return d;
}
int main()
{
M.v[1][1]=0,M.v[1][2]=M.v[2][1]=M.v[2][2]=1;
int T=read();
while(T--)
{
a1=read(),l=read(),r=read(),k=read(),p=read(),m=read();a1%=p;
st.v[1][1]=st.v[2][1]=1,st.v[1][2]=st.v[2][2]=0;
if(k!=3)st=st*Pow(M,k-3);
LL ak=(st.v[2][1]*(l%p)%p+st.v[1][1]*a1%p)%p;
LL ans=0;m=(m-ak+p)%p;
LL x,y;LL d=exgcd(st.v[2][1],p,x,y);
if(m%d){puts("0");continue;}
x=(x*(m/d)%(p/d)+(p/d))%(p/d);
if(x<=r-l)ans=((r-l-x)/(p/d)+1);
printf("%lld\n",ans);
}
}