結果
| 問題 | No.186 中華風 (Easy) |
| コンテスト | |
| ユーザー |
moti
|
| 提出日時 | 2015-04-20 01:08:24 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,382 bytes |
| 記録 | |
| コンパイル時間 | 659 ms |
| コンパイル使用メモリ | 64,328 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-04 16:07:36 |
| 合計ジャッジ時間 | 1,429 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 WA * 8 |
ソースコード
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
typedef long long ll;
ll gcd(ll a, ll b) {
if(b == 0) { return a; }
return gcd(b, a%b);
}
ll extgcd(ll a, ll b, ll& x, ll& y)
{
ll d = a;
if(b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
}
else {
x = 1; y = 0;
}
return d;
}
ll mod_inverse(ll a, ll m)
{
ll b = m, x = 1, y = 0;
while(b) {
ll t = a/b;
a -= t*b; swap(a, b);
x -= t*y; swap(x, y);
}
return (x%m + m) % m;
// ll x, y;
extgcd(a, m, x, y);
return (m + x % m) % m;
}
pair<ll, ll> linear_congruence(const vector<ll>& A, const vector<ll>& B, const vector<ll>& M) {
ll x = 0, m = 1;
for(int i=0; i<A.size(); i++) {
ll a = A[i] * m, b = B[i] - A[i] * x, d = gcd(M[i], a);
if(b % d != 0) { return make_pair(0, -1); }
ll t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
x += m * t;
m *= M[i] / d;
}
return make_pair(x % m, m);
}
int main() {
vector<ll> A(3), B(3),M(3);
rep(i, 3) {
cin >> B[i] >> M[i];
A[i] = 1;
}
auto ans = linear_congruence(A, B, M);
if(ans.second == -1) {
cout << -1 << endl;
}
else {
if(ans.first == 0) {
cout << ans.second << endl;
}
else {
cout << ans.first << endl;
}
}
return 0;
}
moti