結果

問題 No.2119 一般化百五減算
ユーザー NAMIDAIRONAMIDAIRO
提出日時 2022-11-04 22:13:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 2,297 bytes
コンパイル時間 1,111 ms
コンパイル使用メモリ 108,792 KB
実行使用メモリ 6,468 KB
最終ジャッジ日時 2023-09-26 00:40:16
合計ジャッジ時間 2,672 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
#include <iomanip>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

void chmax(ll & x, ll y) { x = max(x, y); }


ll gcd(ll x, ll y) {
    if (y == 0) {
        return x;
    }
    return gcd(y, x % y);
    // return y ? gcd(y, x%y) : x;
}

//ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll x, ll y) {
    return x / gcd(x, y) * y;
}

ll extgcd(ll x, ll y, ll& a, ll& b) {
    if (y == 0) {
        a = 1LL, b = 0LL;
        return x;
    }
    ll g = extgcd(y, x % y, a, b);
    ll ta = a, tb = b;
    a = tb;
    b = ta - x / y * tb;
    if (a < 0LL) a += y / g, b -= x / g; //when you wanna a >= 0 
    return g;
}

struct status {
    ll g, lcm, cr;
};

//m(mod x), n(mod y)
status crt(ll x, ll y, ll m, ll n) {
    status res;
    ll a, b;
    res.g = extgcd(x, y, a, b); //assume non-zero
    //g = xa + yb

    res.lcm = x / res.g * y;
    if ((m - n) % res.g) res.cr = -1;
    else {//xa'+m=-yb'+n ⇔ xa'+yb'=n-m=kg ⇔ xak+ybk = kg
        res.cr = (n - m) / res.g * a % (y / res.g); //係数の桁数を先に落とす
        res.cr = res.cr * x + m;
        res.cr = (res.cr % res.lcm + res.lcm) % res.lcm;
    }
    return res;
}
int main()
{
    int n, m;
    cin >> n >> m;

    vi<int> b(m), c(m);
    rep(i, m) cin >> b[i] >> c[i];
    rep(i, m) {
        c[i] -= c[i] / b[i] * b[i];
        if (c[i] < 0) c[i] += b[i];
    }

    map<int, int> mp;
    rep(i, m) {
        if (mp.count(b[i]) && mp[b[i]] != c[i]) {
            cout << "NaN" << endl;
            return 0;
        }
        mp[b[i]] = c[i];
    }

    auto itr = prev(mp.end(), 1);
    int lb = itr->first, lc = itr->second;
    int ans = lc;

    while (ans <= n) {
        bool ok = true;
        for (auto elem : mp) {
            if ((ans - elem.second) % elem.first) {
                ok = false;
                break;
            }
        }
        if (ok) {
            cout << ans << endl;
            return 0;
        }
        ans += lb;
    }
    cout << "NaN" << endl;
	return 0;
}
0