zoukankan      html  css  js  c++  java
  • 青蛙的约会(扩展欧几里德)

    青蛙的约会
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 122424   Accepted: 25986

    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

    AC代码

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    void gcd ( __int64 a , __int64 b , __int64 &d , __int64 &x , __int64 &y )
    {
        
        if ( ! b ) 
            d = a , x = 1 , y = 0 ;
        else 
            gcd ( b , a%b , d , y , x ) , y -= x * ( a / b ) ; 
    }
    
    int main()
    {
        __int64 s , t , m , n , l ;
        while ( ~ scanf ("%I64d%I64d%I64d%I64d%I64d" , & s , & t , & m , & n , & l ) )
        {
            __int64 a , b , d , ans ;
            __int64 x , y ;
            a = l ;
            b = m - n ;
            ans = t - s ;
            if ( b < 0 )
                b = n - m , ans = s - t ;
            gcd ( a , b , d , x , y ) ;
            if ( ans % d )//无解出现的情况
                printf("Impossible
    ") ;
            else
            {
                __int64 tmp = l / d ;        
                ans = (  ans / d * y ) % tmp ;//求出答案,因答案要求最小,故还得对答案的“周期”取余
                if ( ans < 0 )//如果出现的是负数,就要加上周期
                    ans += tmp ;
                printf ("%I64d
    ",ans);
            }
        }
        return 0;
    }
    View Code
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    android29
    android28
    android27
    android26
    Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup
    Dynamics CRM Import Solution Attribute Display Name description is null or empty
    The service cannot be activated because it does not support ASP.NET compatibility
    IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
    Unable to access the IIS metabase.You do not have sufficient privilege
    LM算法与非线性最小二乘问题
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8395431.html
Copyright © 2011-2022 走看看