結果

問題 No.2066 Simple Math !
ユーザー QCFiumQCFium
提出日時 2022-09-02 22:12:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 2,559 bytes
コンパイル時間 1,418 ms
コンパイル使用メモリ 164,456 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 04:16:14
合計ジャッジ時間 6,424 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 12 ms
4,380 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 162 ms
4,376 KB
testcase_05 AC 161 ms
4,376 KB
testcase_06 AC 160 ms
4,380 KB
testcase_07 AC 159 ms
4,380 KB
testcase_08 AC 157 ms
4,376 KB
testcase_09 AC 166 ms
4,380 KB
testcase_10 AC 162 ms
4,380 KB
testcase_11 AC 160 ms
4,376 KB
testcase_12 AC 164 ms
4,376 KB
testcase_13 AC 161 ms
4,376 KB
testcase_14 AC 149 ms
4,380 KB
testcase_15 AC 149 ms
4,380 KB
testcase_16 AC 150 ms
4,380 KB
testcase_17 AC 148 ms
4,376 KB
testcase_18 AC 149 ms
4,376 KB
testcase_19 AC 54 ms
4,380 KB
testcase_20 AC 54 ms
4,380 KB
testcase_21 AC 54 ms
4,380 KB
testcase_22 AC 56 ms
4,376 KB
testcase_23 AC 56 ms
4,380 KB
testcase_24 AC 98 ms
4,376 KB
testcase_25 AC 96 ms
4,376 KB
testcase_26 AC 97 ms
4,376 KB
testcase_27 AC 97 ms
4,380 KB
testcase_28 AC 98 ms
4,376 KB
testcase_29 AC 38 ms
4,376 KB
testcase_30 AC 7 ms
4,376 KB
testcase_31 AC 10 ms
4,380 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