poj 1061 青蛙约会(扩展欧几里德)
2024-09-02 20:39:59
题目链接:
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 ;
}
最新文章
- JAVA BigDecimal 小数点处理
- Html5-canvas
- Codis使用教程
- redhat下升级gcc编译器
- sql 更新一列为行号
- javascript 事件设计模式
- GDAL python教程(1)——用OGR读写矢量数据
- 从PHP官网被攻击,到熟悉SSL(安全链路层)
- Python自动化开发-基础语法
- window.atob()与window.btoa()方法实现编码与解码
- Mongodb Mysql NoSQL的区别和联系
- day 3 - 2 数据类型练习
- 【ORIGINATE】详解
- Confluence 6 CSS 编辑技巧
- linux下安装nginx及初步认识
- 教你用Windows自带工具给优盘/移动硬盘添加密码
- MySQL服务安全加固
- [转]微擎人人商城m()函数调用model方法
- RuntimeError - [Xcodeproj] Unknown object version.解决方法
- fit_transform和transform的区别
热门文章
- ArcEngine影像图配准
- lemon oa前端页面——由user-base-list谈项目组织
- 浅谈python中的“ ==” 与“ is”、还有cmp
- 将Python打印的内容进行高亮的输出
- wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
- LoadRunner系列之—-04 录制基于https协议的脚本
- 【spring+websocket的使用】
- Linux下通过find命令进行rm文件删除的小技巧
- 【HDOJ 3652】B-number
- js的几种循环语句