题目地址:POJ 1061

扩展GCD好难懂。。

看了半天。最终把证明什么的都看明确了。

。推荐一篇博客吧(戳这里),讲的真心不错。。

直接上代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
LL X, Y;
LL exgcd(LL a, LL b)
{
if(b==0)
{
X=1;
Y=0;
return a;
}
LL r=exgcd(b,a%b);
LL t=X;
X=Y;
Y=t-a/b*Y;
return r;
}
int main()
{
LL x, y, m, n, l, L, d;
while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l)!=EOF)
{
if(m>n)
{
d=exgcd(m-n,l);
L=y-x;
}
else
{
d=exgcd(n-m,l);
L=x-y;
}
//printf("%I64d %I64d\n", X, Y);
if(m==n||L%d)
{
printf("Impossible\n");
continue ;
}
LL ans=X*L/d;
LL s=l/d;
//printf("%I64d %I64d %I64d\n",X, ans,s);
if(ans<=0)
ans=ans%s+s;
else
ans=ans%s;
printf("%I64d\n",ans);
}
return 0;
}

POJ 1061 青蛙的约会(扩展GCD求模线性方程)的更多相关文章

  1. poj 1061 青蛙的约会(扩展gcd)

    题目链接 题意:两只青蛙从数轴正方向跑,给出各自所在位置, 和数轴长度,和各自一次跳跃的步数,问最少多少步能相遇. 分析:(x+m*t) - (y+n*t) = p * L;(t是跳的次数,L是a青蛙 ...

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

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

  3. POJ 1061 青蛙的约会 扩展欧几里德--解不定方程

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 81606   Accepted: 14116 Descripti ...

  4. poj 1061 青蛙的约会 扩展欧几里德

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K       Description 两 只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们 ...

  5. POJ - 1061 青蛙的约会 扩展欧几里得 + (贝祖公式)最小正整数解

    题意: 青蛙 A 和 青蛙 B ,在同一纬度按照相同方向跳跃相同步数,A的起点为X ,每一步距离为m,B的起点为Y,每一步距离为 n,一圈的长度为L,求最小跳跃步数. 思路: 一开始按照追击问题来写, ...

  6. Poj 1061 青蛙的约会(扩展欧几里得解线性同余式)

    一.Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  7. (Relax 数论1.6)POJ 1061 青蛙的约会(扩展的欧几里得公式)

    /* * POJ_1061.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...

  8. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

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

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

随机推荐

  1. 微信小程序:this code is a mock one

    问题 微信小程序调用wx.login() 的 success 函数带的code 提示this code is a mock one 解决方法 appid和微信小程序开发工具所登陆用户管理的小程序清单不 ...

  2. C++11程序设计要点总结-模板机制详解

    C++程序设计要点总结 在编程的过程中呢我们总会遇到一些各种各样的问题,就比如在写方法的时候,我们一个同样的方法要写好几种类型的呢,这让我们很伤脑筋,但是呢C++有一个强大的功能就是模板机制,这个模板 ...

  3. How far away?

    C - How far away ? HDU - 2586 There are n houses in the village and some bidirectional roads connect ...

  4. Linux硬盘的检测--smartctl详细介绍

    概述  随着硬盘容量.速度的快速发展,硬盘的可靠性问题越来越重要,今天的单块硬盘存储容量可轻松达到1TB,硬盘损坏带来的影响非常巨大.不同的文件系统(xfs,reiserfs,ext3)都有自己的检测 ...

  5. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  6. LeetCode(25)Reverse Nodes in k-Group

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  7. 如何用scanf读入一个string

    #include <stdio.h> #include <string> using namespace std; int main() { string a; a.resiz ...

  8. Haproxy配置文件详解

    #/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -st `cat /var/run/haproxy.pid` ################ ...

  9. 命令行可以执行python脚本,jenkins里执行报错:cannot find Chrome binary

    “selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary”这个 ...

  10. Go函数学习

    package main import ( "fmt" "reflect" "runtime" "math" ) //函 ...