zoukankan      html  css  js  c++  java
  • POJ 1061 青蛙的约会 | 同余方程和exGcd

    题解:

    要求s+px=t+qx (mod L)

    移项 (p-q)x=t-s (mod L)

    等价于 (p-q)x+Ly=t-s

    即ax+by=c的方程最小非负根

    exGcd后乘个C

     1 #include<cstdio>
     2 typedef long long ll;
     3 using namespace std;
     4 ll s,t,p,q,L;
     5 ll gcd (ll x,ll y)
     6 {
     7     return y==0?x:gcd(y,x%y);
     8 }
     9 ll exGcd(ll a,ll b,ll &x,ll &y)
    10 {
    11     if (b==0) return x=1,y=0,a;
    12     ll r=exGcd(b,a%b,y,x);
    13     y-=(a/b)*x;
    14     return  r;
    15 }
    16 int main()
    17 {
    18     scanf("%lld%lld%lld%lld%lld",&s,&t,&p,&q,&L);
    19     ll a=(p-q+L)%L,b=L,c=(t-s+L)%L,x,y,G=gcd(a,b);
    20     if (c%G!=0)
    21     {
    22     puts("Impossible");
    23     return 0;
    24     }
    25     a/=G,b/=G,c/=G;
    26     exGcd(a,b,x,y);
    27     x=(x%b+b)%b;
    28     x=x*c%b;
    29     printf("%lld
    ",x);
    30     return 0;
    31 }
  • 相关阅读:
    python023
    python019
    python018
    python017
    python016
    python013
    python012
    数据库,盘它!!!!
    网络编程
    面向对象简述
  • 原文地址:https://www.cnblogs.com/mrsheep/p/7898952.html
Copyright © 2011-2022 走看看