結果

問題 No.187 中華風 (Hard)
ユーザー snteasntea
提出日時 2017-03-04 05:15:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,981 bytes
コンパイル時間 3,086 ms
コンパイル使用メモリ 171,788 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 12:20:07
合計ジャッジ時間 3,392 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#ifdef LOCAL111
	#define _GLIBCXX_DEBUG
#else
	#define NDEBUG
#endif
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; }

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#ifdef LOCAL111
	#define DEBUG(x) cout<<#x<<": "<<(x)<<endl
	template<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
#else
	#define DEBUG(x) true
	template<typename T> void dpite(T a, T b){ return; }
#endif
#define F first
#define S second
#define SNP string::npos
#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": "
template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}

template <typename T>
long long gcd(T x, T y){
	return y==0 ? x : gcd(y, x%y);
}

template <typename T>
long long lcm(T x, T y){
	return x/gcd(x,y)*y;
}

//res.first*a+res.second*b == 1 となるresを返す (a,bは互いに素)
pair<long long, long long> extgcd(long long a,long long b)
{
	if(b==1){
		return pair<long long, long long>(0,1);
	}
	pair<long long, long long> t=extgcd(b,a%b);
	return pair<long long, long long>(t.second,t.first-a/b*t.second);
}

//modの逆元を返す
long long inverse(long long a,long long modl)
{
	return (extgcd(modl,a).second+modl)%modl;
}

// x = second (mod first) 0以上の最小解
long long garner(const vector<pair<long long, long long>>& ex) {
	int n = ex.size();
	long long res = 0;
	long long k = 1;
	for(int i = 0; i < n; ++i) {
		long long x, m;
		tie(m,x) = ex[i];
		x = (x%m+m)%m;
		long long g = gcd(k,m);
		if((x-res)%g != 0) return -1;
		long long inv = inverse(k/g,m/g);
		long long v = ((x-res)/g%m+m)%m*inv%m;
		long long nk = k/g*m;
		res = (res+v*k%nk)%nk;
		k = k/g*m;
	}
	return res;
}

long long garner(const vector<long long>& x, const vector<long long>& mod){
	int n = x.size();
	vector<pair<long long, long long>> v(n);
	for(int i = 0; i < n; ++i) {
		v[i] = {mod[i],x[i]};
	}
	return garner(v);
}

typedef long long int LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;

void ios_init(){
	//cout.setf(ios::fixed);
	//cout.precision(12);
#ifdef LOCAL111
	return;
#endif
	ios::sync_with_stdio(false); cin.tie(0);	
}

int main()
{
	ios_init();
	int n;
	while(cin >> n){
		vector<LL> x(n),y(n);
		REP(i,n) cin >> x[i] >> y[i];
		auto ans = garner(x,y);
		if(ans == 0) ans = lcm(lcm(y[0],y[1]),y[2]);
		cout << ans << endl;
	}
	return 0;
}
0