結果

問題 No.498 ワープクリスタル (給料日編)
ユーザー yuruhiyayuruhiya
提出日時 2021-01-24 11:38:11
言語 Crystal
(1.11.2)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 3,219 bytes
コンパイル時間 19,201 ms
コンパイル使用メモリ 257,776 KB
実行使用メモリ 96,772 KB
最終ジャッジ日時 2023-09-13 13:17:47
合計ジャッジ時間 21,635 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
14,716 KB
testcase_01 AC 17 ms
15,444 KB
testcase_02 AC 16 ms
14,684 KB
testcase_03 AC 15 ms
15,260 KB
testcase_04 AC 167 ms
96,040 KB
testcase_05 AC 17 ms
15,464 KB
testcase_06 AC 165 ms
96,180 KB
testcase_07 AC 166 ms
96,772 KB
testcase_08 AC 166 ms
95,864 KB
testcase_09 AC 173 ms
95,608 KB
testcase_10 AC 15 ms
14,604 KB
testcase_11 AC 147 ms
94,988 KB
testcase_12 AC 16 ms
15,372 KB
testcase_13 AC 141 ms
95,460 KB
testcase_14 AC 15 ms
15,760 KB
testcase_15 AC 24 ms
19,200 KB
testcase_16 AC 17 ms
15,056 KB
testcase_17 AC 22 ms
19,216 KB
testcase_18 AC 178 ms
96,028 KB
testcase_19 AC 160 ms
94,816 KB
testcase_20 AC 163 ms
96,612 KB
testcase_21 AC 161 ms
95,080 KB
testcase_22 AC 162 ms
95,620 KB
testcase_23 AC 162 ms
95,324 KB
testcase_24 AC 162 ms
96,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

struct Mint
  @@MOD = 1_000_000_007i64

  def self.mod
    @@MOD
  end

  def self.zero
    Mint.new
  end

  @value : Int64

  def initialize
    @value = 0i64
  end

  def initialize(value)
    @value = value.to_i64 % @@MOD
  end

  def initialize(m : Mint)
    @value = m.value
  end

  protected def value=(value : Int64)
    @value = value
  end

  getter value : Int64

  def + : self
    self
  end

  def - : self
    Mint.new(value != 0 ? @@MOD - @value : 0)
  end

  def +(m)
    Mint.new(@value + m.to_i64 % @@MOD)
  end

  def +(m : Mint)
    result = Mint.new
    result.value = @value + m.value
    result.value -= @@MOD if result.value > @@MOD
    result
  end

  def -(m)
    Mint.new(@value - m.to_i64 % @@MOD)
  end

  def -(m : Mint)
    result = Mint.new
    result.value = @value - m.value
    result.value += @@MOD if result.value < 0
    result
  end

  def *(m)
    Mint.new(@value * m.to_i64)
  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
    Mint.new(@value * u)
  end

  def //(m)
    self / m
  end

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

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

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

  def succ
    self + 1
  end

  def pred
    self - 1
  end

  def to_i64 : Int64
    @value
  end

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

struct Int
  def to_mint : Mint
    Mint.new(self)
  end
end

class String
  def to_mint : Mint
    Mint.new(self)
  end
end

class Comb(T)
  def initialize(@Max = 400009)
    @factorial = Array(T).new(@Max, T.zero)
    @finv = Array(T).new(@Max, T.zero)
    @inv = Array(T).new(@Max, T.zero)
    @factorial[0] = @factorial[1] = T.new(1)
    @finv[0] = @finv[1] = T.new(1)
    @inv[1] = T.new(1)
    (2...@Max).each do |i|
      @factorial[i] = @factorial[i - 1] * i
      @inv[i] = -@inv[T.mod % i] * (T.mod / i)
      @finv[i] = @finv[i - 1] * @inv[i]
    end
  end

  def permutation(n, r)
    (n < r || n < 0 || r < 0) ? T.zero : @factorial[n] * @finv[n - r]
  end

  def combination(n, r)
    (n < r || n < 0 || r < 0) ? T.zero : @factorial[n] * @finv[r] * @finv[n - r]
  end

  def homogeneous_product(n, r)
    (n < 0 || r < 0) ? T.zero : r == T.zero ? T.new(1) : combination(n + r - 1, r)
  end

  def factorial(n)
    @factorial[n]
  end
end

comb = Comb(Mint).new

h, w, n = read_line.split.map(&.to_i)
y, x, k = (1..n).map { read_line.split.map(&.to_i) }.transpose
puts (0...16).to_a.repeated_permutations(n).sum { |cnt|
  if (0...n).all? { |i| cnt[i] <= k[i] }
    y_sum = (0...n).sum { |i| y[i]*cnt[i] }
    x_sum = (0...n).sum { |i| x[i]*cnt[i] }
    if {h, w} == {y_sum, x_sum}
      sum = cnt.sum
      cnt.reduce(1.to_mint) { |acc, c|
        kk = comb.combination(sum, c)
        sum -= c
        acc * kk
      }
    else
      Mint.zero
    end
  else
    Mint.zero
  end
}
0