結果

問題 No.16 累乗の加算
ユーザー DialBirdDialBird
提出日時 2017-02-12 19:41:17
言語 Ruby
(3.3.0)
結果
AC  
実行時間 92 ms / 5,000 ms
コード長 601 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 11,252 KB
実行使用メモリ 23,232 KB
最終ジャッジ日時 2023-09-08 12:05:20
合計ジャッジ時間 2,136 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
23,048 KB
testcase_01 AC 86 ms
23,168 KB
testcase_02 AC 88 ms
22,972 KB
testcase_03 AC 86 ms
23,180 KB
testcase_04 AC 87 ms
23,004 KB
testcase_05 AC 91 ms
23,180 KB
testcase_06 AC 86 ms
23,196 KB
testcase_07 AC 87 ms
23,016 KB
testcase_08 AC 86 ms
23,160 KB
testcase_09 AC 87 ms
23,128 KB
testcase_10 AC 86 ms
23,232 KB
testcase_11 AC 86 ms
23,028 KB
testcase_12 AC 86 ms
23,088 KB
testcase_13 AC 86 ms
23,088 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Solve
  def initialize
    @devider = 1000003
    @x, _ = gets.chomp.split.map(&:to_i)
    @list = gets.chomp.split.map{ |i| i.to_i % (@devider - 1) }
    @dp = Array.new(@devider, -1)
    
    @x %= @devider
  end

  def run
    result = 0
    @list.each do |l|
      if @dp[l] == -1
        @dp[l] = my_pow(@x, l)
      end
      result += @dp[l]
    end

    p result % @devider
  end

  private

  def my_pow(n, idx)
    res = 1
    if idx > 0
      res = my_pow(n*n % @devider, idx/2)
      res = res * n % @devider if idx & 1 == 1
    end
    return res % @devider
  end
end

Solve.new.run
0