結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
sntea
|
| 提出日時 | 2017-03-04 05:15:48 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,981 bytes |
| コンパイル時間 | 1,502 ms |
| コンパイル使用メモリ | 175,160 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-22 11:01:33 |
| 合計ジャッジ時間 | 2,273 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 WA * 20 |
ソースコード
#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;
}
sntea