結果

問題 No.186 中華風 (Easy)
ユーザー HIcoderHIcoder
提出日時 2023-09-09 11:51:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,710 bytes
コンパイル時間 910 ms
コンパイル使用メモリ 103,072 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 11:52:01
合計ジャッジ時間 1,871 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=1e9+7;


inline long long  mod(long long  a,long long m){
    return (a%m+m)%m;
}


long long  extGcd(long long  a,long long  b,long long& x,long long& y){
    long long  d=a;
    if(b!=0){
        d=extGcd(b,a%b,y,x);
        y-=(a/b)*x;
    }else{
        x=1;y=0;
    }

    return d;
}



// x ≡ bi (mod mi)
// 中国剰余定理
// リターン値を (r, m) とすると解は x ≡ r (mod. m)
// 解なしの場合は (0, -1) をリターン
std::pair<long long, long long> ChineseRem(const std::vector<long long> &b, const std::vector<long long> &m) {
  long long r = 0, M = 1;
  for (int i = 0; i < (int)b.size(); ++i) {
    long long p, q;
    long long d = extGcd(M, m[i], p, q); // p is inv of M/d (mod. m[i]/d)
    if ((b[i] - r) % d != 0) return std::make_pair(0, -1);
    long long tmp = (b[i] - r) / d * p % (m[i]/d);
    r += M * tmp;
    M *= m[i]/d;
  }
  return std::make_pair(mod(r, M), M);
}

ll gcd(ll x,ll y){
    return y==0?x:gcd(y,x%y);
}

ll lcm(ll x,ll y){
    
    return x/gcd(x,y)*y;
}


int main(){
    vector<ll> X(3),Y(3);
    for(int i=0;i<3;i++){
        cin>>X[i]>>Y[i];
    }

    if(X[0]==0 && X[1]==0 && X[2]==0){
        ll ans=1;
        for(int i=0;i<3;i++){
            ans=lcm(Y[i],ans);
        }

        cout<<ans<<endl;
        return 0;
    }

    auto [ans,mod]=ChineseRem(X,Y);

    if(mod==-1){
        cout<<-1<<endl;
    }else{
        cout<<ans<<endl;
    }

}
0