• p1516&poj1061&bzoj1477 青蛙的约会


    传送门(洛谷)

    题目

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。 我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。

    Input

    输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。

    Output

    输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"

    Sample Input

    1 2 3 4 5

    Sample Output

    4
    分析
    根据题目我们可以得到式子
         am+x≡an+y(mod L)
    我们移一下可以得到
         a(m-n)≡y-x(mod L)
    所以我们可以得到
         a(m-n)+bL=y-x
    我们可以将数轴的长度L分为数段,每一段的长度gcd(m-n,L)为单位一,所以我们将公式变为
         a(m-n)+bL=gcd(m-n,L)
    这里我们将gcd(m-n,L)当作1来算,套入exgcd模板即可
    在计算完这种情况下的a之后我们进行一个判断:
    如果gcd(m-n,L)|(y-x)则将等式两边均乘(gcd(m-n,L)÷(y-x)),所以答案即为
         a*gcd(m-n,L)÷(y-x)%(L÷gcd(m-n,L))。
    否则无解
    代码
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cctype>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #include<ctime>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    using namespace std;
    long long a,b;
    inline long long gcd(long long x,long long y){
        if(y>x)swap(x,y);
        if(x%y==0)return y;
        return gcd(y,x%y);
    }
    inline void exgcd(long long x,long long y,long long &a,long long &b){
        if(y==0){
            a=1;
            b=0;
            return;
        }
        exgcd(y,x%y,a,b);
        long long z=a;
        a=b;
        b=z-(x/y)*b;
        return;
    }
    int main(){
        long long n,m,i,j,k,l,x,y;
        cin>>x>>y>>n>>m>>l;
        if(n>m){
            swap(x,y);
            swap(n,m);
        }
        k=gcd(m-n,l);
        exgcd(m-n,l,a,b);
        if((x-y)%k)puts("Impossible");
          else cout<<((x-y)/k*a%(l/k)+(l/k))%(l/k)<<endl;
        return 0;
    }
  • 相关阅读:
    URI、URL、URN区别
    http历史
    http基础
    那些年我踩过的electron+react的坑!!!
    【Error】System limit for number of file watchers reached
    zsh: corrupt history file /home/floodlight/.zsh_history
    electron中持久化保存数据的解决方案electron-store
    坑:pytest 运行报错unknown hook 'pytest_namespace' in plugin <module 'allure.pytest_plugin'
    坑:找到LoadRunner中Recording Options和 Run Time Settings配置选项确实的原因
    Loadrunner基本概念解析<一>
  • 原文地址:https://www.cnblogs.com/yzxverygood/p/9152742.html
Copyright © 2020-2023  润新知