poj 1061 数论 扩展欧几里得算法

题目还算简单,但是得用long long

#include<cstdio>
#define size_num 51000
#include<cstring>
#include<iostream>
using namespace std;
void exgcd(long long a,long long b,long long &d,long long &x,long long& y)
{
	if(!b){d=a;x=1;y=0;}
	else
	{
		exgcd(b,a%b,d,x,y);
		int temp=x;
		x=y;
		y=temp-x*(a/b);
	}
}
int deal()
{
	long long x,y,m,n,L;
	cin>>x>>y>>m>>n>>L;
	long long ad,ax,ay;
	exgcd(n-m,L,ad,ax,ay);
	long long ans=(ax*(x-y))/ad;
	long long bb=L/ad;
	ans=(ans%bb+bb)%bb;
	if((y-x)%ad!=0)
		cout<<"Impossible\n";
	else
		cout<<ans<<endl;
	return 0;
}
int main()
{
	while(deal());
    return 0;
}





你可能感兴趣的:(poj1061,扩展欧几里得算法)