bzoj1477 青蛙的约会 数论

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1477

题解:

由题得:

l + m*x\equiv r+n*x(mod L)

变形后:

(m-n)*x+L*y\equiv r-l(mod L)

令:

A=(m-n+L)%L,B=L求扩展欧几里得就行了答案就是x的最小整数解。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long 
using namespace std; 
ll l,r,m,n,L,x,y,A,B,k,d; 
inline ll exgcd(ll a,ll b,ll &x,ll &y){
	if(a==0){x=0;y=1;return b;}
	ll d,tx,ty;
	d=exgcd(b%a,a,tx,ty);
	x=ty-(b/a)*tx;
	y=tx; 
	return d;
}
int main(){
//	freopen("bzoj1477.in","r",stdin);
	scanf("%lld%lld%lld%lld%lld",&l,&r,&m,&n,&L);
	A=(m-n+L)%L;B=L; k=(r-l+L)%L;
	d=exgcd(A,B,x,y);
	if(k%d) printf("Impossible\n");
	else {
		x=((x*k/d)%(B/d)+(B/d))%(B/d);
		printf("%lld\n",x);
	} 
	return 0;
}

 

 

posted @ 2018-12-01 20:53  Exception2017  阅读(118)  评论(0编辑  收藏  举报