hiho week 95 register

Ended

Participants:477

Verdict:Accepted
Score:100 / 100
Submitted:2016-04-23 21:56:49

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
void Exgcd(LL a,LL b,LL &x,LL &y,LL &d){
    if(!b) d=a,x=1,y=0;else Exgcd(b,a%b,y,x,d),y-=a/b*x;}
inline LL in(LL x=0,char ch=getchar(),int v=1){
    while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();if(ch=='-') v=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x;}
LL s1,s2,v1,v2,m,ans,a,b,x,y,d;
int main(){
    ios::sync_with_stdio(false);
    s1=in(),s2=in(),v1=in(),v2=in(),m=in();
    s1%=m,s2%=m,v1%=m,v2%=m;
    if(s1==s2&&v2-v1!=0) return puts("-1"),0;
    a=v1-v2,b=s2-s1;
    if(a<0){ a=-a,a%=m,b=-b;if(b>0) b%=m;else if(b<0) b=b%m+m; }
    else{ a%=m;if(b>0) b%=m;else if(b<0) b=b%m+m; }
    Exgcd(m,a,x,y,d);if(b%d) return puts("-1"),0;
    ans=y*(b/d);ans=(ans%(m/d)+m/d)%(m/d);
    return cout<<ans<<'\n',0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX