結果

問題 No.187 中華風 (Hard)
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-05-26 13:40:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 491 ms / 3,000 ms
コード長 3,917 bytes
コンパイル時間 2,146 ms
コンパイル使用メモリ 194,440 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 00:11:36
合計ジャッジ時間 8,674 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
4,348 KB
testcase_01 AC 37 ms
4,348 KB
testcase_02 AC 211 ms
4,348 KB
testcase_03 AC 204 ms
4,348 KB
testcase_04 AC 399 ms
4,348 KB
testcase_05 AC 397 ms
4,348 KB
testcase_06 AC 399 ms
4,348 KB
testcase_07 AC 400 ms
4,348 KB
testcase_08 AC 491 ms
4,348 KB
testcase_09 AC 487 ms
4,348 KB
testcase_10 AC 489 ms
4,348 KB
testcase_11 AC 392 ms
4,348 KB
testcase_12 AC 402 ms
4,348 KB
testcase_13 AC 261 ms
4,348 KB
testcase_14 AC 212 ms
4,348 KB
testcase_15 AC 17 ms
4,348 KB
testcase_16 AC 19 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 33 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 318 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 397 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
typedef long long ll;
struct CRT {
    vector<pair<ll, ll>> v;
    void add(ll a, ll m) { v.push_back({ a, m }); } // x = a (mod m)

    set<ll> enumpr(ll n) { set<ll> V;
        for (ll i = 2; i*i <= n; i++) while (n%i == 0) V.insert(i), n /= i;
        if (n>1) V.insert(n); return V; }
    ll ext_gcd(ll p, ll q, ll& x, ll& y) {
        if (q == 0) return x = 1, y = 0, p; ll g = ext_gcd(q, p%q, y, x); y -= p / q*x; return g; }
    ll inv(ll p, ll q) { ll xx, yy, g = ext_gcd(p, q, xx, yy); if (xx<0) xx += q, yy -= p; return xx; }

    ll solve(ll mo = 1000000007) {
        string s; int N = v.size(); set<ll> P; vector<int> num;
        rep(i, 0, N) { set<ll> S = enumpr(v[i].second); for(ll r : S) P.insert(r);}
        num = vector<int>(N, 0);
        for(ll r : P) { int y = 0;
            rep(x, 0, N) { num[x] = 1; int j = v[x].second; while (j%r == 0) j /= r, num[x] *= r;
                if (num[y]<num[x]) y = x; }
            rep(x, 0, N) if (x != y) {
                if (v[x].first%num[x] != v[y].first%num[x]) return -1;
                v[x].second /= num[x]; v[x].first %= v[x].second; } }
        vector<pair<ll, ll> > V2;
        rep(x, 0, N) if (v[x].second>1) V2.emplace_back(v[x].second, v[x].first);
        sort(V2.begin(), V2.end()); v = V2; N = v.size();
        rep(y, 0, N) rep(x, 0, y) { ll k = v[y].second - v[x].second; if (k<0) k += v[y].first;
            v[y].second = k*inv(v[x].first, v[y].first) % v[y].first; }
        ll ret = 0; for (int x = N - 1; x >= 0; x--) ret = (ret*v[x].first + v[x].second) % mo;
        return ret;
    }
};
ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) {
    int ret = 1; while (b > 0) {
        if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1;
    } return ret;
}
int modinv(int a) { return modpow(a, mod - 2); }
map<int, int> enumpr(int n) {
    map<int, int> V;
    for (int i = 2; 1LL * i*i <= n; i++) while (n%i == 0) V[i]++, n /= i;
    if (n>1) V[n]++;
    return V;
}
ll modlcm(vector<int> v) {
    map<int, int> m;
    for (int i : v) {
        auto mm = enumpr(i);
        for (auto p : mm) m[p.first] = max(m[p.first], p.second);
    }
    int ans = 1;
    for (auto p : m) ans = mul(ans, modpow(p.first, p.second));
    return ans;
}
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/



//---------------------------------------------------------------------------------------------------
void _main() {
    CRT crt;

    int N; cin >> N;
    bool zero = true;
    vector<int> v;
    rep(i, 0, N) {
        int m, a; cin >> a >> m;
        crt.add(a, m);
        v.push_back(m);
        if (a != 0) zero = false;
    }

    int ans = 0;
    if (zero) ans = modlcm(v);
    else ans = crt.solve();
    cout << ans << endl;
}
0