結果
| 問題 |
No.186 中華風 (Easy)
|
| コンテスト | |
| ユーザー |
sntea
|
| 提出日時 | 2017-03-04 04:53:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,664 bytes |
| コンパイル時間 | 1,739 ms |
| コンパイル使用メモリ | 175,456 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-19 18:30:14 |
| 合計ジャッジ時間 | 2,552 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#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;
DEBUG(v); DEBUG(m);
DEBUG(nk);
res = (res+(v%nk)*(k%nk)%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);
}
long long use_garner(vector<long long>& x, vector<long long>& mod){
int n = x.size();
for(int i = 0; i < n; ++i) {
x[i] %= mod[i];
}
if(n == 1) return x[0];
long long g = gcd(mod[0],mod[1]);
for(int i = 2; i < n; ++i) {
g = gcd(g,mod[i]);
}
vector<pair<long long, long long>> ex(n);
for(int i = 0; i < n; ++i) {
mod[i] /= g;
if(x[i]%g != 0) return -1;
x[i] /= g;
ex[i] = {mod[i],x[i]};
}
ex.erase(unique(ex.begin(), ex.end()),ex.end());
for(int i = 0; i < n-1; ++i) {
if(ex[i].first == ex[i+1].second) return -1;
}
dpite(ALL(ex));
return garner(ex);
}
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 = 3;
vector<long long> x(n);
vector<long long> y(n);
while(cin >> x[0] >> y[0]){
cin >> x[1] >> y[1];
cin >> x[2] >> y[2];
auto ans = garner(x,y);
if(ans == 0) ans = lcm(lcm(y[0],y[1]),y[2]);
cout << ans << endl;
}
return 0;
}
sntea