bzoj1477: 青蛙的约会(exgcd)

1477: 青蛙的约会

题目:传送门

题解:

   一眼题,追及问题exgcd

   青蛙Rose在b点,一次跳a米。。。

   青蛙hanks_o在d点,一次跳c米

   那么YY:ax+b=cx+d (mod p) 

   化简:(a-c)x-p*y=d-b 

 

水代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 typedef long long LL;
 8 LL exgcd(LL a,LL b,LL &x,LL &y)
 9 {
10     if(a==0)
11     {
12         x=0;y=1;
13         return b;
14     }
15     else
16     {
17         LL tx,ty;
18         LL d=exgcd(b%a,a,tx,ty);
19         x=ty-(b/a)*tx;
20         y=tx;
21         return d;
22     }
23 }
24 int main()
25 {
26     LL A,B,C,D,L;
27     scanf("%lld%lld%lld%lld%lld",&C,&D,&A,&B,&L);
28     LL a=A-B,b=-L,sum=(D-C),x,y;
29     LL d=exgcd(a,b,x,y);
30     if(sum%d!=0)
31     {    
32         printf("Impossible\n");
33         return 0;
34     }
35     x=((x*(sum/d))%(b/d)+(b/d))%(b/d);
36     printf("%lld\n",x);
37     return 0;
38 }

 

转载于:https://www.cnblogs.com/CHerish_OI/p/8425775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值