学长口中恶意到裸题,我就只能呵呵了。主要运用一次运用扩展欧几里德算法,列举学霸教我的一个例子(6x+12y = 6的解为x =1,y = 0;所以6x1+12y1=12的解为x1/2 = x,y2/2 = y);除此之外还要在impossible的时候注意判定。
#include<iostream> using namespace std; void ex_gcd(long long a,long long b,long long& d,long long& x,long long& y) { if(b==0){d=a;x=1;y=0;} else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);} } int main() { long long x,y,m,n,L,s =0,k =0; cin >> x>> y>> m>> n>> L; long long mn = m-n; long long xy = x-y; long long yx; if(m>n) { xy = (y-x+L)%L; yx = y-x; } else { xy = (x-y+L)%L; mn = n-m; yx= x-y; } ex_gcd(mn,-L,xy,s,k); if((x-y)%xy !=0) { cout<< "Impossible" << endl; } else { long long t = s*(yx)/xy; t = t%L; if(t < 0) t+=L; cout<< t << endl; } return 0; }转载于:https://www.cnblogs.com/DUANZ/p/3862355.html
相关资源:数据结构—成绩单生成器