[BZOJ1477]青蛙的约会(扩欧)

题目描述

传送门

题解

扩欧裸题;
不过还是推了一下想了一会儿= =看来基本的算法还是要熟练啊= =

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long

LL x,y,m,n,L,a,b,c;

inline LL gcd(LL a,LL b){
    if (!b) return a;
    else return gcd(b,a%b);
}

inline void exgcd(LL a,LL b,LL &x,LL &y){
    if (!b) x=1,y=0;
    else exgcd(b,a%b,y,x),y-=a/b*x;
}

int main(){
    scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L);
    a=n-m; b=L; c=x-y;
    int t=gcd(a,b);
    if (c%t){
        printf("Impossible\n");
        return 0;
    }
    a/=t; b/=t; c/=t;
    x=y=0;
    exgcd(a,b,x,y);
    x=((c*x)%b+b)%b;
    if (!x) x+=b;
    printf("%lld\n",x);
}

你可能感兴趣的:(数论,bzoj)