zoukankan      html  css  js  c++  java
  • POJ

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

    分析:http://blog.csdn.net/loi_dqs/article/details/49488851

    上述题解很详细

    1、ax ≡ c (mod b),就是ax与c相差若干个b,自然可以转化为ax + by = c。

    2、令d = gcd(a, b),

    (1)如果c不整除以d,则Impossible。

    因为ax + by = c,可得a / d * x + b / d * x = c / d,左式显然为整数,所以若c不整除以d,则无解。

    (2)如果c整除以d,根据扩展欧几里得求解ax + by = gcd(a, b) = d,可得一组特解X和Y。

    即aX + bY = d,两边乘以c / d,可得a(c / d * X) + b(c / d * Y) = c。

    所以原方程ax + by = c的解x = c / d * X,y = c / d * Y。(当然这也是原方程的一组特解,原方程有好多解,如下图)

    3、本题是求最少的跳跃次数,即最小的非负x。根据上图或者如下定理,可知将求得的特解x对b/d取余即可,即(c / d * X) mod (b / d)。

    定理:若gcd(a, b) = d,则方程ax ≡ c (mod b)在[0, b/d - 1]上有唯一解。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 120 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    LL ex_gcd(LL a, LL b, LL &x, LL &y)
    {
        if(!b){
            x = 1;
            y = 0;
            return a;
        }
        LL ans = ex_gcd(b, a % b, x, y);
        LL tmp = x - a / b * y;
        x = y;
        y = tmp;
        return ans;
    }
    int main(){
        LL x, y, m, n, L;
        scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L);
        LL a, b, X, Y, c, d;
        a = m - n;
        b = L;
        c = y - x;
        if(a < 0){//余数都是大于0的,a与c对b同余
            a = -a;
            c = -c;
        }
        d = ex_gcd(a, b, X, Y);
        if(c % d != 0){
            printf("Impossible
    ");
            return 0;
        }
        LL mod = L / d;
        printf("%lld
    ", (c / d * X % mod + mod) % mod);
        return 0;
    }
    
  • 相关阅读:
    IOS AutoLayout 代码实现约束—VFL
    理解iOS Event Handling
    一些优秀的iOS第三方库
    iOS中NSNotification、delegate、KVO三者之间的区别与联系?
    laravel 框架加载自定义函数/类文件
    Nodejs 使用 socket.io 简单实现实时通信
    Redis 与 Memcache 的异同之处
    Redis 服务安装
    PHP 依赖管理神器 Composer 基本使用
    Ajax无刷新图片插件使用
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7663487.html
Copyright © 2011-2022 走看看