标签:
扩展欧几里得算法。可以转化成ax+by=c的最小正整数解。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define clr(x,c) memset(x,c,sizeof(x)) #define ll long long ll read(){ ll x=0;char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) x=x*10+c-‘0‘,c=getchar(); return x; } ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; } void exgcd(ll a,ll b,ll &x,ll &y){ if(!b){ x=1,y=0;return ; } exgcd(b,a%b,x,y); ll temp=x;x=y;y=temp-a/b*y; } int main(){ ll x=read(),y=read(),m=read(),n=read(),l=read(); ll a=n-m,b=l,c=x-y; ll Gcd=gcd(a,b); if(c%Gcd) { printf("Impossible\n");return 0; } a/=Gcd,b/=Gcd,c/=Gcd; exgcd(a,b,x,y); x=((c*x)%b+b)%b; printf("%lld\n",x); return 0; }
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5724123.html