結果

問題 No.1259 スイッチ
ユーザー simansiman
提出日時 2023-06-29 11:44:27
言語 Ruby
(3.3.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 580 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 11,452 KB
実行使用メモリ 23,156 KB
最終ジャッジ日時 2023-09-20 07:08:45
合計ジャッジ時間 14,598 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,048 KB
testcase_01 AC 83 ms
15,156 KB
testcase_02 AC 83 ms
15,088 KB
testcase_03 AC 83 ms
15,124 KB
testcase_04 AC 83 ms
15,308 KB
testcase_05 AC 85 ms
15,056 KB
testcase_06 AC 84 ms
15,196 KB
testcase_07 AC 84 ms
15,092 KB
testcase_08 AC 83 ms
15,112 KB
testcase_09 AC 83 ms
15,220 KB
testcase_10 AC 202 ms
19,940 KB
testcase_11 AC 266 ms
22,608 KB
testcase_12 AC 197 ms
19,592 KB
testcase_13 AC 150 ms
17,756 KB
testcase_14 AC 181 ms
18,796 KB
testcase_15 AC 147 ms
17,636 KB
testcase_16 AC 234 ms
21,204 KB
testcase_17 AC 174 ms
18,800 KB
testcase_18 AC 257 ms
21,996 KB
testcase_19 AC 255 ms
22,172 KB
testcase_20 AC 203 ms
20,048 KB
testcase_21 AC 274 ms
22,472 KB
testcase_22 AC 141 ms
17,488 KB
testcase_23 AC 251 ms
21,864 KB
testcase_24 AC 210 ms
20,012 KB
testcase_25 AC 208 ms
20,068 KB
testcase_26 AC 274 ms
22,788 KB
testcase_27 AC 243 ms
21,112 KB
testcase_28 AC 204 ms
20,132 KB
testcase_29 AC 237 ms
21,136 KB
testcase_30 AC 178 ms
18,980 KB
testcase_31 AC 224 ms
20,588 KB
testcase_32 AC 142 ms
17,496 KB
testcase_33 AC 276 ms
22,964 KB
testcase_34 AC 186 ms
19,436 KB
testcase_35 AC 229 ms
21,116 KB
testcase_36 AC 219 ms
20,344 KB
testcase_37 AC 213 ms
20,012 KB
testcase_38 AC 201 ms
19,784 KB
testcase_39 AC 272 ms
22,264 KB
testcase_40 AC 266 ms
22,692 KB
testcase_41 AC 237 ms
21,272 KB
testcase_42 AC 273 ms
22,736 KB
testcase_43 AC 196 ms
19,520 KB
testcase_44 AC 263 ms
22,128 KB
testcase_45 AC 283 ms
23,156 KB
testcase_46 AC 191 ms
19,292 KB
testcase_47 AC 281 ms
22,712 KB
testcase_48 AC 180 ms
18,764 KB
testcase_49 AC 273 ms
22,576 KB
testcase_50 AC 192 ms
19,460 KB
testcase_51 AC 141 ms
17,616 KB
testcase_52 AC 269 ms
22,612 KB
testcase_53 AC 165 ms
18,424 KB
testcase_54 AC 265 ms
22,396 KB
testcase_55 AC 138 ms
17,328 KB
testcase_56 AC 174 ms
18,964 KB
testcase_57 AC 252 ms
21,812 KB
testcase_58 AC 202 ms
19,544 KB
testcase_59 AC 225 ms
20,792 KB
testcase_60 AC 281 ms
23,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Integer
  def mod_inverse(mod)
    self.pow(mod - 2, mod)
  end
end

N, K, M = gets.split.map(&:to_i)
MOD = 10 ** 9 + 7
ans = 0
memo = Array.new(N + 1, 0)
memo[0] = 1
val = 1
1.upto(N - 1) do |l|
  val *= (N - l)
  val %= MOD
  memo[l] = val
end

if M == 1
  1.upto(N) do |ls|
    next if K % ls != 0

    ans += memo[ls - 1] * N.pow(N - ls, MOD)
    ans %= MOD
  end
else
  ans = N.pow(N, MOD)

  1.upto(N) do |ls|
    next if K % ls != 0

    ans += -1 * memo[ls - 1] * N.pow(N - ls, MOD)
    ans %= MOD
  end

  ans *= (N - 1).mod_inverse(MOD)
  ans %= MOD
end

puts ans
0