HDU 3579 Hello Kiki(模线性方程组)

2019-04-14 18:24发布

  • 原题链接:Here!

  • 思路:典型模线性方程组的题,需要注意的是如果最后解为0,需要输出M[]的lcm。这是为什么呢?如果解为0,也就是说当 0 ≡ Ai ( mod Mi ) (1 <= i <= n),即Ai = Mi*c(c为非负整数)原来的同余方程X ≡ Ai( mod Mi )变为 X = Mi*k (k为非负整数),出现这种情况的只能是X为lcm( M1,M2,M3,....,Mn )。

  • 代码:
    #include using namespace std; typedef long long LL; LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b); } LL exgcd(LL a,LL b,LL &x,LL &y){ if(b==0){ x=1; y=0; return a; } LL d = exgcd(b,a%b,x,y); LL tmp = x; x = y; y = tmp - a/b*y; return d; } LL solve(LL a[],LL b[],LL num){ LL a1,a2,b1,b2,k1,kk,tmp,d,x,y,c; bool ok = true; a1 = a[0]; b1 = b[0]; for(int i=1;i>t; while(t--){ lcm = 1; cin>>n; for(int i=0;i>M[i]; lcm = lcm/gcd(lcm,M[i])*M[i]; } for(int i=0;i>A[i]; LL ans = solve(M,A,n); printf("Case %d: ",++kase); if( ans!=-1 && ans==0 ) cout<