結果

問題 No.186 中華風 (Easy)
ユーザー Nzt3
提出日時 2023-11-02 18:16:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,222 bytes
コンパイル時間 1,818 ms
コンパイル使用メモリ 197,232 KB
最終ジャッジ日時 2025-02-17 17:19:05
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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