POJ2115 C Looooops 一元模线性方程

/*
    题目描述:给出程序段for(int i = A ; i!= B ; i+= C)    statement ,给出A,B,C,k,已知该程序段的运算中出现
              的数都是mod k之后的,问该程序段将被执行多少次结束,不会结束的话输出FOREVER
                
    方法:由题意列出方程有A + Cx = B (mod k),变形Cx = B - A(mod k),要求该模线性方程的最小正整数解
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-8;
LL ex_gcd(LL a , LL b , LL &x , LL &y )
{
    if(b==0){
        x = 1;      y = 0;
        return a;
    }
    LL d = ex_gcd(b , a% b , y , x);
    y = y - a/b * x;
    return d;
}
LL linearmod(LL a , LL b , LL c)   //求解ax=b (mod c)的最小正整数解
{
    LL x , y;
    LL d = ex_gcd(a , c , x , y);
    if(b%d)
        return -1;
    x = b / d * x ;
    LL m = c / d;
    return (x % m + m)% m;
}
int main()
{
    LL a , b , c ;
    int k;
    while(scanf("%lld%lld%lld%d",&a , &b , &c , &k)==4 ){
        if(!a && !b && !c && !k)        break;
        LL real_mod = 1LL << k;
        LL ans = linearmod(c ,b - a , real_mod);
        if(ans == -1)
            printf("FOREVER\n");
        else
            printf("%lld\n" , ans );
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值