POJ 1061 青蛙的约会

题目链接:http://poj.org/problem?id=1061


题意:我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。


思路:x + k * m  =  y + k * n  + t  * L  =>  k * ( n - m ) + t * L = x - y , n-m 为A,L为B,那么式子就是Ax + By = C的形式,直接用扩展欧几里得。由于范围很大,为了防止越界,我们先将所有系数的公约数除掉,也就是变成ax+by = c/gcd(a,b)的形式。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod %100000007

LL x,y,m,n,L;


LL exgcd(LL a,LL b,LL &x,LL &y)  //返回最大公约数,x,y存的是一组解
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int r=exgcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-a/b*y;
    return r;
}
LL GCD(LL x,LL y)
{
    if ( y == 0 ) return x;
    return GCD(y,x%y);
}
bool slove(LL x,LL y , LL m , LL n , LL L)
{
    LL A = n - m;
    LL B = L;
    if ( A < 0 ) A+=L;
    LL c = x - y;
    LL gcd = GCD(A,B);
    if ( c % gcd ) return false;
    A/=gcd;
    B/=gcd;
    c/=gcd;
    LL xx,yy;
    exgcd( A , B , xx , yy );
    xx = xx * c % B;
    xx = (xx % B + B)%B;
    cout<<xx<<endl;
    return true;
}

int main()
{
    cin>>x>>y>>m>>n>>L;
    if ( !slove(x,y,m,n,L) )
        puts("Impossible");
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值