poj1061 青蛙的约会(扩展欧几里得)
2016-04-05 21:26
369 查看
题目链接:http://poj.org/problem?id=1061
题目大意:中文题,不解释
方法:设t为A青蛙和B青蛙的跳的次数,k为绕地球绕的圈数
则得出公式:(x+m*t)- (y+n*t)= k*L
化简得:(x-y)+(m-n)*t = k*L
化简:(m-n)*t - k*L = y-x(由扩展欧几里得定理可知:存在x0,y0使得a*x0+b*y0=gcd(a,b))
最终t = (x0+k(L/d))
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
LL r,t;
if(b==0)
{
x=1, y=0;
return a;
}
r=exgcd(b, a%b, x, y);
t=x;
x=y;
y=t-a/b*y;
return r;
}
int main()
{
LL n, L, m, x, y, t;
LL xx, yy;
cin>>x>>y>>m>>n>>L;
LL d = exgcd(n-m, L, xx, yy);
if((x-y) % d != 0)
cout<<"Impossible"<<endl;
else
cout<<((xx*(x-y)/d)%(L/d)+(L/d))%(L/d)<<endl;
return 0;
}
题目大意:中文题,不解释
方法:设t为A青蛙和B青蛙的跳的次数,k为绕地球绕的圈数
则得出公式:(x+m*t)- (y+n*t)= k*L
化简得:(x-y)+(m-n)*t = k*L
化简:(m-n)*t - k*L = y-x(由扩展欧几里得定理可知:存在x0,y0使得a*x0+b*y0=gcd(a,b))
最终t = (x0+k(L/d))
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
LL r,t;
if(b==0)
{
x=1, y=0;
return a;
}
r=exgcd(b, a%b, x, y);
t=x;
x=y;
y=t-a/b*y;
return r;
}
int main()
{
LL n, L, m, x, y, t;
LL xx, yy;
cin>>x>>y>>m>>n>>L;
LL d = exgcd(n-m, L, xx, yy);
if((x-y) % d != 0)
cout<<"Impossible"<<endl;
else
cout<<((xx*(x-y)/d)%(L/d)+(L/d))%(L/d)<<endl;
return 0;
}
相关文章推荐
- 鼠标设置感兴趣区域(ROI)
- php之文件载入include, include_once,require,require_once
- 【codevs 2981】题解
- LeetCode #002 Add Two Numbers
- JavaScript 防篡改对象
- POJ 2429 GCD & LCM Inverse(Pollard_rho法质因数分解)
- Jenkins插件之AnsiColor
- JavaMelody 系统监控软件
- Elasticsearch——Search的基本介绍
- Executing Raw SQL Queries using Entity Framework
- Libiconv裁剪
- 自定义 View 基础和原理
- P2P系统、一致性哈希、DHT
- eclipse怎么设置字体大小
- ArrayList和LinkedList区别及使用场景
- Navicat for Oracle Cannot load OCI DLL,126
- 关于Repository模式
- 开始学习算法
- 彻底卸载oracle数据库
- jquery eval解析JSON