結果

問題 No.186 中華風 (Easy)
ユーザー Nzt3Nzt3
提出日時 2023-11-02 17:28:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,675 bytes
コンパイル時間 2,191 ms
コンパイル使用メモリ 208,192 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-11-02 17:28:50
合計ジャッジ時間 3,499 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 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;
using lll=__int128_t;

// 頼んだ!128bit!!
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
  std::ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = std::end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = std::end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(std::ios_base::badbit);
    }
  }
  return dest;
}

namespace Lib{
  lll extGCD(lll a, lll b, lll &x, lll &y){
    if(b==0){
      x=1,y=0;
      return a;
    }
    lll d=extGCD(b,a%b,y,x);
    y=y-a/b*x;
    return d;
  }
}

pair<lll,lll> crt(vector<ll> m,vector<ll> r){
  assert(m.size()==r.size());
  int n=m.size();
  ll ret_m=1,ret_r=0;
  for(int i=0;i<n;i++){
    __int128_t 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);
    }
    lll ret_m2=lcm(ret_m,m[i]);
    lll ret_r2=ret_r+x*(((r[i]-ret_r)/d*ret_m)%ret_m2)%ret_m2;
    // cout<<"x "<<x<<" ret_r2 "<<ret_r2<<" ret_m "<<ret_m<<" ret_m2 "<<ret_m2<<" d "<<d;
    ret_m=ret_m2;
    ret_r=(ret_r2%ret_m+ret_m)%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(M,R);
  if(ans==0)ans+=mod;
  cout<<ans<<'\n';
  for(int i=0;i<3;i++){
    // cout<<(ans%M[i]==R[i])<<'\n';
  }
}
0