POJ 2115 C Looooops 同余方程 长整型常量要加LL

  1. 长整型常量一定要加LL!!例如:1LL<< k;
  2. 同余方程用扩展欧几里得求解。
  3. 注意边界值特殊情况的考虑。

题目链接:http://acm.hust.edu.cn/vjudge/problem/21464

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
using namespace std;

#define ms(s) memset(s,0,sizeof(s))
typedef unsigned long long ULL;
typedef long long LL;

const int INF = 0x3fffffff;

LL e_gcd(LL a, LL b, LL& x, LL& y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    LL ans = e_gcd(b, a%b, y, x);
    y -= x*(a/b);

    return ans;
}

int main(){
//    freopen("F:\\input.txt","r",stdin);
//    freopen("F:\\output.txt","w",stdout);
//    ios::sync_with_stdio(false);

    LL a,b,c,k,m,r;
    LL x,y;

    while((~scanf("%lld%lld%lld%lld",&a,&b,&c,&k)) && k){
        m = 1LL<<k;
        LL gcdd = e_gcd(c,m,x,y);
        if((b-a)%gcdd != 0 || (c==0 && a!=b)){
            printf("FOREVER\n");
        }
        else{
            x = x*((b-a)/gcdd)%m;
            x = (x%(m/gcdd)+m/gcdd)%(m/gcdd);
            printf("%lld\n",x);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值