单变元模线性方程 入门指南

已知a、b、n,求x,使得ax≡b(mod n)。

令 d=gcd(a,n),先使用扩展欧几里得求ax+ny=d的解。如果b不能整除d则无解,否则mod n意义下的解有d个,可以通过对某个解不断地加n/d得到。


POJ 2115 C Looooops

对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束。

若在有限次内结束,则输出循环次数。

否则输出死循环。


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<LL> VL;
LL extend_gcd(LL a,LL b,LL &x,LL &y){
    if (b==0){
        x=1;y=0;
        return a;
    }
    else{
        LL r=extend_gcd(b,a%b,y,x);
        y=y-x*(a/b);
        return r;
    }
}
LL line_mod_equation(LL a,LL b,LL n){
    LL x,y;
    LL d=extend_gcd(a,n,x,y);
    if (b%d==0){
        x%=n;
        x+=n;
        x%=n;
        return x*(b/d)%(n/d);
    }
    else return -1;
}

int main()
{
    int a,b,c,k;
    while (~scanf("%d%d%d%d",&a,&b,&c,&k)){
        if (a==0&&b==0&&c==0&&k==0) break;
        LL m=1LL<<k;
        LL ans=line_mod_equation(c,(b-a+m)%m,m);
        if (ans==-1) printf("FOREVER\n");
        else printf("%I64d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值