C - 青蛙的约会 POJ - 1061 (扩展欧几里得)

题目链接:https://cn.vjudge.net/contest/276376#problem/C

题目大意:中文题目。

具体思路:扩展gcd,具体证明过程看图片(就这麽个题我搞了一天,,,)。  

 

AC代码:

 1 #include<iostream>
 2 #include<stack>
 3 #include<stdio.h>
 4 #include<cmath>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 # define ll long long
 9 const int maxn = 1e5+100;
10 ll xo,yo;
11 ll exgcd(ll x,ll y)
12 {
13     if(y==0)
14     {
15         xo=1;
16         yo=0;
17         return x;
18     }
19     ll gcd=exgcd(y,x%y);
20     ll tmp=yo;
21      yo=xo-x/y*yo;
22     xo=tmp;
23     return gcd;
24 }
25 int main()
26 {
27     ll x,y,n,m,L;
28     scanf("%lld %lld %lld %lld %lld",&x,&y,&m,&n,&L);
29     ll ans=exgcd(n-m,L);
30     if((x-y)%ans)
31         printf("Impossible\n");
32     else
33     {
34         L/=ans;
35         printf("%lld\n",(((x-y)*xo/ans)%L+L)%L);
36     }
37     return 0;
38 }

猜你喜欢

转载自www.cnblogs.com/letlifestop/p/10274241.html