結果

問題 No.186 中華風 (Easy)
ユーザー e6i24v_kyoproe6i24v_kyopro
提出日時 2021-03-05 23:54:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,225 bytes
コンパイル時間 1,690 ms
コンパイル使用メモリ 166,436 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 12:00:29
合計ジャッジ時間 2,680 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

 
 
typedef long long ll;
 
using namespace std;

#define rep(i,n)  for(int i=0;i<(n);i++)
const ll INF=1000000000000000000;

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

ll extGcd(ll a,ll b,ll &p,ll &q){
    if(b==0){p=1;q=0;return a;}
    ll d=extGcd(b,a%b,q,p);
    q-=a/b*p;
    return d;
}

pair<ll,ll> ChineseRem(const vector<ll> &b,const vector<ll> &m){
    ll r=0,M=1;
    for(int i=0;i<(int)b.size();++i){
        ll p,q;
        ll d=extGcd(M,m[i],p,q);
        if((b[i]-r)%d!=0)return make_pair(0,-1);
        ll tmp=(b[i]-r)/d*p%(m[i]/d);
        r+=M*tmp;
        M*=m[i]/d;
    }
    return make_pair(mod(r,M),M);
    
}
ll gcd(ll a, ll b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}

ll lcm(ll a, ll b)
{
   return a * b / gcd(a, b);
}


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

    
    if(X[0]==X[1]&&X[1]==X[2]&&X[2]==0){
        cout<<lcm(Y[0],lcm(Y[1],Y[2]))<<endl;
    }else {
        pair<ll,ll> pr=ChineseRem(X,Y);
        if(pr.second==-1){
            cout<<-1<<endl;
        }else{
            cout<<pr.first<<endl;
        }
    }
    
    
    
}
0