結果

問題 No.757 チャンパーノウン定数 (2)
ユーザー parukiparuki
提出日時 2018-12-17 11:21:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,307 ms / 2,000 ms
コード長 1,254 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 11,796 KB
実行使用メモリ 10,472 KB
最終ジャッジ日時 2023-10-25 23:27:27
合計ジャッジ時間 17,622 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,120 KB
testcase_01 AC 26 ms
10,072 KB
testcase_02 AC 29 ms
10,100 KB
testcase_03 AC 27 ms
10,104 KB
testcase_04 AC 30 ms
10,120 KB
testcase_05 AC 31 ms
10,128 KB
testcase_06 AC 30 ms
10,140 KB
testcase_07 AC 29 ms
10,136 KB
testcase_08 AC 34 ms
10,156 KB
testcase_09 AC 34 ms
10,156 KB
testcase_10 AC 27 ms
10,072 KB
testcase_11 AC 28 ms
10,100 KB
testcase_12 AC 26 ms
10,104 KB
testcase_13 AC 31 ms
10,120 KB
testcase_14 AC 31 ms
10,128 KB
testcase_15 AC 31 ms
10,140 KB
testcase_16 AC 27 ms
10,136 KB
testcase_17 AC 35 ms
10,156 KB
testcase_18 AC 34 ms
10,156 KB
testcase_19 AC 26 ms
10,072 KB
testcase_20 AC 27 ms
10,100 KB
testcase_21 AC 27 ms
10,104 KB
testcase_22 AC 31 ms
10,124 KB
testcase_23 AC 31 ms
10,128 KB
testcase_24 AC 33 ms
10,140 KB
testcase_25 AC 28 ms
10,136 KB
testcase_26 AC 33 ms
10,156 KB
testcase_27 AC 33 ms
10,156 KB
testcase_28 AC 884 ms
10,356 KB
testcase_29 AC 674 ms
10,340 KB
testcase_30 AC 37 ms
10,208 KB
testcase_31 AC 43 ms
10,188 KB
testcase_32 AC 34 ms
10,156 KB
testcase_33 AC 54 ms
10,164 KB
testcase_34 AC 242 ms
10,260 KB
testcase_35 AC 42 ms
10,184 KB
testcase_36 AC 39 ms
10,204 KB
testcase_37 AC 526 ms
10,292 KB
testcase_38 AC 762 ms
10,336 KB
testcase_39 AC 971 ms
10,348 KB
testcase_40 AC 1,307 ms
10,432 KB
testcase_41 AC 1,297 ms
10,436 KB
testcase_42 AC 383 ms
10,268 KB
testcase_43 AC 992 ms
10,376 KB
testcase_44 AC 1,269 ms
10,472 KB
testcase_45 AC 912 ms
10,348 KB
testcase_46 AC 671 ms
10,316 KB
testcase_47 AC 1,104 ms
10,404 KB
testcase_48 AC 624 ms
10,324 KB
testcase_49 AC 975 ms
10,348 KB
testcase_50 AC 34 ms
10,156 KB
testcase_51 AC 32 ms
10,140 KB
testcase_52 AC 28 ms
10,104 KB
testcase_53 AC 27 ms
10,072 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