zoukankan      html  css  js  c++  java
  • POJ

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 94857   Accepted: 17597

    Description

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

    Source

    /**
              题意:两只青蛙 A的初始位置是x,每次跳n;B的初始位置是y,每次跳m,总长度为l;
              做法:设两只青蛙跳了t后相遇,A的为位置为 x + t * n ;B 的位置是 y + t*m
              并且 x + t * n - y + t*m = k * l;  可得(n - m) * t + k * l = y - x
              可以得到    扩展欧几里得 a * x + b* y = GCD(a,b); 求出x0,y0;
              其中a = n-m;b = l , x = x0 * (y-x ) / GCD(a,b) ,y = y0 * ( y - x)/GCD(a,b) 
              在扩展欧几里的中a/gcd(a,b) 为整数,b/gcd(a,b) 也是整数,所以在判断如果(y-x) % gcd(a,b) 
              不是整数,则无解;
              
    **/
    #include <iostream>
    #include <cmath>
    #include <string.h>
    #include <stdio.h>
    using namespace std;
    void swap(long long &x, long long &y)
    {
        long long  t;
        t = x;
        x = y;
        y = t;
    }
    int gcd(long long a,long long b)
    {
        if(b == 0) return a;
        return gcd(b,a%b);
    }
    
    void  extend_gcd(long long a,long long b,long long d,long long &x,long long &y)
    {
        if(a == 0 && b == 0) return;
        if(b == 0)
        {
            d = a;
            x = 1;
            y = 0;
            return;
        }
        extend_gcd(b,a%b,d,y,x);
        y -= a/b *x;
    }
    long long solve(long long a,long long b,long long n)
    {
        long long tmp,tt,x1,y1;
        tmp = gcd(a,b);
        if(n%tmp) return -1;
        extend_gcd(a,b,tmp,x1,y1);
        tt = (n*x1 /tmp) %(b/tmp);
        if(tt <0) tt += (b/tmp);
        return tt;
    }
    int main()
    {
        long long x,y,n,m,l;
        while(~scanf("%lld %lld %lld %lld %lld",&x,&y,&n,&m,&l))
        {
            long long a,b,c;
            if(n < m)
            {
                swap(n,m);
                swap(x,y);
            }
            a = n-m;
            c = y - x;
            if(c < l) c += l;
            long long ans = solve(a,l,c);
            if(ans == -1)  printf("Impossible
    ");
            else
                printf("%lld
    ", ans);
        }
    }
  • 相关阅读:
    78. Subsets
    93. Restore IP Addresses
    71. Simplify Path
    82. Remove Duplicates from Sorted List II
    95. Unique Binary Search Trees II
    96. Unique Binary Search Trees
    312. Burst Balloons
    程序员社交平台
    APP Store开发指南
    iOS框架搭建(MVC,自定义TabBar)--微博搭建为例
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4474713.html
Copyright © 2011-2022 走看看