結果

問題 No.187 中華風 (Hard)
ユーザー w2w2w2w2
提出日時 2021-04-10 16:12:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 134 ms / 3,000 ms
コード長 8,986 bytes
コンパイル時間 7,428 ms
コンパイル使用メモリ 302,732 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 12:11:55
合計ジャッジ時間 9,943 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 115 ms
4,380 KB
testcase_03 AC 113 ms
4,380 KB
testcase_04 AC 134 ms
4,384 KB
testcase_05 AC 133 ms
4,376 KB
testcase_06 AC 133 ms
4,376 KB
testcase_07 AC 134 ms
4,380 KB
testcase_08 AC 117 ms
4,376 KB
testcase_09 AC 117 ms
4,380 KB
testcase_10 AC 117 ms
4,376 KB
testcase_11 AC 133 ms
4,376 KB
testcase_12 AC 132 ms
4,380 KB
testcase_13 AC 54 ms
4,376 KB
testcase_14 AC 55 ms
4,376 KB
testcase_15 AC 114 ms
4,384 KB
testcase_16 AC 115 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 102 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 132 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region Macros
#pragma GCC target("avx", "avx2")
#pragma GCC optimize("-O3", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/convolution>
#include <atcoder/fenwicktree>
#include <atcoder/lazysegtree>
#include <atcoder/math>
#include <atcoder/maxflow>
#include <atcoder/mincostflow>
#include <atcoder/modint>
#include <atcoder/scc>
#include <atcoder/segtree>
#include <atcoder/string>
#include <atcoder/twosat>
using namespace atcoder;
#endif
using ll= long long;
using ld= long double;
using ull= unsigned long long;
using i128= __int128;
using pll= pair<ll, ll>;
using vi= vector<int>;
using vl= vector<ll>;
using vd= vector<ld>;
using vs= vector<string>;
using vi128= vector<i128>;
using vb= vector<bool>;
using vpll= vector<pll>;
using vvi= vector<vi>;
using vvl= vector<vl>;
using vvd= vector<vd>;
using vvs= vector<vs>;
using vvi128= vector<vi128>;
using vvb= vector<vb>;
using vvpll= vector<vpll>;
#if __has_include(<atcoder/all>)
using mint= modint1000000007;
// using mint = modint998244353;
#endif
constexpr ll mod= 1e9 + 7;
// constexpr ll mod= 998244353;
#define ALL(x) (x).begin(), (x).end()
#define _overload3(_1, _2, _3, name, ...) name
#define REPBASE(i, a, b) for(ll(i)= (a), (i##_b)= (b); (i) < (i##_b); (i)++)
#define RREPBASE(i, a, b) for(ll(i)= (a), (i##_b)= (b); (i) >= (i##_b); (i)--)
#define LOOP(n) REPBASE(i##__COUNTER__##_##__LINE__, 0, n)
#define REPB(i, n) REPBASE(i, 0, n)
#define REPS(i, n) REPBASE(i, 1, n + 1)
#define RREP(i, n) RREPBASE(i, n - 1, 0)
#define RREPS(i, n) RREPBASE(i, n, 1)
#define REP(...) _overload3(__VA_ARGS__, REPBASE, REPB, LOOP)(__VA_ARGS__)
#define EACH(x, c) for(auto &x : c)
#define PERM(p)   \
    sort(ALL(p)); \
    for(bool(p##c)= 1; (p##c); (p##c)= next_permutation(ALL(p)))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
template <class T> inline ll SZ(const T &x) { return x.size(); }
ll TOPBIT(const ll &t) { return (t == 0 ? -1 : 63 - __builtin_clzll(t)); }
ll BIT(const ll &n) { return (1LL << n); }
template <class T> inline string YES(const T &n) { return (n ? "YES" : "NO"); }
template <class T> inline string Yes(const T &n) { return (n ? "Yes" : "No"); }
template <class T> inline string yes(const T &n) { return (n ? "yes" : "no"); }
template <class T> inline void UNIQUE(T &v) {
    v.erase(unique(ALL(v)), v.end());
}
template <class T, class U> inline ll LB(const T &v, const U &x) {
    return distance(v.begin(), lower_bound(ALL(v), x));
}
template <class T, class U> inline ll UB(const T &v, const U &x) {
    return distance(v.begin(), upper_bound(ALL(v), x));
}
template <class T> inline bool chmax(T &a, const T &b) {
    if(a < b) {
        a= b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
    if(b < a) {
        a= b;
        return 1;
    }
    return 0;
}
const vpll dx4{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const vpll dx8{{1, 0},  {1, 1},   {0, 1},  {-1, 1},
               {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
#define VEC(name, size, ...)        \
    vector<__VA_ARGS__> name(size); \
    IN(name)
#define VV(name, h, w, ...)                                        \
    vector<vector<__VA_ARGS__>> name((h), vector<__VA_ARGS__>(w)); \
    IN(name)
#define LL(...)     \
    ll __VA_ARGS__; \
    IN(__VA_ARGS__)
#define STR(...)        \
    string __VA_ARGS__; \
    IN(__VA_ARGS__)
#define LD(...)     \
    ld __VA_ARGS__; \
    IN(__VA_ARGS__)
#define I128(...)     \
    i128 __VA_ARGS__; \
    IN(__VA_ARGS__)
void _scan() {}
template <class T, class S> void _scan(pair<T, S> &p) {
    _scan(p.fi), _scan(p.se);
}
template <class T> void _scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
    _scan(head);
    IN(tail...);
}
template <class T> inline istream &operator>>(istream &stream, vector<T> &v) {
    EACH(x, v)
    stream >> x;
    return stream;
}
template <class T>
inline ostream &operator<<(ostream &stream, const vector<T> &v) {
    ll sz= SZ(v);
    REP(i, sz) { stream << v[i] << (i == sz - 1 ? "" : " "); }
    return stream;
}
template <class T>
inline ostream &operator<<(ostream &stream, const vector<vector<T>> &v) {
    ll sz= SZ(v);
    REP(i, sz) { stream << v[i] << (i == sz - 1 ? "" : "\n"); }
    return stream;
}
template <class T, class U>
inline istream &operator>>(istream &stream, pair<T, U> &p) {
    stream >> p.fi >> p.se;
    return stream;
}
template <class T, class U>
inline ostream &operator<<(ostream &stream, const pair<T, U> &p) {
    stream << p.fi << " " << p.se;
    return stream;
}
template <class T, class U>
inline pair<T, U> operator+(pair<T, U> p, const pair<T, U> &q) {
    p+= q;
    return p;
}
template <class T, class U>
inline pair<T, U> &operator+=(pair<T, U> &p, const pair<T, U> &q) {
    p.fi+= q.fi;
    p.se+= q.se;
    return p;
}
template <class T, class U>
inline pair<T, U> operator-(pair<T, U> p, const pair<T, U> &q) {
    p-= q;
    return p;
}
template <class T, class U>
inline pair<T, U> &operator-=(pair<T, U> &p, const pair<T, U> &q) {
    p.fi-= q.fi;
    p.se-= q.se;
    return p;
}
template <class T, class U> inline pair<T, U> operator+(const pair<T, U> &p) {
    return p;
}
template <class T, class U> inline pair<T, U> operator-(const pair<T, U> &p) {
    return mp(-p.fi, -p.se);
}
inline i128 parse(string &s) {
    reverse(ALL(s));
    i128 ret= 0;
    bool minus= 0;
    if(*(s.rbegin()) == '-') {
        minus= 1;
        s.pop_back();
    }
    while(!s.empty()) {
        if('0' <= *(s.rbegin()) && *(s.rbegin()) <= '9') {
            ret= ret * 10 + *(s.rbegin()) - '0';
            s.pop_back();
        } else {
            break;
        }
    }
    reverse(ALL(s));
    if(minus) ret*= -1;
    return ret;
}
inline string to_string(i128 val) {
    string ret= "";
    bool minus= 0;
    if(val < 0) {
        minus= 1;
        val*= -1;
    }
    do {
        int digit= val % 10;
        ret+= to_string(digit);
        val/= 10;
    } while(val != 0);
    if(minus) ret+= "-";
    reverse(ALL(ret));
    return ret;
}
inline istream &operator>>(istream &stream, i128 &val) {
    string str;
    stream >> str;
    val= parse(str);
    while(!str.empty()) {
        stream.putback(*(str.rbegin()));
        str.pop_back();
    }
    return stream;
}
inline ostream &operator<<(ostream &stream, const i128 &val) {
    stream << to_string(val);
    return stream;
}
inline void print() { cout << "\n"; }
template <class Head, class... Tail>
inline void print(const Head &H, const Tail &...T) {
    cout << H;
    (cout << ... << (cout << " ", T));
    cout << endl;
}
inline void debug_out() { cout << "\n"; }
template <class Head, class... Tail>
inline void debug_out(const Head &H, const Tail &...T) {
    cout << H;
    (cout << ... << (cout << " ", T));
    cout << endl;
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
#pragma endregion

/* safe_mod */
/* O(1) */
constexpr ll safe_mod(ll x, const ll &m= mod) {
    x%= m;
    if(x < 0) x+= m;
    return x;
}

/* modinv */
/* O(logmod) */
/* constraint: (a,m) == 1 */
/* requirement : safe_mod */
constexpr ll modinv(ll a, const ll &m= mod) {
    ll b= m, u= 1, v= 0;
    while(b) {
        ll t= a / b;
        a-= t * b;
        swap(a, b);
        u-= t * v;
        swap(u, v);
    }
    return safe_mod(u, m);
}

/* Chinese Remainder Theorem */
/* Garner's Algorithm */
/* O(|r|^2logmax(m)) */
/* requirement : safe_mod, modinv */
/* return (solution % mod,lcm(m) % mod) */
/* if no solution, return (0,0) */
pll Garner(vl r, vl ms, const ll &m= mod) {
    REP(i, SZ(r)) {
        REP(l, i) {
            ll g= gcd(ms[i], ms[l]);
            if(unlikely((r[i] - r[l]) % g != 0)) return {0, 0};
            ms[i]/= g;
            ms[l]/= g;
            ll gi= gcd(ms[i], g), gl= g / gi;
            do {
                g= gcd(gi, gl);
                gi*= g, gl/= g;
            } while(g != 1);
            ms[i]*= gi, ms[l]*= gl;
            r[i]%= ms[i], r[l]%= ms[l];
        }
    }
    ms.eb(m);
    vl coeffs(SZ(ms), 1);
    vl constants(SZ(ms), 0);
    REP(i, SZ(r)) {
        ll t= safe_mod((r[i] - constants[i]) * modinv(coeffs[i], ms[i]), ms[i]);
        REP(l, i + 1, SZ(ms)) {
            (constants[l]+= t * coeffs[l])%= ms[l];
            (coeffs[l]*= ms[i])%= ms[l];
        }
    }
    return {constants.back(), coeffs.back()};
}

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
    LL(N);
    vl X(N), Y(N);
    REP(i, N) cin >> X[i] >> Y[i];
    pll ans= Garner(X, Y);
    ll zero= 1;
    REP(i, N) if(X[i] != 0) zero= 0;
    if(ans.se == 0) {
        print(-1);
    } else {
        if(zero) {
            print(ans.se);
        } else {
            print(ans.fi);
        }
    }
}
0