結果
| 問題 |
No.186 中華風 (Easy)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-02-28 14:51:53 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,967 bytes |
| コンパイル時間 | 1,949 ms |
| コンパイル使用メモリ | 199,024 KB |
| 最終ジャッジ日時 | 2025-01-19 08:09:06 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
is >> p.first >> p.second;
return is;
}
template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
for(int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != (int)v.size() ? " " : "");
}
return os;
}
template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
for(T &in : v) is >> in;
return is;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) res += ", ";
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef PLOKI_LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// calculate ap + bq = gcd(a, b)
// return d = gcd(a, b)
ll extgcd(ll a, ll b, ll& p, ll& q) {
if(b == 0) {
p = 1;
q = 0;
return a;
}
ll d = extgcd(b, a % b, q, p);
q -= a / b * p;
return d;
}
// solve the equation
// * x == r1 (mod. m1)
// * x == r2 (mod. m2)
// let return val be (r, m),
// the solution can be written as
// (r, m) == (0, -1) if not exist
pair<ll, ll> crt(ll r1, ll m1, ll r2, ll m2) {
ll p, q;
ll d = extgcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d)
if((r2 - r1) % d != 0) return {0, -1};
ll m = m1 * (m2 / d); // lcm of (m1, m2)
ll tmp = (r2 - r1) / d * p % (m2 / d);
ll r = r1 + m1 * tmp;
r = (r % m + m) % m;
return {r, m};
}
int main() {
vector<int> x(3), y(3);
for (int i = 0; i < 3; ++i) {
cin >> x[i] >> y[i];
}
auto [xt, yt] = crt(x[0], y[0], x[1], y[1]);
if(xt == 0 && yt == -1) {
cout << -1 << endl;
return 0;
}
auto [ax, ay] = crt(xt, yt, x[2], y[2]);
if(ax == 0 && ay == -1) {
cout << -1 << endl;
return 0;
}
debug(ax, ay);
if(ax <= 0) ax += ay;
cout << ax << endl;
}