【poj1061-青蛙的约会】拓展欧几里得-不定方程

http://poj.org/problem?id=1061

题意:裸题。注意负数。

//poj1061
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

typedef long long LL;
LL tx,ty;

LL exgcd(LL a,LL b)
{
    if(b==0) {tx=1,ty=0;return a;}
    LL d=exgcd(b,a%b);
    LL x=ty,y=tx-(a/b)*ty;
    tx=x;ty=y;
    return d;
}

int main()
{
    // freopen("a.in","r",stdin);
    // freopen("a.out","w",stdout);
    LL x,y,m,n,l;
    scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l);
    LL g=exgcd(m-n,l);
    // printf("g = %I64d
",g);
    if((y-x)%g!=0) printf("Impossible
");
    else {
        LL rx=tx*(y-x)/g;
        LL r=l/g;
        if(r<0) r*=-1;
        rx=(rx%r+r)%r;
        printf("%I64d
",rx);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/KonjakJuruo/p/5178505.html