結果
問題 | No.117 組み合わせの数 |
ユーザー | yuruhiya |
提出日時 | 2021-05-30 13:09:55 |
言語 | Crystal (1.11.2) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,526 bytes |
コンパイル時間 | 14,571 ms |
コンパイル使用メモリ | 295,364 KB |
実行使用メモリ | 90,496 KB |
最終ジャッジ日時 | 2024-11-08 20:47:15 |
合計ジャッジ時間 | 16,025 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ソースコード
# verification-helper: PROBLEM https://yukicoder.me/problems/no/117 # require "../math/Mint" macro static_modint(name, mod) struct {{name}} MOD = Int64.new({{mod}}) def self.zero new end def self.raw(value : Int64) result = new result.value = value result end @value : Int64 def initialize @value = 0i64 end def initialize(value) @value = value.to_i64 % MOD end def initialize(m : self) @value = m.value end protected def value=(value : Int64) @value = value end getter value : Int64 def + : self self end def - : self self.class.raw(value != 0 ? MOD &- value : 0i64) end def +(v) self + v.to_m end def +(m : self) x = value &+ m.value x &-= MOD if x >= MOD self.class.raw(x) end def -(v) self - v.to_m end def -(m : self) x = value &- m.value x &+= MOD if x < 0 self.class.raw(x) end def *(v) self * v.to_m end def *(m : self) self.class.new(value &* m.value) end def /(v) self / v.to_m end def /(m : self) raise DivisionByZeroError.new if m.value == 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 self.class.new(value &* u) end def //(v) self / v end def **(exponent : Int) t, res = self, self.class.raw(1i64) while exponent > 0 res *= t if exponent & 1 == 1 t *= t exponent >>= 1 end res end def ==(v) value == v end def ==(m : self) value == m.value end def succ self.class.raw(value != MOD &- 1 ? value &+ 1 : 0i64) end def pred self.class.raw(value != 0 ? value &- 1 : MOD &- 1) end def abs self end def to_i64 : Int64 value end delegate to_s, to: @value delegate inspect, to: @value end struct Int def to_m : {{name}} {{name}}.new(self) end end class String def to_m : {{name}} {{name}}.new(self) end end end # require "../math/Combination" class Combination(T) def initialize @size = 2 @factorial = [T.new(1), T.new(1)] @inv = [T.zero, T.new(1)] @finv = [T.new(1), T.new(1)] end private def expand_until(n : Int) while @size <= n @factorial << @factorial[-1] * @size @inv << -@inv[T::MOD % @size] * (T::MOD // @size) @finv << @finv[-1] * @inv[@size] @size += 1 end end def factorial(n : Int) expand_until(n) @factorial[n] end def inv(n : Int) expand_until(n) @inv[n] end def finv(n : Int) expand_until(n) @finv[n] end def permutation(n : Int, r : Int) (n < r || n < 0 || r < 0) ? T.zero : factorial(n) * finv(n - r) end def combination(n : Int, r : Int) (n < r || n < 0 || r < 0) ? T.zero : factorial(n) * finv(r) * finv(n - r) end def repeated_combination(n : Int, r : Int) (n < 0 || r < 0) ? T.zero : r == T.zero ? T.new(1) : combination(n + r - 1, r) end end static_modint(Mint, 10**9 + 7) C = Combination(Mint).new read_line.to_i.times do read_line =~ /(.).(\d+).(\d+)./ c, n, r = $1[-1], $2.to_i, $3.to_i puts case c when 'C' C.combination(n, r) when 'P' C.permutation(n, r) when 'H' C.repeated_combination(n, r) end end