結果

問題 No.186 中華風 (Easy)
ユーザー Nzt3Nzt3
提出日時 2023-11-08 18:45:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 925 bytes
コンパイル時間 2,213 ms
コンパイル使用メモリ 205,628 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-08 18:45:42
合計ジャッジ時間 3,056 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;

namespace Lib{
  ll extGCD(ll a, ll b, ll &x, ll &y){
    if(b==0){
      x=1,y=0;
      return a;
    }
    ll d=extGCD(b,a%b,y,x);
    y=y-a/b*x;
    return d;
  }
}
namespace Lib{  
pair<ll,ll> crt(vector<ll> r,vector<ll>m){
  assert(r.size()==m.size());
  ll ret_r=r[0],ret_m=m[0];
  int n=r.size();
  for(int i=1;i<n;i++){
    ll x=0,y=0,d=extGCD(ret_m,m[i],x,y);
    if((ret_r-r[i])%d!=0){
      return make_pair(-1ll,-1ll);
    }
    ll tmp=x*(r[i]-ret_r)/d%(m[i]/d);
    ret_r+=tmp*ret_m;
    ret_m*=m[i]/d;
    ret_r%=ret_m;
    if(ret_r<0)ret_r+=ret_m;
  }
  if(ret_r<0)ret_r+=ret_m;
  return make_pair(ret_r,ret_m);
}
} // namespace Lib


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  vector r(3,0ll),m(3,0ll);
  for(int i=0;i<3;i++){
    cin>>r[i]>>m[i];
  }
  auto [ans,l]=Lib::crt(r,m);
  if(ans==0)ans=l;
  cout<<ans<<'\n';
}
0