結果

問題 No.757 チャンパーノウン定数 (2)
ユーザー maimai
提出日時 2018-12-05 13:04:27
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,278 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 249 ms
コンパイル使用メモリ 11,504 KB
実行使用メモリ 96,596 KB
最終ジャッジ日時 2023-09-25 17:00:43
合計ジャッジ時間 7,972 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
15,172 KB
testcase_01 AC 82 ms
15,060 KB
testcase_02 AC 82 ms
15,060 KB
testcase_03 AC 82 ms
15,192 KB
testcase_04 AC 81 ms
15,360 KB
testcase_05 AC 85 ms
15,100 KB
testcase_06 AC 83 ms
15,300 KB
testcase_07 AC 81 ms
15,400 KB
testcase_08 AC 81 ms
15,116 KB
testcase_09 AC 82 ms
15,360 KB
testcase_10 AC 82 ms
15,188 KB
testcase_11 AC 84 ms
15,056 KB
testcase_12 AC 83 ms
15,284 KB
testcase_13 AC 82 ms
15,148 KB
testcase_14 AC 80 ms
15,196 KB
testcase_15 AC 81 ms
15,056 KB
testcase_16 AC 80 ms
15,336 KB
testcase_17 AC 80 ms
15,168 KB
testcase_18 AC 81 ms
15,328 KB
testcase_19 AC 81 ms
15,328 KB
testcase_20 AC 82 ms
15,304 KB
testcase_21 AC 82 ms
15,100 KB
testcase_22 AC 83 ms
15,172 KB
testcase_23 AC 82 ms
15,156 KB
testcase_24 AC 81 ms
15,328 KB
testcase_25 AC 82 ms
15,116 KB
testcase_26 AC 84 ms
15,300 KB
testcase_27 AC 83 ms
15,164 KB
testcase_28 AC 99 ms
17,928 KB
testcase_29 AC 110 ms
18,356 KB
testcase_30 AC 87 ms
15,712 KB
testcase_31 AC 83 ms
15,668 KB
testcase_32 AC 84 ms
15,448 KB
testcase_33 AC 83 ms
15,736 KB
testcase_34 AC 96 ms
17,476 KB
testcase_35 AC 84 ms
15,524 KB
testcase_36 AC 87 ms
15,672 KB
testcase_37 AC 109 ms
18,164 KB
testcase_38 AC 107 ms
18,072 KB
testcase_39 AC 111 ms
18,592 KB
testcase_40 AC 125 ms
19,640 KB
testcase_41 AC 123 ms
19,620 KB
testcase_42 AC 1,278 ms
96,596 KB
testcase_43 AC 93 ms
18,116 KB
testcase_44 AC 120 ms
19,084 KB
testcase_45 AC 101 ms
17,432 KB
testcase_46 AC 88 ms
16,224 KB
testcase_47 AC 116 ms
18,716 KB
testcase_48 AC 94 ms
16,596 KB
testcase_49 AC 103 ms
17,956 KB
testcase_50 AC 83 ms
15,260 KB
testcase_51 AC 84 ms
15,220 KB
testcase_52 AC 85 ms
15,304 KB
testcase_53 AC 86 ms
15,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

B = gets.to_i
S = gets.chomp
L = S.size
N = S.to_i(B)
#N = gets.to_i(B)
#N = 9**100000

# guchoku
# str = ((0..999).map{|e|e.to_s(B)}*"")
# puts str[0..50]
# puts str[N]

def dig(y, w)
    # range "000".."999"
    i = y/w
    d = y%w
    i.to_s(B)[d] || "0"
end

# deprecated
def climb(x, sz, ofs, w)
    # range "100".."999"
    len = (sz-ofs)*w
    # p [x,sz,ofs,w]
    # p (ofs...sz).map{|e|e.to_s(B)}
    if x >= len
        climb(x-len, sz*B, sz, w+1)
    else
        # p B**(w-1)
        # ((ofs...sz).map{|e|e.to_s(B)}*"")[x]
        dig(x+B**(w-1)*w, w)
    end
end


# deprecated
def climb_flat(x, sz, ofs, w)
    len = (sz-ofs)*w
    while x >= len
        x, sz, ofs, w = x-len, sz*B, sz, w+1
        len = (sz-ofs)*w
    end
    dig(x+B**(w-1)*w, w)
end

# deprecated
def climb_flat2(x)
    ofs, w = 1, 1
    
    len = (B-1)*w # ((B-1)*w)*B**w
    l = 0
    while x >= len
        l += len
        x, ofs, w = x-len, ofs*B, w+1
        len = ofs*(w*(B-1))
    end
    dig(x+len/(B-1), w)
end

# deprecated
def climb_flat3(x)
    
    ((L-10)..(L-1)).each do |w|
        len = ( ((B-1)*(w)+B-1).to_s(B)+"0"*(w)       ).to_i(B)
        sum = ( (w-1).to_s(B)+(B-2).to_s*(w-1)+(B-1).to_s ).to_i(B)
        return dig(x-sum+len/(B-1), w+1) if x < len+sum
    end
    
    len = ( ((B-1)*(L-1)+B-1).to_s(B)+"0"*(L-1)       ).to_i(B)
    sum = ( (L-2).to_s(B)+(B-2).to_s*(L-2)+(B-1).to_s ).to_i(B)
    
    return dig(x-sum+len/(B-1), L)
end


if N == 0
    p 0
else
    puts climb_flat2(N-1) if B <= 2 || L <= 20
    puts climb_flat3(N-1) unless B <= 2 || L <= 20
end

0