zoukankan      html  css  js  c++  java
  • POJ 1061 青蛙的约会

    题目链接http://poj.org/problem?id=1061

    扩展欧几里得+线性同余方程

    从题意中很容易得到等式x+mt = y+nt(mod L)//t代表时间
    移动左右得到 (m-n)t = y-x(mod L);
    所以 得到(m-n)*a - L*b = y-x的扩展欧几里得,求解a,套上模版就OK了。

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 __int64 x,y;
     4 __int64 ext_eucld(__int64 a,__int64 b)
     5 {
     6     __int64 t,d;
     7     if(b == 0)
     8     {
     9         x = 1;
    10         y = 0;
    11         return a;
    12     }
    13     d = ext_eucld(b,a%b);
    14     t = x;x = y;y = t-(a/b)*y;
    15     return d;
    16 }
    17 int main()
    18 {
    19     __int64 m,n,l,a,b,d,ans;
    20     scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&m,&n,&l);
    21     b = ((b-a)%l+l)%l;//注意把b-a,m-n变为正数
    22     m = ((m-n)%l+l)%l;
    23     d = ext_eucld(m,l);
    24     if(b%d)
    25     {
    26         printf("Impossible\n");
    27     }
    28     else
    29     {
    30     l /= d;
    31     ans = (x*(b/d)%l+l)%l;
    32     printf("%I64d\n",ans);
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    Python学习4
    Python学习3
    Python学习2
    表空间
    sqlplus常用设置
    HashMap和LinkedHashMap
    堆栈源码
    观察者模式
    策略模式
    java线性表
  • 原文地址:https://www.cnblogs.com/timeship/p/2622280.html
Copyright © 2011-2022 走看看