結果

問題 No.757 チャンパーノウン定数 (2)
ユーザー parukiparuki
提出日時 2018-12-17 11:22:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,393 ms / 2,000 ms
コード長 1,254 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 83,128 KB
最終ジャッジ日時 2023-10-25 23:27:49
合計ジャッジ時間 16,091 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
55,620 KB
testcase_01 AC 34 ms
53,572 KB
testcase_02 AC 35 ms
53,572 KB
testcase_03 AC 34 ms
53,572 KB
testcase_04 AC 37 ms
55,620 KB
testcase_05 AC 37 ms
55,620 KB
testcase_06 AC 38 ms
55,620 KB
testcase_07 AC 35 ms
53,572 KB
testcase_08 AC 45 ms
57,668 KB
testcase_09 AC 38 ms
57,668 KB
testcase_10 AC 35 ms
53,572 KB
testcase_11 AC 36 ms
53,572 KB
testcase_12 AC 34 ms
53,572 KB
testcase_13 AC 37 ms
55,620 KB
testcase_14 AC 37 ms
55,620 KB
testcase_15 AC 38 ms
55,620 KB
testcase_16 AC 40 ms
53,572 KB
testcase_17 AC 42 ms
57,668 KB
testcase_18 AC 39 ms
57,668 KB
testcase_19 AC 34 ms
53,572 KB
testcase_20 AC 38 ms
55,620 KB
testcase_21 AC 35 ms
53,572 KB
testcase_22 AC 36 ms
55,620 KB
testcase_23 AC 37 ms
55,620 KB
testcase_24 AC 37 ms
55,620 KB
testcase_25 AC 34 ms
53,572 KB
testcase_26 AC 39 ms
57,668 KB
testcase_27 AC 41 ms
57,668 KB
testcase_28 AC 906 ms
78,916 KB
testcase_29 AC 681 ms
77,580 KB
testcase_30 AC 46 ms
70,136 KB
testcase_31 AC 58 ms
75,172 KB
testcase_32 AC 39 ms
57,668 KB
testcase_33 AC 65 ms
75,176 KB
testcase_34 AC 267 ms
75,648 KB
testcase_35 AC 57 ms
75,164 KB
testcase_36 AC 47 ms
72,372 KB
testcase_37 AC 541 ms
76,844 KB
testcase_38 AC 796 ms
78,100 KB
testcase_39 AC 1,014 ms
79,708 KB
testcase_40 AC 1,393 ms
83,128 KB
testcase_41 AC 939 ms
78,560 KB
testcase_42 AC 350 ms
75,448 KB
testcase_43 AC 886 ms
77,236 KB
testcase_44 AC 915 ms
78,512 KB
testcase_45 AC 660 ms
76,932 KB
testcase_46 AC 665 ms
76,116 KB
testcase_47 AC 800 ms
77,684 KB
testcase_48 AC 464 ms
76,072 KB
testcase_49 AC 748 ms
77,192 KB
testcase_50 AC 41 ms
57,668 KB
testcase_51 AC 39 ms
55,620 KB
testcase_52 AC 35 ms
53,572 KB
testcase_53 AC 36 ms
53,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# n桁の数は
# f(n) = (B-1)Σ[i=1...n]i*B^(i-1) 個ある。
# g(n) = Σ[i=1...n]i*B^(i-1)
# とおくとg(n)は等差*等比の形になっている。

# 以下を参考に
# 等比×等差の和を求める2通りの方法 | 高校数学の美しい物語
# https://mathtrain.jp/ar
# 
# 等比Bに注目して差をとる。つまり
# g(n) = 1*1 + 2*B + 3*B^2 + ... + n*B^(n-1)
# Bg(n) =      1*B + 2*B^2 + 3*B^3 + ... + (n-1)*B^(n-1) + n*B^n
# の差をとると
# (1-B)g(n) = 1 + B + B^2 + B^3 + ... + B^(n-1) - n*B^n
# 右辺にできた等比数列に注目すると
# (1-B)g(n) = (B^n-1)/(B-1) - n*B^n
# g(n) = ((B^n-1) / (B-1) - n*B^n) / (1-B)
# よって
# f(n) = n*B^n - (B^n-1)/(B-1)
# ちなみにWolframAlphaを使えば、g(n)はすぐに求まる。

B = int(input())
D = input().strip()
# D(B) => E(10)
E = 0
for d in D:
    E = E*B + int(d)

def f(n):
    return n*B**n - (B**n-1)//(B-1)

# 何桁か
# (low, high]
low = 0
high = 100000
while high-low>1:
    med = (low+high)//2
    if f(med)>=E:
        high = med
    else:
        low = med

digit = high
index = E-f(digit-1)# 1-indexed
mod = (index-1)%digit

# numberの上位mod桁目 0-indexed
num = B**(digit-1)+(index-1)//digit
ans = (num//B**(digit-mod-1))%B
print(ans)

0