結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー yuruhiyayuruhiya
提出日時 2020-10-19 19:24:39
言語 Crystal
(1.11.2)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 2,206 bytes
コンパイル時間 18,483 ms
コンパイル使用メモリ 254,520 KB
実行使用メモリ 92,984 KB
最終ジャッジ日時 2023-09-13 12:20:31
合計ジャッジ時間 37,114 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
75,084 KB
testcase_01 AC 152 ms
74,916 KB
testcase_02 AC 157 ms
74,932 KB
testcase_03 AC 148 ms
74,872 KB
testcase_04 AC 155 ms
75,060 KB
testcase_05 AC 161 ms
74,828 KB
testcase_06 AC 160 ms
74,932 KB
testcase_07 AC 152 ms
75,172 KB
testcase_08 AC 152 ms
74,996 KB
testcase_09 AC 153 ms
76,728 KB
testcase_10 AC 155 ms
75,872 KB
testcase_11 AC 162 ms
77,316 KB
testcase_12 AC 154 ms
75,912 KB
testcase_13 AC 158 ms
81,248 KB
testcase_14 AC 156 ms
77,216 KB
testcase_15 AC 154 ms
75,632 KB
testcase_16 AC 165 ms
75,892 KB
testcase_17 AC 154 ms
76,572 KB
testcase_18 AC 161 ms
90,400 KB
testcase_19 AC 153 ms
75,172 KB
testcase_20 AC 158 ms
75,140 KB
testcase_21 AC 162 ms
75,112 KB
testcase_22 AC 162 ms
75,220 KB
testcase_23 AC 162 ms
74,892 KB
testcase_24 AC 160 ms
74,892 KB
testcase_25 AC 152 ms
74,828 KB
testcase_26 AC 157 ms
75,120 KB
testcase_27 AC 156 ms
74,804 KB
testcase_28 AC 180 ms
74,976 KB
testcase_29 AC 174 ms
75,296 KB
testcase_30 AC 155 ms
74,972 KB
testcase_31 AC 148 ms
74,980 KB
testcase_32 AC 155 ms
75,032 KB
testcase_33 AC 153 ms
75,016 KB
testcase_34 AC 155 ms
74,912 KB
testcase_35 AC 150 ms
74,956 KB
testcase_36 AC 151 ms
75,296 KB
testcase_37 AC 152 ms
75,172 KB
testcase_38 AC 158 ms
74,936 KB
testcase_39 AC 152 ms
74,984 KB
testcase_40 AC 148 ms
74,884 KB
testcase_41 AC 154 ms
75,192 KB
testcase_42 AC 152 ms
75,012 KB
testcase_43 AC 151 ms
75,124 KB
testcase_44 AC 150 ms
75,092 KB
testcase_45 AC 148 ms
74,924 KB
testcase_46 AC 151 ms
75,048 KB
testcase_47 AC 148 ms
75,048 KB
testcase_48 AC 148 ms
74,952 KB
testcase_49 AC 169 ms
91,036 KB
testcase_50 AC 152 ms
75,588 KB
testcase_51 AC 157 ms
75,768 KB
testcase_52 AC 155 ms
77,152 KB
testcase_53 AC 153 ms
75,772 KB
testcase_54 AC 155 ms
77,408 KB
testcase_55 AC 156 ms
75,100 KB
testcase_56 AC 156 ms
75,720 KB
testcase_57 AC 155 ms
80,884 KB
testcase_58 AC 165 ms
86,312 KB
testcase_59 AC 162 ms
83,496 KB
testcase_60 AC 161 ms
78,276 KB
testcase_61 AC 173 ms
92,984 KB
testcase_62 AC 157 ms
78,312 KB
testcase_63 AC 164 ms
86,668 KB
testcase_64 AC 155 ms
77,028 KB
testcase_65 AC 146 ms
76,500 KB
testcase_66 AC 168 ms
79,912 KB
testcase_67 AC 158 ms
77,504 KB
testcase_68 AC 152 ms
77,560 KB
testcase_69 AC 150 ms
75,076 KB
testcase_70 AC 153 ms
74,920 KB
testcase_71 AC 156 ms
74,896 KB
testcase_72 AC 160 ms
75,148 KB
testcase_73 AC 148 ms
74,832 KB
testcase_74 AC 155 ms
75,056 KB
testcase_75 AC 152 ms
75,044 KB
testcase_76 AC 158 ms
74,960 KB
testcase_77 AC 161 ms
74,780 KB
testcase_78 AC 151 ms
74,876 KB
testcase_79 AC 151 ms
75,068 KB
testcase_80 AC 159 ms
75,076 KB
testcase_81 AC 162 ms
75,012 KB
testcase_82 AC 158 ms
74,976 KB
testcase_83 AC 151 ms
75,156 KB
testcase_84 AC 152 ms
75,028 KB
testcase_85 AC 151 ms
74,840 KB
testcase_86 AC 154 ms
75,092 KB
testcase_87 AC 157 ms
74,904 KB
testcase_88 AC 161 ms
75,000 KB
testcase_89 AC 157 ms
81,012 KB
testcase_90 AC 163 ms
82,960 KB
testcase_91 AC 177 ms
91,968 KB
testcase_92 AC 161 ms
76,560 KB
testcase_93 AC 165 ms
83,328 KB
testcase_94 AC 155 ms
75,320 KB
testcase_95 AC 160 ms
84,548 KB
testcase_96 AC 169 ms
81,852 KB
testcase_97 AC 161 ms
81,284 KB
testcase_98 AC 166 ms
77,500 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 ModInt
  @@MOD = 1_000_000_007i64

  def self.mod
    @@MOD
  end

  def self.zero
    ModInt.new(0)
  end

  def initialize(n)
    @n = n.to_i64 % @@MOD
  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 : Int)
    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

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

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

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

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

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

  def fact(n)
    @fac[n]
  end
end

comb = Comb(ModInt).new

n = read_line.to_i
a = read_line.split.map(&.to_i)

if a.sum == 0
  puts 1
elsif (ans = comb.h(n - a.sum - a.size + 2, a.size)) != 0
  puts ans
else
  puts "NA"
end
0