HDU 3579: Hello Kiki
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3579: Hello Kiki相关的知识,希望对你有一定的参考价值。
Hello Kiki
///@author Sycamore, ZJNU ///@date 8/4/2017 ///@ref stanford-acm-master #include<bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair<int, int>PII; typedef long long ll; typedef pair<ll, ll>PLL; typedef vector<ll> VL; #define endl ‘\n‘ ll mod(ll a, ll b) { return ((a%b) + b) % b; } ll extended_euclid(ll a, ll b, ll &x, ll &y) { ll xx = y = 0; ll yy = x = 1; while (b) { ll q = a / b; ll t = b; b = a%b; a = t; t = xx; xx = x - q*xx; x = t; t = yy; yy = y - q*yy; y = t; } return a; } PLL chinese_remainder_theorem(ll m1, ll r1, ll m2, ll r2) { ll s, t; ll g = extended_euclid(m1, m2, s, t); if (r1%g != r2%g) return make_pair(0, -1); return make_pair(mod(s*r2*m1 + t*r1*m2, m1*m2) / g, m1*m2 / g); } PLL chinese_remainder_theorem(const VL &m, const VL &r) { PLL ret = make_pair(r[0], m[0]); for (ll i = 1; i < m.size(); i++) { ret = chinese_remainder_theorem(ret.second, ret.first, m[i], r[i]); if (ret.second == -1) break; } return ret; } int main() { ios::sync_with_stdio(false); int T; cin >> T; for (int t = 1; t <= T; t++) { int N; cin >> N; VL m(N), r(N); for (auto &e : m)cin >> e; for (auto &e : r)cin >> e; auto res = chinese_remainder_theorem(m, r); cout << "Case " << t << ": "; if (~res.second) { if (mod(res.first, res.second)) cout << mod(res.first, res.second) << ‘\n‘; else cout << res.second << ‘\n‘; } else cout << "-1\n"; } return 0; }
以上是关于HDU 3579: Hello Kiki的主要内容,如果未能解决你的问题,请参考以下文章
HDU3579Hello Kiki(中国剩余定理)(不互质的情况)
hdu3579-Hello Kiki拓展欧几里得-同余方程组