結果
| 問題 | No.186 中華風 (Easy) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-09-17 16:54:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 1,112 bytes |
| 記録 | |
| コンパイル時間 | 675 ms |
| コンパイル使用メモリ | 71,040 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-19 18:41:22 |
| 合計ジャッジ時間 | 1,497 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#line 1 "a.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/186"
#include<iostream>
using namespace std;
#line 1 "/home/kotatsugame/library/math/linear_congruence.cpp"
//require extgcd
#include<vector>
#line 1 "/home/kotatsugame/library/math/extgcd.cpp"
template<typename T>
T extgcd(T a,T b,T&x,T&y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
T q=a/b;
T g=extgcd(b,a-q*b,y,x);
y-=q*x;
return g;
}
#line 4 "/home/kotatsugame/library/math/linear_congruence.cpp"
pair<long long,long long>linear_congruence(const vector<int>&A,const vector<int>&B,const vector<int>&M)
//find Ax=B mod M. NA=>(0,-1)
{
long long x=0,m=1;
for(int i=0;i<A.size();i++)
{
long long a=m*A[i],b=B[i]-x*A[i],u,v;
long long d=extgcd(a,(long long)M[i],u,v);
if(b%d!=0)return make_pair(0LL,-1LL);
long long t=b/d*u%(M[i]/d);
x+=m*t;
m*=M[i]/d;
}
x%=m;
if(x<0)x+=m;
return make_pair(x,m);
}
#line 5 "a.cpp"
int main()
{
vector<int>r(3),m(3);
for(int i=0;i<3;i++)cin>>r[i]>>m[i];
pair<long,long>p=linear_congruence({1,1,1},r,m);
if(p.second==-1)cout<<-1<<endl;
else cout<<(p.first==0?p.second:p.first)<<endl;
}