結果

問題 No.16 累乗の加算
ユーザー tshigtshig
提出日時 2016-08-04 14:50:51
言語 Ruby
(3.3.0)
結果
AC  
実行時間 86 ms / 5,000 ms
コード長 770 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 11,400 KB
実行使用メモリ 15,368 KB
最終ジャッジ日時 2023-09-08 12:00:42
合計ジャッジ時間 2,614 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,196 KB
testcase_01 AC 83 ms
15,368 KB
testcase_02 AC 86 ms
15,132 KB
testcase_03 AC 84 ms
15,200 KB
testcase_04 AC 83 ms
15,036 KB
testcase_05 AC 84 ms
15,260 KB
testcase_06 AC 86 ms
15,252 KB
testcase_07 AC 85 ms
15,316 KB
testcase_08 AC 86 ms
15,316 KB
testcase_09 AC 85 ms
15,252 KB
testcase_10 AC 84 ms
15,192 KB
testcase_11 AC 81 ms
15,024 KB
testcase_12 AC 84 ms
15,120 KB
testcase_13 AC 86 ms
15,252 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Calc0016
  def initialize(args)
    args = args.map { |l| l.chomp.split(/\s+/) }
    @x, @n = args.shift.map(&:to_i)
    @as = args.shift.map(&:to_i)
    @m = 1_000_003
    build_pow_table
  end

  def run
    r = 1
    bs = @as.sort.reverse
    bs.each_cons(2).map { |a1, a2| a1 - a2 }.each do |s|
      r = r * pow(s) % @m + 1
    end
    r = r * pow(bs.last) % @m
    r
  end

  def pow(k)
    r = 1
    k.to_s(2).split(//).reverse.each_with_index do |b, i|
      r = (r * @pow_table[i]) % @m if b == '1'
    end
    r
  end

  def build_pow_table
    prev = @x
    @pow_table = [@x]
    (1..100_000_000.bit_length).each do |i|
      prev = (prev * prev) % @m
      @pow_table << prev
    end
  end
end

puts Calc0016.new(STDIN.readlines).run if __FILE__ == $0
0