結果

問題 No.757 チャンパーノウン定数 (2)
ユーザー maimai
提出日時 2018-12-05 13:17:36
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,217 ms / 2,000 ms
コード長 1,912 bytes
コンパイル時間 443 ms
コンパイル使用メモリ 11,360 KB
実行使用メモリ 96,616 KB
最終ジャッジ日時 2023-09-25 18:42:31
合計ジャッジ時間 9,412 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,332 KB
testcase_01 AC 78 ms
15,284 KB
testcase_02 AC 79 ms
15,048 KB
testcase_03 AC 79 ms
15,280 KB
testcase_04 AC 79 ms
15,176 KB
testcase_05 AC 80 ms
15,088 KB
testcase_06 AC 79 ms
15,144 KB
testcase_07 AC 77 ms
15,164 KB
testcase_08 AC 77 ms
15,084 KB
testcase_09 AC 78 ms
15,288 KB
testcase_10 AC 78 ms
15,040 KB
testcase_11 AC 78 ms
15,292 KB
testcase_12 AC 79 ms
15,164 KB
testcase_13 AC 80 ms
15,264 KB
testcase_14 AC 79 ms
15,352 KB
testcase_15 AC 80 ms
15,344 KB
testcase_16 AC 78 ms
15,172 KB
testcase_17 AC 79 ms
15,048 KB
testcase_18 AC 79 ms
15,372 KB
testcase_19 AC 80 ms
15,128 KB
testcase_20 AC 78 ms
15,344 KB
testcase_21 AC 78 ms
15,176 KB
testcase_22 AC 76 ms
15,152 KB
testcase_23 AC 78 ms
15,040 KB
testcase_24 AC 79 ms
15,052 KB
testcase_25 AC 79 ms
15,340 KB
testcase_26 AC 78 ms
15,128 KB
testcase_27 AC 78 ms
15,164 KB
testcase_28 AC 131 ms
21,856 KB
testcase_29 AC 118 ms
19,784 KB
testcase_30 AC 81 ms
15,780 KB
testcase_31 AC 80 ms
15,664 KB
testcase_32 AC 78 ms
15,544 KB
testcase_33 AC 85 ms
17,072 KB
testcase_34 AC 92 ms
17,340 KB
testcase_35 AC 79 ms
15,412 KB
testcase_36 AC 80 ms
15,916 KB
testcase_37 AC 112 ms
19,212 KB
testcase_38 AC 119 ms
20,960 KB
testcase_39 AC 127 ms
21,740 KB
testcase_40 AC 136 ms
21,396 KB
testcase_41 AC 139 ms
21,260 KB
testcase_42 AC 1,217 ms
96,616 KB
testcase_43 AC 99 ms
21,012 KB
testcase_44 AC 149 ms
22,008 KB
testcase_45 AC 143 ms
22,472 KB
testcase_46 AC 103 ms
22,228 KB
testcase_47 AC 144 ms
21,812 KB
testcase_48 AC 144 ms
23,788 KB
testcase_49 AC 132 ms
22,224 KB
testcase_50 AC 80 ms
15,180 KB
testcase_51 AC 79 ms
15,176 KB
testcase_52 AC 80 ms
15,176 KB
testcase_53 AC 79 ms
15,348 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


def climb_flat4(x)
    
    ww = len = sum = nil
    L.downto(0) do |w|
        l = ( ((B-1)*(w)+B-1).to_s(B)+"0"*(w)       ).to_i(B)
        s = ( (w-1).to_s(B)+(B-2).to_s*(w-1)+(B-1).to_s ).to_i(B)
        break unless x < l+s
        ww = w
        len = l
        sum = s
    end
    
    return dig(x-sum+len/(B-1), ww+1)
end



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

0