結果

問題 No.187 中華風 (Hard)
ユーザー ningenMeningenMe
提出日時 2019-05-02 15:08:07
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,109 bytes
コンパイル時間 1,197 ms
コンパイル使用メモリ 156,560 KB
最終ジャッジ日時 2024-11-14 21:26:59
合計ジャッジ時間 1,571 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、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;}

class Garner{
public:
	long long lcm,mod,r;
	int flg,N;
	vector<long long> b,m,coef,val;

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

	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^-1 mod b, a and b must be coprime
	long long Inv_Mod(long long a, long long b) {
		long long x, y;
		Ext_GCD(a, b, x, y);
		return (x%b+b)%b;
	}

	// O(N^2 ?) r mod m_i = b_i, lcm = LCM(m_i) 
	Garner(const vector<long long> &ar1, const vector<long long> &ar2, const long long mod)
	 : b(ar1), m(ar2), mod(mod), N(ar1.size()), lcm(1), flg(1), coef(N+(mod > 0),1),val(N+(mod > 0),0) {
		long long g,gl,gr;
		for (int l = 0; l < N && flg; ++l) {
			for (int r = l+1; r < N && flg; ++r) {
				g = GCD(m[l], m[r]);
				
				if ((b[l] - b[r]) % g != 0) {
					flg = 0;
					break;
				}

				m[l] /= g, m[r] /= g;
				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 < N; ++i) {
			lcm *= m[i];
			if(mod > 0) lcm %= mod;
		}
		if(!flg){
			r = -1;
			return;
		} 
		if(mod > 0) m.push_back(mod);
		for (int i = 0; i < N; ++i) {
			long long t = (b[i] - val[i]) * Inv_Mod(coef[i], m[i]);
			((t %= m[i]) += m[i]) %= m[i];
			for (int j = i+1; j < N + (mod > 0); ++j) {
				(val[j] += t * coef[j]) %= m[j];
				(coef[j] *= m[i]) %= m[j];
			}
		}
		r = val[N-1 + (mod > 0)];
	}

};


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;
	}
	class Garner g(b,m,MOD);	
	ll ans = g.r;
	if(flg) ans = g.lcm;
	cout << ans << endl;
	
	return 0;
}
0