5131: [CodePlus2017年12月]可做题2 矩阵乘法 exgcd

题解:

现在看挺水的啊……我当初还不会做呢……就找一下规律,矩阵乘法一下,然后用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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值