結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
15,148 KB
testcase_01 AC 83 ms
15,296 KB
testcase_02 AC 83 ms
15,312 KB
testcase_03 AC 83 ms
15,276 KB
testcase_04 AC 83 ms
15,140 KB
testcase_05 AC 83 ms
15,084 KB
testcase_06 AC 82 ms
15,124 KB
testcase_07 AC 80 ms
15,108 KB
testcase_08 AC 82 ms
15,320 KB
testcase_09 AC 83 ms
15,048 KB
testcase_10 AC 83 ms
15,044 KB
testcase_11 AC 82 ms
15,088 KB
testcase_12 AC 83 ms
15,144 KB
testcase_13 AC 82 ms
15,124 KB
testcase_14 AC 82 ms
15,048 KB
testcase_15 AC 82 ms
15,228 KB
testcase_16 AC 82 ms
15,044 KB
testcase_17 AC 82 ms
15,144 KB
testcase_18 AC 82 ms
15,084 KB
testcase_19 AC 82 ms
15,240 KB
testcase_20 AC 82 ms
15,152 KB
testcase_21 AC 83 ms
15,048 KB
testcase_22 AC 83 ms
15,136 KB
testcase_23 AC 83 ms
15,260 KB
testcase_24 AC 84 ms
15,120 KB
testcase_25 AC 84 ms
15,132 KB
testcase_26 AC 84 ms
15,244 KB
testcase_27 AC 82 ms
15,356 KB
testcase_28 AC 161 ms
21,472 KB
testcase_29 AC 122 ms
19,712 KB
testcase_30 AC 84 ms
15,700 KB
testcase_31 AC 85 ms
15,568 KB
testcase_32 AC 83 ms
15,452 KB
testcase_33 AC 91 ms
16,820 KB
testcase_34 AC 96 ms
17,304 KB
testcase_35 AC 83 ms
15,560 KB
testcase_36 AC 85 ms
15,696 KB
testcase_37 AC 115 ms
19,216 KB
testcase_38 AC 120 ms
20,752 KB
testcase_39 AC 130 ms
21,672 KB
testcase_40 AC 148 ms
21,208 KB
testcase_41 AC 139 ms
21,248 KB
testcase_42 AC 122 ms
25,856 KB
testcase_43 AC 103 ms
21,288 KB
testcase_44 AC 153 ms
22,252 KB
testcase_45 AC 148 ms
22,740 KB
testcase_46 AC 108 ms
22,280 KB
testcase_47 AC 146 ms
21,692 KB
testcase_48 AC 148 ms
23,612 KB
testcase_49 AC 133 ms
22,276 KB
testcase_50 AC 82 ms
15,240 KB
testcase_51 AC 82 ms
15,088 KB
testcase_52 AC 84 ms
15,048 KB
testcase_53 AC 85 ms
15,296 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 L <= 20
    puts climb_flat4(N-1) unless L <= 20
end

0