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

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

    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
    题解:线性方程列出来:(n-m)x+y*L=x-y;
    因为次数是正整数;所以转换为最小整数解;
    代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<vector>
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 typedef long long LL;
    11 void e_gcd(LL a,LL b,LL &d,LL &x,LL &y){
    12     if(!b){
    13         d=a;
    14         x=1;
    15         y=0;
    16     }
    17     else{
    18         e_gcd(b,a%b,d,x,y);
    19         LL temp=x;
    20         x=y;
    21         y=temp-a/b*y;
    22     }
    23 }
    24 LL cal(LL a,LL b,LL c){
    25     LL d,x,y;
    26     e_gcd(a,b,d,x,y);
    27     if(c%d!=0)return -1;
    28     x*=c/d;
    29     if(b<0)b=-b;
    30     b/=d;
    31     LL ans=x%b;
    32     if(ans<=0)ans+=b;
    33     return ans;
    34 }
    35 int main(){
    36     LL x,y,m,n,L;
    37     while(~scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)){
    38         LL ans=cal(n-m,L,x-y);
    39         if(ans==-1)puts("Impossible");
    40         else printf("%lld
    ",ans);
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4908496.html
Copyright © 2011-2022 走看看