题意:

  两只青蛙在同一条纬度上,它们各自朝西跳,问它们要跳多少步才能碰面(必须同时到达同一点).

分析:

  假设它们跳了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的更多相关文章

  1. poj 1061 青蛙的约会 拓展欧几里得模板

    // poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...

  2. ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德

    POJ 1061 青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu  Descr ...

  3. 扩展欧几里德 POJ 1061

    欧几里德的是来求最大公约数的,扩展欧几里德,基于欧几里德实现了一种扩展,是用来在已知a, b求解一组x,y使得ax+by = Gcd(a, b) =d(解一定存在,根据数论中的相关定理,证明是用裴蜀定 ...

  4. 数学#扩展欧几里德 POJ 1061&2115&2891

    寒假做的题了,先贴那时写的代码. POJ 1061 #include<iostream> #include<cstdio> typedef long long LL; usin ...

  5. POJ.1061 青蛙的约会 (拓展欧几里得)

    POJ.1061 青蛙的约会 (拓展欧几里得) 题意分析 我们设两只小青蛙每只都跳了X次,由于他们相遇,可以得出他们同余,则有: 代码总览 #include <iostream> #inc ...

  6. poj 1061 青蛙的约会 (扩展欧几里得模板)

    青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status ...

  7. 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 ...

  8. POJ 1061 青蛙的约会(扩展GCD求模线性方程)

    题目地址:POJ 1061 扩展GCD好难懂.. 看了半天.最终把证明什么的都看明确了. .推荐一篇博客吧(戳这里),讲的真心不错.. 直接上代码: #include <iostream> ...

  9. 数论问题(1) : poj 1061

    最近,本人发现了一个新网站poj(不算新) 当然了,上面的资源很好...... 就是还没搞清楚它的搜索该怎么弄,如果有大佬能教教我怎么弄,请在下方留言 闲话少说,回归我们的正题 题目转自poj 106 ...

  10. Poj 1061 青蛙的约会(扩展GCD)

    题目链接:http://poj.org/problem?id=1061 解题报告:两只青蛙在地球的同一条纬度线上,选取一个点位坐标轴原点,所以现在他们都在同一个首尾相连的坐标轴上,那么他们现在的位置分 ...

随机推荐

  1. 《与小卡特一起学Python》 Code6 注释

    """这是一个包括多行的注释, 使用了三重引号字符串. 这不完全是注释,不过也可以相当于注释. """ #***************** ...

  2. md语法之行内代码和代码片

    md语法之行内代码和代码片 比如说要在行内写上一句或者半句代码(代码的意思就是某种脚本语言), 用撇号围起来就可以了. 比如: import pandas as pd 写代码片(单独的一块脚本语言)的 ...

  3. 使用python操作FTP上传和下载

    函数释义 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,函数列举如下 ftp登陆连接 from ftplib import F ...

  4. js去除数组重复项

    /** * js去除数组重复项 */ //方法一.使用正则法 // reg.test(str),匹配得到就返回true,匹配不到返回false var arr = ["345",& ...

  5. Linux安装时内存如何分区的相关问题

    Linux系统安装时内存如何分区:Linux系统必须的分区是根分区(/)和swap交换分区.普通用户一般分三个区,一个根分区(/),一个家目录(home分区),一个交换分区(swap分区),以80G的 ...

  6. 萝卜招聘网 http://www.it9s.com 可以发布免费下载简历求职 ,免费!免费!全部免费!找工作看过来 免费下载简历 !

    萝卜招聘网  http://www.it9s.com  可以发布免费下载简历求职 ,免费!免费!全部免费!找工作看过来 免费下载简历 !萝卜招聘网  http://www.it9s.com  可以发布 ...

  7. 最佳的 14 个免费的响应式 Web 设计测试工具

    一旦你决定要搭建一个网站就应该已经制定了设计标准.你认为下一步该做什么呢?测试!我使用“测试”这个词来检测你网站对不同屏幕和浏览器尺寸的响应情况.测试在响应式网页设计的过程中是很重要的一步.如果你明白 ...

  8. 本地测试Sql

    数据库sqlserver2008 编程vs2008,C# 该测试是数据库和程序在一台计算机上,如果不在一台计算机上就不一样了 我的数据库最大链接是127,好像不固定. 一.测试链接对速度的影响 sta ...

  9. c#数据绑定(2)——删除DataTable的数据

    文/嶽永鹏 c#数据绑定(1)中,简要的通过代码应用了DataTable,DataTableColumns,DataTableRow类,通过UI界面的Textbox向DataTable中添加数据然后响 ...

  10. 实现password框中显示文字提示的方式

    其实实际上实现中并不能让password中显示文字提示,但是我们在工作中有这样的需求,当没输入东西的时候,框内有提示输入密码,但是当输入东西的时候又显示的是*号,那么是如何实现的呢?其实原理很简单,就 ...