模线性方程

应用场景:一般是求二元一次方程的解ax+by=c像这样的,然后她的标准板子是ax-ny=b然后用扩展欧几里得去解一组xy的解(具体的数学道理我不懂,就直接上代码);

题目中一般是 a已知n已知b已知,求一组xy

 

int extend_gcd(LL a,LL n,LL &x,LL &y){
    if(n==0){
        x=1;
        y=0;
        return a;
    }
    LL r=extend_gcd(n,a%n,x,y);
    LL t=x;
    x=y;
    y=t-a/n*y;
    return r;
}//return的是a n的最大公约数

int liner_equation(int a,int b,int n){
    LL x,y;
    LL d=extend_gcd(a,n,x,y);
    if(b%d)return -1;
    LL res=x*(b%d)%n+n;
    res=res%(n/d);
    return res;
} //返回的是x0,

 

https://www.luogu.com.cn/problem/P1516

 

posted @ 2020-11-11 11:11  金龙喩  阅读(84)  评论(0)    收藏  举报