結果

問題 No.1243 約数加算
ユーザー FromBooskaFromBooska
提出日時 2023-02-22 12:40:50
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,212 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 837,572 KB
最終ジャッジ日時 2023-09-29 20:46:24
合計ジャッジ時間 5,201 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,368 KB
testcase_01 AC 72 ms
71,128 KB
testcase_02 AC 74 ms
71,276 KB
testcase_03 WA -
testcase_04 MLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 2**120 > 1**18なので、1から10**18まで120回で達することができる
# まず最大値より大きければどんどん大きくするのがいい、貪欲的
# 入力例1の(26, 57)で、26未満の約数を最初に加えるメリットはない
# 26+26とすれば、26の約数はすべてキープされて後でも使えるから
# 毎回約数をリストすると間に合わないか

def divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

T = int(input())
for t in range(T):

    A, B = map(int, input().split())
    ans = []
    while B >= A*2:
        ans.append(A)
        A *= 2

    from bisect import *
    while B > A:
        #print(A, B)
        divs = divisors(A) # Aの約数は変わっていく
        idx = bisect_left(divs, B-A+1)
        num = divs[idx-1]
        while B >= A+num:
            ans.append(num)
            #print(B, A, divs, divs[idx-1])
            A += num
        #print(A, B, num)
    print(len(ans))
    print(*ans)
0