码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1061 青蛙的约会 扩展欧几里得

时间:2016-04-21 13:23:11      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

模板题,这题有一点需要注意,因为要求非负,ax=b(mod L) 得保证 a>=0

技术分享
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=5e4+1;
LL exgcd(LL a,LL b,LL& x,LL& y){
  if(!b){
    x=1;
    y=0;
    return a;
  }
  LL d=exgcd(b,a%b,y,x);
  y-=a/b*x;
  return d;
}
int main(){
  LL x,y,m,n,l;
  scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l);
  LL a=(m-n),b=y-x;
  if(a<0)a=-a,b=-b;
  a%=l,b%=l;
  LL d=exgcd(a,l,x,y);
  if(b%d)printf("Impossible\n");
  else{
    x*=b/d;
    LL tmp=l/d;
    x=(x%tmp+tmp)%tmp;
    printf("%I64d\n",x);
  } 
  return 0;
}
View Code

 

POJ1061 青蛙的约会 扩展欧几里得

标签:

原文地址:http://www.cnblogs.com/shuguangzw/p/5416267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!