結果

問題 No.1259 スイッチ
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-06-18 13:54:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 980 ms / 2,000 ms
コード長 1,417 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 87,000 KB
実行使用メモリ 259,560 KB
最終ジャッジ日時 2023-09-04 06:41:22
合計ジャッジ時間 29,354 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,000 KB
testcase_01 AC 74 ms
71,380 KB
testcase_02 AC 73 ms
71,560 KB
testcase_03 AC 73 ms
71,252 KB
testcase_04 AC 73 ms
71,432 KB
testcase_05 AC 73 ms
71,220 KB
testcase_06 AC 74 ms
71,408 KB
testcase_07 AC 73 ms
71,248 KB
testcase_08 AC 73 ms
71,368 KB
testcase_09 AC 74 ms
71,224 KB
testcase_10 AC 624 ms
203,812 KB
testcase_11 AC 915 ms
259,516 KB
testcase_12 AC 592 ms
203,016 KB
testcase_13 AC 387 ms
145,436 KB
testcase_14 AC 510 ms
176,428 KB
testcase_15 AC 375 ms
145,568 KB
testcase_16 AC 769 ms
238,648 KB
testcase_17 AC 496 ms
176,120 KB
testcase_18 AC 859 ms
259,560 KB
testcase_19 AC 862 ms
259,388 KB
testcase_20 AC 631 ms
203,976 KB
testcase_21 AC 939 ms
259,224 KB
testcase_22 AC 349 ms
137,416 KB
testcase_23 AC 844 ms
259,256 KB
testcase_24 AC 662 ms
219,908 KB
testcase_25 AC 642 ms
219,092 KB
testcase_26 AC 940 ms
259,296 KB
testcase_27 AC 783 ms
257,928 KB
testcase_28 AC 638 ms
218,832 KB
testcase_29 AC 784 ms
257,368 KB
testcase_30 AC 187 ms
176,420 KB
testcase_31 AC 233 ms
236,940 KB
testcase_32 AC 147 ms
137,348 KB
testcase_33 AC 284 ms
259,000 KB
testcase_34 AC 201 ms
188,868 KB
testcase_35 AC 249 ms
237,880 KB
testcase_36 AC 237 ms
219,752 KB
testcase_37 AC 233 ms
219,176 KB
testcase_38 AC 216 ms
203,096 KB
testcase_39 AC 293 ms
259,252 KB
testcase_40 AC 290 ms
259,136 KB
testcase_41 AC 245 ms
238,116 KB
testcase_42 AC 283 ms
259,260 KB
testcase_43 AC 212 ms
202,648 KB
testcase_44 AC 274 ms
259,212 KB
testcase_45 AC 297 ms
259,316 KB
testcase_46 AC 196 ms
189,104 KB
testcase_47 AC 288 ms
259,256 KB
testcase_48 AC 188 ms
175,692 KB
testcase_49 AC 284 ms
259,260 KB
testcase_50 AC 556 ms
189,496 KB
testcase_51 AC 345 ms
137,636 KB
testcase_52 AC 907 ms
259,140 KB
testcase_53 AC 452 ms
164,600 KB
testcase_54 AC 907 ms
259,232 KB
testcase_55 AC 336 ms
137,152 KB
testcase_56 AC 501 ms
176,132 KB
testcase_57 AC 847 ms
259,272 KB
testcase_58 AC 607 ms
203,360 KB
testcase_59 AC 725 ms
237,832 KB
testcase_60 AC 980 ms
259,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,k,m=map(int,input().split())
mod=10**9+7
# 閉路の長さとしてlがありうるとすると。
# cmb(n-1,l-1)*(l-1)! * n**(n-l)が加算される。
# A1以外のl-1個の選び方とその並べ方。残りn-l個のスイッチの取り方。
mod=10**9+7
def cmb(n,r,mod=mod):
  if (r<0 or r>n):
    return 0
  r=min(r,n-r)
  return (g1[n]*g2[r]*g2[n-r])%mod
g1=[1,1] # g1[i]=i! % mod :階乗
g2=[1,1] # g2[i]=(i!)^(-1) % mod :階乗の逆元
inverse=[0,1]
for i in range(2,n+1):
  g1.append((g1[-1]*i)%mod)
  inverse.append((-inverse[mod%i]*(mod//i))%mod)
  g2.append((g2[-1]*inverse[-1])%mod)
if m==1:
  ans=0
  for i in range(1,n+1):# 閉路の長さとしてiを考える
    if k%i==0:
      ans+=cmb(n-1,i-1) * g1[i-1] * pow(n,n-i,mod) % mod
      ans%=mod
  print(ans)
else:
  ans=0
  for x in range(2,n+1):# 1から到達可能な点の個数
    if k%x!=0:
      # 1,mを含むx個の点の選び方。ループの形状の個数。1,m以外の点の並び方。残りn-x個の決め方
      ans+=cmb(n-2,x-2) * x * g1[x-2] * pow(n,n-x,mod) % mod
      ans%=mod
    else:
      # 1,mを含むx個の点の選び方。ループの形状の個数(しっぽなしの閉路はダメ。しっぽなしだとK回目の操作で1になるから)
      # 1,m以外の点の並び方。残りn-x個の決め方
      ans+=cmb(n-2,x-2) * (x-1) * g1[x-2] * pow(n,n-x,mod) % mod
      ans%=mod
  print(ans)

0