結果

問題 No.187 中華風 (Hard)
ユーザー assy1028assy1028
提出日時 2021-08-06 15:28:55
言語 C++17(clang)
(14.0.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,182 bytes
コンパイル時間 7,498 ms
コンパイル使用メモリ 138,936 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 01:55:11
合計ジャッジ時間 10,868 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 110 ms
4,348 KB
testcase_03 AC 107 ms
4,348 KB
testcase_04 AC 134 ms
4,348 KB
testcase_05 AC 134 ms
4,348 KB
testcase_06 AC 134 ms
4,348 KB
testcase_07 AC 134 ms
4,348 KB
testcase_08 AC 88 ms
4,348 KB
testcase_09 AC 88 ms
4,348 KB
testcase_10 AC 85 ms
4,348 KB
testcase_11 AC 134 ms
4,348 KB
testcase_12 AC 134 ms
4,348 KB
testcase_13 AC 28 ms
4,348 KB
testcase_14 AC 29 ms
4,348 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 103 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 133 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const ll INF = 1e9 + 9;
const ll MOD = 1e9 + 7;

int n;
ll x[1010], y[1010];

class Garner {
private:
    vector<ll> as;
    vector<ll> ms;


    ll gcd(ll a, ll b) {
        if (b == 0) return a;
        return gcd(b, a%b);
    }
    
    // return: gcd(a, b)
    // ax + by = gcd(a, b) を満たす (x, y) が格納される
    ll ext_gcd(ll a, ll b, ll &x, ll &y) {
        if (b == 0) {
            x = 1;
            y = 0;
            return a;
        }
        ll d = ext_gcd(b, a%b, y, x);
        y -= a/b * x;
        return d;
    }

    // ax = 1 (mod m) となる x を返す
    // gcd(a, m) != 1 の場合 -1 を返す
    ll mod_inverse(ll a, ll m) {
        ll x, y;
        ll g = ext_gcd(a, m, x, y);
        if (g > 1) return -1;
        if (x < 0) return m+x;
        return x;
    }

    bool make_coprime() {
        const int sz = as.size();
        for (int i = 0; i < sz; i++) {
            for (int j = i+1; j < sz; j++) {
                ll g = gcd(ms[i], ms[j]);
                if ((as[i] - as[j]) % g != 0) return false;
                ms[i] /= g;
                ms[j] /= g;
                ll gi = gcd(ms[i], g);
                ll gj = g / gi;
                do {
                    g = gcd(gi, gj);
                    gi *= g;
                    gj /= g;
                } while (g != 1);
                ms[i] *= gi;
                ms[j] *= gj;
                as[i] %= ms[i];
                as[j] %= ms[j];
            }
        }
        return true;
    }

public:
    void add_constraint(const ll a, const ll m) {
        as.push_back((a%m+m)%m);
        ms.push_back(m);
    }

    ll calc(const bool is_coprime, const ll MOD = 0) {
        if (!is_coprime) {
            // m を互いに素にする
            if (!make_coprime())
                return -1;
        }

        if (MOD == 0) {
            const int sz = as.size();
            ll m_prod = 1;
            ll x = as[0] % ms[0];
            for (int i = 1; i < sz; i++) {
                m_prod *= ms[i-1];
                ll t = (as[i] - x) * mod_inverse(m_prod, ms[i]) % ms[i];
                if (t < 0) t += ms[i];
                x += t * m_prod;
            }
            return x;
        } else {
            ms.push_back(MOD);
            const int sz = ms.size();
            vector<ll> coeffs(sz, 1);
            vector<ll> constants(sz, 0);
            for (int i = 0; i < (int)as.size(); i++) {
                ll t = (as[i] - constants[i]) * mod_inverse(coeffs[i], ms[i]) % ms[i];
                if (t < 0) t += ms[i];
                for (int j = i+1; j < sz; j++) {
                    constants[j] += t * coeffs[j];
                    constants[j] %= ms[j];
                    coeffs[j] *= ms[i];
                    coeffs[j] %= ms[j];
                }
            }
            return constants.back();
        }
    }
};

ll solve() {
    Garner g;
    for (int i = 0; i < n; i++) {
        g.add_constraint(x[i], y[i]);
    }
    return g.calc(false, MOD);
}

void input() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}
0