POJ 1061
题意:
两只青蛙在同一条纬度上,它们各自朝西跳,问它们要跳多少步才能碰面(必须同时到达同一点).
分析:
假设它们跳了t步才相遇,青蛙a初始坐标为x,青蛙b初始坐标为y,则跳了t步相遇后a的坐标为 x+m*t-p1*l, b的坐标为 y+n*t-p2*l (p1,p2分别表示a,b跳
的圈数) x+m*t-p1*l = y+n*t-p2*l => x-y = (n-m)*t + (p1-p2)*l => x-y = (n-m)*t + p*l.
代码如下:
//方法一 #include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <iterator>
#include <vector> using namespace std; #define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXN 10000010
#define MAXM 1000010 LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
if(b == )
{
x = ;
y = ;
return a;
}
else
{
LL r = extend_gcd(b, a%b, y, x);
y -= x*(a/b);
return r;
}
} int main()
{
LL x, y, m, n, l;
while(scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l)==)
{
LL t, p;
LL ans = extend_gcd(n-m, l, t, p);
if((x-y)%ans)
printf("Impossible\n");
else
{
t = t*(x-y)/ans;
t = (t%(l/ans)+(l/ans))%(l/ans);
printf("%lld\n", t);
}
} return ;
}
//方法二 #include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <iterator>
#include <vector> using namespace std; #define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXN 10000010
#define MAXM 1000010 LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
if(b == )
{
x = ;
y = ;
return a;
}
else
{
LL r = extend_gcd(b, a%b, y, x);
y -= x*(a/b);
return r;
}
} LL cal(LL a, LL b, LL c)
{
LL x, y;
LL ans = extend_gcd(a, b, x, y);
if(c%ans)
return -;
x *= c/ans;
b /= ans;
if(b < )
b = b*(-);
// b = abs(b); //abs只能对int取绝对值
ans = x%b;
if(ans <= )
ans += b;
return ans;
} int main()
{
LL x, y, m, n, l;
while(scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l)==)
{
LL sum = cal(n-m, l, x-y);
if(sum == -)
printf("Impossible\n");
else
printf("%lld\n", sum);
} return ;
}
POJ 1061的更多相关文章
- poj 1061 青蛙的约会 拓展欧几里得模板
// poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...
- ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德
POJ 1061 青蛙的约会 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu Descr ...
- 扩展欧几里德 POJ 1061
欧几里德的是来求最大公约数的,扩展欧几里德,基于欧几里德实现了一种扩展,是用来在已知a, b求解一组x,y使得ax+by = Gcd(a, b) =d(解一定存在,根据数论中的相关定理,证明是用裴蜀定 ...
- 数学#扩展欧几里德 POJ 1061&2115&2891
寒假做的题了,先贴那时写的代码. POJ 1061 #include<iostream> #include<cstdio> typedef long long LL; usin ...
- POJ.1061 青蛙的约会 (拓展欧几里得)
POJ.1061 青蛙的约会 (拓展欧几里得) 题意分析 我们设两只小青蛙每只都跳了X次,由于他们相遇,可以得出他们同余,则有: 代码总览 #include <iostream> #inc ...
- poj 1061 青蛙的约会 (扩展欧几里得模板)
青蛙的约会 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status ...
- AC日记——青蛙的约会 poj 1061
青蛙的约会 POJ - 1061 思路: 扩展欧几里得: 设青蛙们要跳k步,我们可以得出式子 m*k+a≡n*k+b(mod l) 式子变形得到 m*k+a-n*k-b=t*l (m-n)*k-t ...
- POJ 1061 青蛙的约会(扩展GCD求模线性方程)
题目地址:POJ 1061 扩展GCD好难懂.. 看了半天.最终把证明什么的都看明确了. .推荐一篇博客吧(戳这里),讲的真心不错.. 直接上代码: #include <iostream> ...
- 数论问题(1) : poj 1061
最近,本人发现了一个新网站poj(不算新) 当然了,上面的资源很好...... 就是还没搞清楚它的搜索该怎么弄,如果有大佬能教教我怎么弄,请在下方留言 闲话少说,回归我们的正题 题目转自poj 106 ...
- Poj 1061 青蛙的约会(扩展GCD)
题目链接:http://poj.org/problem?id=1061 解题报告:两只青蛙在地球的同一条纬度线上,选取一个点位坐标轴原点,所以现在他们都在同一个首尾相连的坐标轴上,那么他们现在的位置分 ...
随机推荐
- spring 配置 redis
1.maven相关pom.xml <dependencies> <!--spring redis--> <dependency> <groupId>or ...
- django部署
部署教程 阿里云django部署教程 注意备份Apache2的siteconf文件 细节 环境迁移 如果是修改了注意些requirement文件
- (转)Linux NFS服务器的安装与配置
转自:http://www.cnblogs.com/mchina/archive/2013/01/03/2840040.html 由于在6系列里面,portmap已经改名了 ,6系列需要使用 serv ...
- 在后台启动受管服务器经常报错:Server may already be running
报错如下: 1. Unable to obtain lock on /usr/local/odrive/odrive_chen/Middleware/user_projects/domains/oim ...
- 【转】 C++模板详解
C++模板 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成员或者成员函数的参数.返回值取得任意类型. 模板是一种对类型进行参数化的工具: 通常有 ...
- IO流中SequenceInputStream类
SequenceInputStream类: 不断的读取InputStream流对象,对于使用Enumeration对象的情况,该类将持续读取所有InputStream对象中的内容,直到到达最后一个In ...
- Rdseed与SAC的安装
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- 利用反射调用方法时,处理ref,out参数需要注意的问题(转)
转自:http://www.68idc.cn/help/buildlang/ask/20150318283817.html 项目中如下的泛型方法,因为要在运行时,动态指定类型参数,所以要利用反射来实现 ...
- 分享一些关于PHP时间函数的常用时间
<?php // 各种时间函数 echo "现在:".date("Y-m-d H:i:s")."<br>"; echo & ...
- parseInt方法——将字符串解析为int值
parseXxx()方法 语法:static int parseInt(String s) 返回字符串的十进制整数形式 static int parseInt(String s, int rad ...