标签:namespace main exgcd color nsx pac clu lse class
求 (n-m)*tx+l*ty=y-x,tx最小的解,用exgcd就ok了。
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll exgcd(ll a,ll b,ll &x,ll &y){ if(b==0){ x=1;y=0; return a; } ll ret=exgcd(b,a%b,x,y); ll temp=x; x=y;y=temp-(a/b)*y; return ret; } int main(){ ll x,y,n,m,l,ansx,ansy; scanf("%lld%lld%lld%lld%lld",&x,&y,&n,&m,&l); ll t=exgcd(m-n,l,ansx,ansy); ll temp=x-y;l=abs(l/t); if(temp%t!=0)printf("Impossible"); else printf("%lld",(((temp/t)*ansx)%l+l)%l); return 0; }
标签:namespace main exgcd color nsx pac clu lse class
原文地址:https://www.cnblogs.com/Dream-Runner/p/9446433.html