結果
| 問題 | No.186 中華風 (Easy) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-11-08 18:45:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 925 bytes |
| 記録 | |
| コンパイル時間 | 1,980 ms |
| コンパイル使用メモリ | 196,712 KB |
| 最終ジャッジ日時 | 2025-02-17 20:08:58 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#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';
}