zoukankan      html  css  js  c++  java
  • 【BZOJ1477】青蛙的约会

    Description

    两个物体在一个周长为l的环上同方向运动, A初始时位置为a,速度为m,B初始位置为b,速度为n,求何时相遇。

    Solution

    根据题意,我们不难列出方程,推导如下

    $a+mt equiv {b+nt} pmod l$

    $(m-n)t equiv {b-a} pmod l$

    $(m-n)t+kl=b-a$

    最终我们得到了一个线性模方程,可以用Exgcd求解。

    根据裴蜀定理,该方程有整数解,当且仅当(b-a)%t=0,因此我们可以判断问题是否有解。

    最终Exgcd求出的答案可能为负数,我们将它加上若干l即可

    Code

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 ll exgcd(ll a, ll b, ll &x, ll &y) {
     5     if (!b) {
     6         x = 1; 
     7         y = 0;
     8         return a;
     9     }
    10     ll gcd = exgcd(b, a % b, x, y);
    11     ll x2 = x;
    12     ll y2 = y;
    13     x = y2;
    14     y = x2 - (a / b) * y2;
    15     return gcd;
    16 }
    17 inline ll read() {
    18     ll ret = 0, op = 1;
    19     char c = getchar();
    20     while (!isdigit(c)) {
    21         if (c == '-') op = -1; 
    22         c = getchar();
    23     }
    24     while (isdigit(c)) {
    25         ret = ret * 10 + c - '0';
    26         c = getchar();
    27     }
    28     return ret * op;
    29 }
    30 int main() {
    31     ll a, b, m, n, l;
    32     a = read(); b = read(); m = read(); n = read(); l = read();
    33     ll aa = m - n;
    34     ll bb = l;
    35     ll retx, rety;
    36     ll ret = exgcd(aa, bb, retx, rety);
    37     if ((b - a) % ret != 0) puts("Impossible"); 
    38     else {
    39         ll ans = (b - a) / ret * retx;
    40         while (ans < 0) ans += l;
    41         printf("%lld
    ", ans % l);
    42     }
    43     return 0;
    44 }
    AC Code
  • 相关阅读:
    multiprocessing总结
    CPython在CPU密集型应用下的并发
    多线程交互
    线程等待与守护线程
    Python多线程(1)
    一个简单的单线程异步服务器
    多线程与多进程的实现
    socket的功能分割到不同函数
    数据处理项目Postmortem
    M2 终审
  • 原文地址:https://www.cnblogs.com/shl-blog/p/11232122.html
Copyright © 2011-2022 走看看