結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,168 KB
testcase_01 AC 78 ms
15,124 KB
testcase_02 AC 77 ms
15,196 KB
testcase_03 AC 77 ms
15,124 KB
testcase_04 AC 78 ms
15,288 KB
testcase_05 AC 78 ms
15,268 KB
testcase_06 AC 77 ms
15,264 KB
testcase_07 AC 79 ms
15,196 KB
testcase_08 AC 79 ms
15,164 KB
testcase_09 AC 78 ms
15,204 KB
testcase_10 AC 77 ms
15,312 KB
testcase_11 AC 78 ms
15,044 KB
testcase_12 AC 79 ms
15,288 KB
testcase_13 AC 79 ms
15,060 KB
testcase_14 AC 79 ms
15,148 KB
testcase_15 AC 79 ms
15,176 KB
testcase_16 AC 78 ms
15,144 KB
testcase_17 AC 79 ms
15,288 KB
testcase_18 AC 79 ms
15,196 KB
testcase_19 AC 79 ms
15,292 KB
testcase_20 AC 80 ms
15,240 KB
testcase_21 AC 79 ms
15,036 KB
testcase_22 AC 79 ms
15,144 KB
testcase_23 AC 78 ms
15,172 KB
testcase_24 AC 78 ms
15,340 KB
testcase_25 AC 78 ms
15,244 KB
testcase_26 AC 79 ms
15,248 KB
testcase_27 AC 79 ms
15,100 KB
testcase_28 AC 96 ms
17,908 KB
testcase_29 AC 106 ms
18,372 KB
testcase_30 AC 80 ms
15,720 KB
testcase_31 AC 81 ms
15,584 KB
testcase_32 AC 80 ms
15,416 KB
testcase_33 AC 80 ms
15,900 KB
testcase_34 AC 96 ms
17,364 KB
testcase_35 AC 83 ms
15,612 KB
testcase_36 AC 80 ms
15,792 KB
testcase_37 AC 102 ms
18,060 KB
testcase_38 AC 100 ms
18,268 KB
testcase_39 AC 108 ms
18,780 KB
testcase_40 AC 118 ms
19,348 KB
testcase_41 AC 118 ms
19,372 KB
testcase_42 AC 1,215 ms
96,420 KB
testcase_43 AC 89 ms
17,960 KB
testcase_44 AC 116 ms
19,088 KB
testcase_45 AC 97 ms
17,384 KB
testcase_46 AC 84 ms
16,116 KB
testcase_47 AC 113 ms
18,612 KB
testcase_48 AC 88 ms
16,712 KB
testcase_49 AC 100 ms
18,032 KB
testcase_50 AC 79 ms
15,060 KB
testcase_51 AC 80 ms
15,084 KB
testcase_52 AC 80 ms
15,036 KB
testcase_53 AC 80 ms
15,192 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)
    lx = x
    
    # ofs, w = 1, 1
    # len = (B-1)*w # ((B-1)*w)*B**w
    # l = 0
    # i = 0
    # while x >= len
    #     l += len
    #     x, ofs, w = x-len, ofs*B, w+1
    #     len = ofs*(w*(B-1))
    #     #p [len, l]
    #     #p [( ((B-1)*(i+1)+B-1).to_s(B)+"0"*(i+1)       ).to_i(B), ( (i).to_s(B)+(B-2).to_s*(i)+(B-1).to_s ).to_i(B)]
    #     i += 1
    # end
    # p [len, l]
    # p [x, len+l]
    # p [L, i]
    
    
    ((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(lx-sum+len/(B-1), w+1) if lx < 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)
    #p [len, sum]
    
    return dig(lx-sum+len/(B-1), L)
end


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

0