码迷,mamicode.com
首页 > 其他好文 > 详细

math1061

时间:2014-10-09 21:34:47      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   io   os   sp   on   amp   new   bs   as   

公青蛙一开始在x位置,母青蛙在y位置。公青蛙每次跳m米,母青蛙每次跳n米,并且都是向右跳的。地球经线长度是L,然后地球是圆的,也就是说,跳到LL+1L+2……其实就是跳到012。 公青蛙想追母青蛙,问多少次后它们能跳到一起。如果它们永远不能相遇,就输出Impossible

 

 

等价于(n-m)*k + L*s = x-y咯。这就是ax + by = c求整数x的模型。

 

# include <stdio.h>

__int64 gcd(__int64 a,__int64 b)

{

       if(b==0)

              return a;

       return gcd(b,a%b);

}

void exgcd(__int64 a,__int64 b,__int64 &m,__int64 &n)//M,N,分别表示x,y

{

       if(b==0)

       {

              m=1;

              n=0;

              return ;

       }

       exgcd(b,a%b,m,n);

       __int64 t;

       t=m;

       m=n;

       n=t-a/b*n;

}

int main()

{

       __int64 x,y,m,n,l,a,b,c,k1,k2,r,t;

       while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l)!=EOF)

       {

              a=n-m;

              b=l;

              c=x-y;

              r=gcd(a,b);

              if(c%r)

              {

                     printf("Impossible\n");

                     continue;

              }

              exgcd(a,b,k1,k2);

//扩展欧几里得求得ax+by=gcd(a,b)的x,y值,存入k1,k2中

  k1=(c*k1/r)%(b/r);

////////////////////////////////////////////////////////////////////////////

K1在[0,b/r-1]上有唯一解

////////////////////////////////////////////////////////////////////////////

 

              if(k1<0)

                      k1+=b/r;

              printf("%I64d\n",k1);

       }

       return 0;

}

 

 

 

 

 

 

 

 

math1061

标签:style   io   os   sp   on   amp   new   bs   as   

原文地址:http://www.cnblogs.com/notlate/p/4014249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!