結果

問題 No.2526 Kth Not-divisible Number
ユーザー moharan627moharan627
提出日時 2023-11-03 21:53:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 2,206 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 81,820 KB
実行使用メモリ 77,332 KB
最終ジャッジ日時 2023-11-03 21:54:02
合計ジャッジ時間 4,645 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
55,804 KB
testcase_01 AC 35 ms
55,804 KB
testcase_02 AC 35 ms
55,804 KB
testcase_03 AC 411 ms
76,836 KB
testcase_04 AC 389 ms
76,828 KB
testcase_05 AC 384 ms
76,832 KB
testcase_06 AC 411 ms
76,816 KB
testcase_07 AC 397 ms
76,812 KB
testcase_08 AC 185 ms
76,820 KB
testcase_09 AC 179 ms
77,332 KB
testcase_10 AC 239 ms
76,704 KB
testcase_11 AC 183 ms
77,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#sys.setrecursionlimit(10 ** 6)
#sys.set_int_max_str_digits(0)
INF = float('inf')
MOD = 10**9 + 7
MOD2 = 998244353
from collections import defaultdict
def ceil(A,B):
    return -(-A//B)
import math
def my_lcm(x, y):
    return (x * y) // math.gcd(x, y)
def solve():
    def II(): return int(sys.stdin.readline())
    def LI(): return list(map(int, sys.stdin.readline().split()))
    def LC(): return list(sys.stdin.readline().rstrip())
    def IC(): return [int(c) for c in sys.stdin.readline().rstrip()]
    def MI(): return map(int, sys.stdin.readline().split())
    T = II()

    def meguru_bisect(ng, ok):
        """
        この条件は、問題に基づくやつじゃなくてもOK
        条件の付け方は、ngとokの広げ方をどのようにするかで決まってくる。

        負の無限~(目的値)が条件を満たす場合
        ng…条件を満たさない値の範囲での最小値
        ok…条件を満たす値の範囲での最大値
        ok < ng
        is_ok == Trueの場合、探索範囲が右半分へ
        is_ok == Falseの場合、探索範囲が左半分へ

        (目的値)~正の無限 が条件を満たす場合
        ng…条件を満たさない値の範囲での最大値
        ok…条件を満たす値の範囲での最小値
        ng < ok
        is_ok == Trueの場合、探索範囲が左半分へ
        is_ok == Falseの場合、探索範囲が右半分へ
        """
        while (abs(ok - ng) > 1):
            mid = (ok + ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok
    for test in range(T):
        A,B,K = MI()
        LCM = my_lcm(A,B)
        a_num = (LCM-1)//A
        b_num = (LCM-1)//B
        k_num = LCM-(a_num+b_num)-1
        #print(k_num,K)
        P = K//k_num
        if(K%k_num==0):
            P-=1
        Ans = P*LCM
        K-= k_num*P
        #print(k_num,P,K)
        add = 0
        if(1<=K):
            def is_ok(n):
                A_n = n//A
                B_n = n//B
                return K<= n-(A_n+B_n)
            add = meguru_bisect(0,LCM)
        print(Ans+add)
    return
solve()
0