結果

問題 No.186 中華風 (Easy)
ユーザー Nzt3Nzt3
提出日時 2023-11-02 18:16:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,222 bytes
コンパイル時間 2,130 ms
コンパイル使用メモリ 206,316 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-11-02 18:16:59
合計ジャッジ時間 3,278 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

ll safe_mod(ll x,ll m){
  x%=m;
  if(x<0)x+=m;
  return x;
}
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;
  }
}
ll modinv(ll a,ll m){
  assert(gcd(a,m)==1);
  ll x,y;
  Lib::extGCD(a,m,x,y);
  x=safe_mod(x,m);
  return x;
}

pair<ll,ll> crt(vector<ll> r,vector<ll> m){
  assert(m.size()==r.size());
  int n=m.size();
  ll ret_m=1,ret_r=0;
  for(int i=0;i<n;i++){
    assert(m[i]>=1);
    ll x=0,y=0,d=Lib::extGCD(ret_m,m[i],x,y);
    if(ret_r%d!=r[i]%d){
      return make_pair(-1ll,-1ll);
    }
    ll dr=x*(r[i]-ret_r)/d%(m[i]/d);
    // cout<<"x "<<x<<" ret_m "<<ret_m<<" dr "<<dr<<" d "<<d;
    ret_r+=dr*ret_m;
    ret_m*=m[i]/d;
    ret_r=safe_mod(ret_r,ret_m);
    // cout<<" ret_r "<<ret_r<<'\n';
  }
  return make_pair(ret_r,ret_m);
}

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