結果

問題 No.1092 modular arithmetic
ユーザー yuruhiyayuruhiya
提出日時 2020-06-26 12:57:50
言語 Crystal
(1.11.2)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,385 bytes
コンパイル時間 18,323 ms
コンパイル使用メモリ 256,920 KB
実行使用メモリ 11,620 KB
最終ジャッジ日時 2023-09-13 10:53:02
合計ジャッジ時間 20,433 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 22 ms
11,620 KB
testcase_02 AC 3 ms
4,496 KB
testcase_03 AC 29 ms
10,280 KB
testcase_04 AC 25 ms
9,524 KB
testcase_05 AC 24 ms
9,560 KB
testcase_06 AC 18 ms
8,372 KB
testcase_07 AC 24 ms
9,516 KB
testcase_08 AC 27 ms
10,124 KB
testcase_09 AC 24 ms
9,572 KB
testcase_10 AC 20 ms
8,920 KB
testcase_11 AC 22 ms
9,896 KB
testcase_12 AC 18 ms
8,424 KB
testcase_13 AC 13 ms
8,144 KB
testcase_14 AC 15 ms
8,440 KB
testcase_15 AC 5 ms
5,288 KB
testcase_16 AC 11 ms
7,268 KB
testcase_17 AC 14 ms
8,556 KB
testcase_18 AC 28 ms
10,336 KB
testcase_19 AC 18 ms
8,240 KB
testcase_20 AC 29 ms
10,284 KB
testcase_21 AC 24 ms
9,548 KB
testcase_22 AC 27 ms
10,020 KB
testcase_23 AC 40 ms
9,632 KB
testcase_24 AC 14 ms
5,792 KB
testcase_25 AC 31 ms
8,080 KB
testcase_26 AC 39 ms
9,748 KB
testcase_27 AC 8 ms
5,160 KB
testcase_28 AC 32 ms
8,416 KB
testcase_29 AC 48 ms
10,020 KB
testcase_30 AC 32 ms
8,436 KB
testcase_31 AC 6 ms
5,064 KB
testcase_32 AC 28 ms
7,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

struct ModInt
  @@mod : Int64 = 1_000_000_007i64

  def initialize(n)
    @n = n.to_i64 % @@mod
  end

  def self.zero
    ModInt.new(0)
  end

  def self.mod=(m)
    @@mod = m.to_i64
  end

  getter n : Int64

  def + : self
    self
  end

  def - : self
    ModInt.new(n != 0 ? @@mod - @n : 0)
  end

  def +(m)
    ModInt.new(@n + m.to_i64 % @@mod)
  end

  def -(m)
    ModInt.new(@n - m.to_i64 % @@mod)
  end

  def *(m)
    ModInt.new(@n * m.to_i64 % @@mod)
  end

  def /(m)
    raise DivisionByZeroError.new if m == 0
    a, b, u, v = m.to_i64, @@mod, 1i64, 0i64
    while b != 0
      t = a // b
      a -= t * b
      a, b = b, a
      u -= t * v
      u, v = v, u
    end
    ModInt.new(@n * u)
  end

  def //(m)
    self / m
  end

  def **(m)
    t, res = self, ModInt.new(1)
    while m > 0
      res *= t if m.odd?
      t *= t
      m >>= 1
    end
    res
  end

  def ==(m)
    @n == m.to_i64
  end

  def !=(m)
    @n != m.to_i64
  end

  def succ
    self + 1
  end

  def pred
    self - 1
  end

  def to_i64 : Int64
    @n
  end

  delegate to_s, to: @n
  delegate inspect, to: @n
end

ModInt.mod, n = read_line.split.map &.to_i
a = read_line.split.map { |i| ModInt.new(i) }
s = read_line
puts s.size.times.reduce(a.first) { |x, i|
  case s[i]
  when '+'
    x + a[i + 1]
  when '-'
    x - a[i + 1]
  when '*'
    x * a[i + 1]
  else
    x / a[i + 1]
  end
}
0