結果

問題 No.2066 Simple Math !
ユーザー QCFiumQCFium
提出日時 2022-09-02 22:12:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 2,559 bytes
コンパイル時間 1,392 ms
コンパイル使用メモリ 167,836 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 21:47:11
合計ジャッジ時間 5,467 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 5 ms
6,940 KB
testcase_02 AC 13 ms
6,940 KB
testcase_03 AC 7 ms
6,940 KB
testcase_04 AC 153 ms
6,940 KB
testcase_05 AC 148 ms
6,944 KB
testcase_06 AC 151 ms
6,944 KB
testcase_07 AC 156 ms
6,940 KB
testcase_08 AC 154 ms
6,944 KB
testcase_09 AC 147 ms
6,944 KB
testcase_10 AC 154 ms
6,940 KB
testcase_11 AC 160 ms
6,944 KB
testcase_12 AC 155 ms
6,940 KB
testcase_13 AC 148 ms
6,944 KB
testcase_14 AC 138 ms
6,948 KB
testcase_15 AC 140 ms
6,944 KB
testcase_16 AC 141 ms
6,940 KB
testcase_17 AC 137 ms
6,940 KB
testcase_18 AC 141 ms
6,940 KB
testcase_19 AC 51 ms
6,940 KB
testcase_20 AC 49 ms
6,944 KB
testcase_21 AC 50 ms
6,944 KB
testcase_22 AC 51 ms
6,940 KB
testcase_23 AC 51 ms
6,940 KB
testcase_24 AC 92 ms
6,940 KB
testcase_25 AC 91 ms
6,944 KB
testcase_26 AC 88 ms
6,940 KB
testcase_27 AC 90 ms
6,940 KB
testcase_28 AC 91 ms
6,944 KB
testcase_29 AC 37 ms
6,944 KB
testcase_30 AC 7 ms
6,944 KB
testcase_31 AC 11 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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 gcd(int a, int b) {
	while (a && b) {
		if (a > b) a %= b;
		else b %= a;
	}
	return a + b;
}

int64_t solve(int p, int q, int k) {
	int g = gcd(p, q);
	p /= g;
	q /= g;
	int64_t lcm = (int64_t) p * q;
	int64_t lcm_cnt = floor_sum(p, p, q, p + q - 1);
	// std::cerr << lcm_cnt << std::endl;
	if (k <= lcm_cnt) {
		int64_t l = -1;
		int64_t r = lcm;
		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;
		}
		return r * g;
	} else {
		k -= lcm_cnt;
		return (lcm + k) * g;
	}
}

int main() {
	int t = ri();
	for (int i = 0; i < t; i++) {
		int p = ri();
		int q = ri();
		int k = ri();
		printf("%" PRId64 "\n", solve(p, q, k));
	}
	return 0;
}
0