結果

問題 No.186 中華風 (Easy)
ユーザー polyomino_24polyomino_24
提出日時 2019-10-16 15:47:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,358 bytes
コンパイル時間 1,189 ms
コンパイル使用メモリ 124,944 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 00:57:31
合計ジャッジ時間 2,271 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
template<typename T>
T gcd(T a, T b){
    while(b){
        a %= b;
        swap(a, b);
    }
    return a;
}

//ax+by=gcd(a,b)を満たすx,yを求め、gcd(a,b)を返す
//(a/b*b+a%b)x+by=gcd(a,b)=gcd(b,a%b)
//b(a/b*x+y)+(a%b)x=gcd(b,a%b)
//https://qiita.com/drken/items/b97ff231e43bce50199a
template<typename T>
T ext_gcd(T a, T b, T &x, T &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }else{
        T g = ext_gcd(b, a % b, y, x);
        y -= a / b * x;
        return g;
    }
}
//x=v_i(mod m_i)を満たすx=r(mod m=lcm(m_1,m_2))を求める
//m_iが互いに素な必要なし
template<typename T>
pair<T, T> CRT(T v1, T m1, T v2, T m2){
    T a, b;
    T g = ext_gcd(m1, m2, a, b);
    if(v1 % g != v2 % g){
        return make_pair(-1, -1);//存在しない
    }
    T m = m1 / g * m2;
    T temp = (v2 - v1) / g * a % (m2 / g);
    T r = ((v1 + temp * m1) % m + m) % m;
    return make_pair(r, m);
}
int main(){
    ll v[3],m[3];
    rep(i,3)cin >> v[i] >> m[i];
    auto ans = CRT(v[0], m[0], v[1], m[1]);
    if(ans.first == -1){
        cout << - 1 << endl;
    }else{
        ans = CRT(ans.first, ans.second, v[2], m[2]);
        if(ans.first)cout << ans.first << endl;
        else cout << ans.second << endl;
    }
}
0