hihoCoder扩展欧几里得

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <math.h>
#include <queue>
#include <stack>
using namespace std;
#define INF 0x3f3f3f
#define pi acos(-1.0)
#define LL long long

LL exgcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    LL ans=exgcd(b,a%b,x,y);
    LL temp=x;
    x=y;
    y=temp-a/b*y;
    return ans;
}
int main()
{
    LL v1,v2;
    LL m;
    LL s1,s2;
    LL a,b,x,y,v,c;
    scanf("%lld%lld%lld%lld%lld",&s1,&s2,&v1,&v2,&m);

    c=s2-s1;
    a=v1-v2;//-1
    b=m;    //6
    if(a<0)
        a+=m;

    LL d;
    d=exgcd(a,b,x,y);
    if(c%d||s1==s2)
    {
        printf("-1\n");
        return 0;
    }
    a=a/d;
    b=b/d;
    c=c/d;
    //printf("%lld\n",c);
    x=x*c%b;
    while(x<0)
        x+=b;
    printf("%lld\n",x);
    return 0;
}
posted @ 2016-07-03 16:03  see_you_later  阅读(105)  评论(0编辑  收藏  举报