結果

問題 No.187 中華風 (Hard)
ユーザー ningenMeningenMe
提出日時 2019-05-02 12:30:17
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,286 bytes
コンパイル時間 1,117 ms
コンパイル使用メモリ 157,384 KB
最終ジャッジ日時 2024-11-14 21:26:44
合計ジャッジ時間 1,471 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:20:25: error: ‘make_v’ function uses ‘auto’ type specifier without trailing return type
   20 | template<typename... T> auto make_v(size_t N,T... t){return vector<decltype(make_v(t...))>(N,make_v(t...));}
      |                         ^~~~
main.cpp:20:25: note: deduced return type only available with ‘-std=c++14’ or ‘-std=gnu++14’

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define REP(i,n) for(long long i = 0; i < (n); i++)
#define FOR(i, m, n) for(long long i = (m);i < (n); ++i)
#define ALL(obj) (obj).begin(),(obj).end()

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;

const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e15;
const long double PI = 3.1415926535897932384626433;

template<typename T> vector<T> make_v(size_t N,T init){return vector<T>(N,init);}
template<typename... T> auto make_v(size_t N,T... t){return vector<decltype(make_v(t...))>(N,make_v(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max<T>(a,b);}
template <class T> void chmin(T& a, const T b){a=min<T>(a,b);}
void YN(bool flg) {cout << ((flg) ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << ((flg) ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << ((flg) ? "yes" : "no") << endl;}

//Greatest Common Divisor
long long GCD(long long a, long long b) {
    return ((b == 0) ? a : GCD(b, a % b));
}

// 拡張 Euclid の互除法
long long Ext_GCD(long long a, long long b, long long &x, long long &y) {
    ll res;
	if (b == 0) res = a,x = 1,y = 0;
	else res = Ext_GCD(b, a%b, y, x), y -= a/b * x;
    return res;
}

// a and mod must be coprime
long long Inv_Mod(long long a, long long mod) {
    long long x, y;
    Ext_GCD(a, mod, x, y);
    return (x%mod+mod)%mod;
}

// Garner のアルゴリズムの前処理
long long Pre_Garner(vector<long long> &b, vector<long long> &m, long long mod) {
    long long res = 1;
    for (int l = 0; l < b.size(); ++l) {
        for (int r = l+1; r < b.size(); ++r) {
            long long g = GCD(m[l], m[r]);
            if ((b[l] - b[r]) % g != 0) return -1;
            m[l] /= g,m[r] /= g;
            long long gl = GCD(m[l], g), gr = g/gl;
            do {
                g = GCD(gl, gr);
                gl *= g, gr /= g;
            } while (g != 1);
            m[l] *= gl, m[r] *= gr;
            b[l] %= m[l], b[r] %= m[r];
        }
    }
    for (int i = 0; i < b.size(); ++i) (res *= m[i]) %= mod;
    return res;
}

long long Garner(vector<long long> b, vector<long long> m, long long mod) {
    m.push_back(mod);
    vector<long long> coeffs(m.size(), 1);
    vector<long long> constants(m.size(), 0);
    for (int k = 0; k < m.size(); ++k) {
        long long t = (b[k] - constants[k]) * Inv_Mod(coeffs[k], m[k]);
		((t %= m[k]) += m[k]) %= m[k];
        for (int i = k+1; i < m.size(); ++i) {
            (constants[i] += t * coeffs[i]) %= m[i];
            (coeffs[i] *= m[k]) %= m[i];
        }
    }
    return constants.back();
}

int main() {
	int N; cin >> N;
	V<ll> b(N), m(N);
	int flg = 1;
	for(int i = 0; i < N; ++i){
		cin >> b[i] >> m[i];
		if(b[i]) flg = 0;
	}
	ll lcm = Pre_Garner(b,m,MOD);
	ll ans = 0;
	if(flg) ans = lcm;
	else if (lcm == -1) ans = -1;
	else ans = Garner(b,m,MOD);
	cout << ans << endl;

	
	return 0;
}
0