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

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。 
    我们把这两只青蛙分别叫做青蛙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

    就是一个exgcd板题 关键在于推公式
    exgcd就是用特解求全部解 找出一个特殊情况就好了
    注意答案为负数的情况

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 10010, INF = 0x7fffffff;
    
    LL gcd(LL a, LL b)
    {
        return b == 0 ? a : gcd(b, a % b);
    }
    
    LL exgcd(LL a, LL b, LL& d, LL& x, LL& y)
    {
        if(!b)
        {
            d = a;
            x = 1;
            y = 0;
        }
        else
        {
            exgcd(b, a % b, d, y, x);
            y -= x * (a / b);
        }
    }
    
    int main()
    {
        LL a, b, d, x, y;
        LL _x, _y, m, n, l;
        cin >> _x >> _y >> m >> n >> l;
        if((_x - _y) % gcd(l, n - m)) return puts("Impossible");
        exgcd(n - m, l, d, x, y);
        x *= (_x - _y) / d;
        x = (x % l + l) % l;
        cout << x << endl;
    
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    [转载]各种计算机语言的经典书籍
    [转载]VC 常用快捷键
    [转载]Visual Studio中的debug和release版本的区别
    [转载]Visual C++开发工具与调试技巧整理
    [转载]一个游戏程序员的学习资料
    [转载]C++资源之不完全导引(完整版)
    [转载]一个图形爱好者的书架/白话说学计算机图形学
    [摘录]这几本游戏编程书籍你看过吗?
    Oracle分析函数的使用
    [C/C++]C++下基本类型所占位数和取值范围
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10281418.html
Copyright © 2011-2022 走看看