1. 程式人生 > >NOIP 2012 提高組 複賽 day2 mod 同餘方程

NOIP 2012 提高組 複賽 day2 mod 同餘方程

//poj1061 青蛙的約會
#include <stdio.h>

void gcd(long long a,long long b,long long &d,long long &x, long long &y){
    if(!b){
        d=a;
        x=1;
        y=0;
    }else{
        gcd(b,a%b,d,y,x);
        y-=a/b*x;
    }
}
int main(){
    long long a,b,d,x,y,b2;
    long long t1,t2,t3,t4,t5;
    scanf("%lld%lld%lld%lld%lld",&t1,&t2,&t3,&t4,&t5);
    a=t3-t4;
    b=t5*-1;
    gcd(a,b,d,x,y);
    if((t2-t1)%d!=0){
        printf("Impossible\n");
    }else{
        b2=b/d;
        x=((t2-t1)/d*x%b2+b2)%b2;
        printf("%lld\n",x);
    }
    return 0;
}