結果

問題 No.187 中華風 (Hard)
ユーザー Nzt3Nzt3
提出日時 2023-11-08 23:45:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 3,000 ms
コード長 1,417 bytes
コンパイル時間 10,336 ms
コンパイル使用メモリ 207,540 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-08 23:46:13
合計ジャッジ時間 5,723 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 114 ms
6,676 KB
testcase_03 AC 112 ms
6,676 KB
testcase_04 AC 131 ms
6,676 KB
testcase_05 AC 131 ms
6,676 KB
testcase_06 AC 131 ms
6,676 KB
testcase_07 AC 131 ms
6,676 KB
testcase_08 AC 116 ms
6,676 KB
testcase_09 AC 116 ms
6,676 KB
testcase_10 AC 116 ms
6,676 KB
testcase_11 AC 131 ms
6,676 KB
testcase_12 AC 131 ms
6,676 KB
testcase_13 AC 54 ms
6,676 KB
testcase_14 AC 55 ms
6,676 KB
testcase_15 AC 112 ms
6,676 KB
testcase_16 AC 114 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 100 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 135 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 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;
  }
  ll modinv(ll a,ll m){
    ll x=0,y=0,d=extGCD(a,m,x,y);
    if(d!=1){
      return -1;
    }
    return x;
  }
}
namespace Lib{  
pair<ll,ll>crt_mod(vector<ll> r,vector<ll> m,ll MOD){
  int n=r.size();
  for(int i=0;i<n;i++){
    for(int j=0;j<i;j++){
      ll g=gcd(m[i],m[j]);
      if((r[i]-r[j])%g!=0){
        return make_pair(-1ll,-1ll);
      }
      m[i]/=g,m[j]/=g;
      ll gi=gcd(m[i],g),gj=g/gi;
      do{
        g=gcd(gi,gj);
        gi*=g,gj/=g;
      }while(g!=1);
      m[i]*=gi,m[j]*=gj;
      r[i]%=m[i],r[j]%=m[j];
    }
  }
  m.push_back(MOD);
  vector cef(n+1,1ll),con(n+1,0ll);
  for(int i=0;i<n;i++){
    ll t=(r[i]-con[i])*modinv(cef[i],m[i])%m[i];
    if(t<0)t+=m[i];
    for(int j=i+1;j<=n;j++){
      con[j]+=cef[j]*t;
      con[j]%=m[j];
      cef[j]=cef[j]*m[i]%m[j];
    }
  }
  return make_pair(con[n],cef[n]);
}
} // namespace Lib


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  const ll MOD=1e9+7;
  cin>>N;
  vector R(N,0ll),M(N,0ll);
  for(int i=0;i<N;i++)cin>>R[i]>>M[i];
  auto [ans,l]=Lib::crt_mod(R,M,MOD);
  for(int i=0;i<N;i++){
    if(R[i]!=0){
      cout<<ans<<'\n';
      return 0;
    }
  }
  cout<<l<<'\n';
}
0