题目链接:

  http://poj.org/problem?id=1061

题目大意:

  中文题目,题意一目了然,就是数据范围大的出奇。

解题思路:

  假设两只青蛙都跳了T次,可以列出来不定方程:p*l + (n-m)*T == x - y。列出等式以后,利用扩展欧几里德计算不定方程的解。在求出整数最小解的地方卡了好久,好久。

想具体了解扩展欧几里德的用法和证明的话,可以看一下神牛的博文,我自认弱绞尽脑汁也写不来这么好,附上链接:http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html

代码:

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
#define LL long long
LL gcd (LL a, LL b, LL &x, LL &y)//扩展欧几里德模板
{
if (b == )
{
x = ;
y = ;
return a;
}
LL r = gcd (b, a%b, x, y), t;
t = x;
x = y;
y = t - a / b * y;
return r;
}
int main ()
{
LL x, y, m, n, l;
while (scanf ("%lld %lld %lld %lld %lld", &x, &y, &m, &n, &l) != EOF)
{
LL u, v, a, b, c;
a = n - m;
b = l;
c = gcd(a, b, u, v);//求出来的u是gcd(a, b) = au + bv中的。
if ((x - y) % c != )//x*gcd(a, b) = a*u*x + b*v*x成立,才能求出题目中的解
{
printf ("Impossible\n");
continue;
}
LL s = b / c;
u = u * (x - y) / c;//这个时候才是(x-y) = (n-m)*t + p*l中t的解
u = (u % s + s) % s;
printf ("%lld\n", u);
}
return ;
}

最新文章

  1. JAVA BigDecimal 小数点处理
  2. Html5-canvas
  3. Codis使用教程
  4. redhat下升级gcc编译器
  5. sql 更新一列为行号
  6. javascript 事件设计模式
  7. GDAL python教程(1)——用OGR读写矢量数据
  8. 从PHP官网被攻击,到熟悉SSL(安全链路层)
  9. Python自动化开发-基础语法
  10. window.atob()与window.btoa()方法实现编码与解码
  11. Mongodb Mysql NoSQL的区别和联系
  12. day 3 - 2 数据类型练习
  13. 【ORIGINATE】详解
  14. Confluence 6 CSS 编辑技巧
  15. linux下安装nginx及初步认识
  16. 教你用Windows自带工具给优盘/移动硬盘添加密码
  17. MySQL服务安全加固
  18. [转]微擎人人商城m()函数调用model方法
  19. RuntimeError - [Xcodeproj] Unknown object version.解决方法
  20. fit_transform和transform的区别

热门文章

  1. ArcEngine影像图配准
  2. lemon oa前端页面——由user-base-list谈项目组织
  3. 浅谈python中的“ ==” 与“ is”、还有cmp
  4. 将Python打印的内容进行高亮的输出
  5. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
  6. LoadRunner系列之—-04 录制基于https协议的脚本
  7. 【spring+websocket的使用】
  8. Linux下通过find命令进行rm文件删除的小技巧
  9. 【HDOJ 3652】B-number
  10. js的几种循环语句