結果

問題 No.2066 Simple Math !
ユーザー QCFiumQCFium
提出日時 2022-09-02 21:52:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,177 bytes
コンパイル時間 1,539 ms
コンパイル使用メモリ 167,036 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 21:23:26
合計ジャッジ時間 6,333 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 159 ms
6,944 KB
testcase_05 AC 160 ms
6,940 KB
testcase_06 AC 159 ms
6,944 KB
testcase_07 AC 160 ms
6,940 KB
testcase_08 AC 159 ms
6,944 KB
testcase_09 AC 159 ms
6,944 KB
testcase_10 AC 159 ms
6,944 KB
testcase_11 AC 158 ms
6,940 KB
testcase_12 AC 159 ms
6,944 KB
testcase_13 AC 158 ms
6,940 KB
testcase_14 AC 143 ms
6,944 KB
testcase_15 AC 144 ms
6,944 KB
testcase_16 AC 145 ms
6,944 KB
testcase_17 AC 144 ms
6,944 KB
testcase_18 AC 144 ms
6,940 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 117 ms
6,940 KB
testcase_25 AC 116 ms
6,944 KB
testcase_26 AC 116 ms
6,940 KB
testcase_27 AC 116 ms
6,940 KB
testcase_28 AC 115 ms
6,940 KB
testcase_29 AC 40 ms
6,940 KB
testcase_30 WA -
testcase_31 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

long long floor_sum(long long n, long long m, long long a, long long b) {
    assert(0 <= n && n < (1LL << 32));
    assert(1 <= m && m < (1LL << 32));
    unsigned long long ans = 0;
    if (a < 0) {
        unsigned long long a2 = safe_mod(a, m);
        ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);
        a = a2;
    }
    if (b < 0) {
        unsigned long long b2 = safe_mod(b, m);
        ans -= 1ULL * n * ((b2 - b) / m);
        b = b2;
    }
    return ans + floor_sum_unsigned(n, m, a, b);
}


int main() {
	int t = ri();
	for (int i = 0; i < t; i++) {
		int p = ri();
		int q = ri();
		int k = ri();
		
		int64_t l = 0;
		int64_t r = 1000000000000000000;
		while (r - l > 1) {
			int64_t m = l + ((r - l) >> 1);
			
			uint64_t t0 = m / p;
			uint64_t t1 = m / q;
			if (t0 * t1 / t0 != t1 || t0 * t1 >= 1000000000000) {
				r = m;
				continue;
			}
			
			int64_t s = m / p;
			int64_t cur = floor_sum(s + 1, q, p, m - s * p) + s;
			// std::cerr << m << " " << cur << std::endl;
			if (cur < k) l = m;
			else r = m;
		}
		printf("%" PRId64 "\n", r);
	}
	return 0;
}
0