結果

問題 No.2066 Simple Math !
ユーザー karinohitokarinohito
提出日時 2022-09-02 22:05:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 3,103 bytes
コンパイル時間 2,286 ms
コンパイル使用メモリ 202,676 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 04:08:21
合計ジャッジ時間 12,846 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 21 ms
4,376 KB
testcase_02 AC 31 ms
4,380 KB
testcase_03 AC 26 ms
4,380 KB
testcase_04 AC 473 ms
4,376 KB
testcase_05 AC 472 ms
4,376 KB
testcase_06 AC 473 ms
4,380 KB
testcase_07 AC 475 ms
4,380 KB
testcase_08 AC 471 ms
4,376 KB
testcase_09 AC 492 ms
4,376 KB
testcase_10 AC 490 ms
4,380 KB
testcase_11 AC 487 ms
4,380 KB
testcase_12 AC 489 ms
4,376 KB
testcase_13 AC 491 ms
4,376 KB
testcase_14 AC 425 ms
4,376 KB
testcase_15 AC 427 ms
4,380 KB
testcase_16 AC 428 ms
4,380 KB
testcase_17 AC 426 ms
4,376 KB
testcase_18 AC 427 ms
4,380 KB
testcase_19 AC 145 ms
4,376 KB
testcase_20 AC 144 ms
4,380 KB
testcase_21 AC 144 ms
4,380 KB
testcase_22 AC 145 ms
4,380 KB
testcase_23 AC 146 ms
4,376 KB
testcase_24 AC 236 ms
4,376 KB
testcase_25 AC 233 ms
4,376 KB
testcase_26 AC 234 ms
4,380 KB
testcase_27 AC 233 ms
4,376 KB
testcase_28 AC 234 ms
4,380 KB
testcase_29 AC 56 ms
4,380 KB
testcase_30 AC 28 ms
4,380 KB
testcase_31 AC 25 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <random>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vvvvvll = vector<vvvvll>;
using vvvvvvll = vector<vvvvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vvvvb = vector<vvvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vvvvd = vector<vvvd>;
using vvvvvd = vector<vvvvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}


vector<ll> fact, factinv, inv;
ll mod = 1e9 + 7;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}
ll floor_sum(ll n, ll m, ll a, ll b) {
    ll ans = 0;
    if (a >= m) {
        ans += (n - 1) * n * (a / m) / 2;
        a %= m;
    }
    if (b >= m) {
        ans += n * (b / m);
        b %= m;
    }

    ll y_max = (a * n + b) / m, x_max = (y_max * m - b);
    if (y_max == 0) return ans;
    ans += (n - (x_max + a - 1) / a) * y_max;
    ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
    return ans;
}

int main() {

    ll T;
    cin >> T;
    rep(t, T) {
        ll P, Q, K;
        cin >> P >> Q >> K;
        ll G = gcd(P, Q);
        P /= G;
        Q /= G;
        if (P > Q)swap(P, Q);
        if (K > (P * Q - P - Q) - (P - 1) * (Q - 1) / 2) {
            ll an = P * Q - P - Q + 1;
            an += K - ((P * Q - P - Q) - (P - 1) * (Q - 1) / 2) - 1;
            cout << an * G << endl;
            continue;
        }
        ll L = 0, R = P * Q - P - Q + 1;
        while (R - L > 1) {
            ll mid = (R + L) / 2;
            ll M = mid / P;
            ll res = floor_sum(M + 1, Q,Q-P, mid);
            res += M+1;
            res -= M * (M + 1) / 2;
            res--;
            if (res >= K)R = mid;
            else L = mid;
        }
        cout <<R*G << endl;
    }
}
0