結果

問題 No.1516 simple 門松列 problem Re:MASTER
ユーザー googol_S0googol_S0
提出日時 2021-05-21 22:21:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,524 bytes
コンパイル時間 260 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 78,676 KB
最終ジャッジ日時 2024-10-10 09:06:10
合計ジャッジ時間 3,390 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,736 KB
testcase_01 AC 65 ms
70,016 KB
testcase_02 AC 177 ms
78,232 KB
testcase_03 AC 39 ms
52,992 KB
testcase_04 AC 43 ms
52,480 KB
testcase_05 AC 53 ms
63,616 KB
testcase_06 AC 121 ms
77,324 KB
testcase_07 AC 90 ms
76,288 KB
testcase_08 AC 146 ms
77,336 KB
testcase_09 AC 39 ms
52,864 KB
testcase_10 AC 49 ms
61,952 KB
testcase_11 AC 40 ms
53,536 KB
testcase_12 AC 40 ms
53,632 KB
testcase_13 AC 39 ms
53,120 KB
testcase_14 AC 186 ms
78,420 KB
testcase_15 AC 153 ms
77,184 KB
testcase_16 AC 137 ms
77,312 KB
testcase_17 AC 124 ms
77,524 KB
testcase_18 WA -
testcase_19 AC 60 ms
67,456 KB
testcase_20 AC 187 ms
78,476 KB
testcase_21 AC 189 ms
78,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K=map(int,input().split())
if K==3:
  if N==3:
    print(4,12)
  elif N==4:
    print(2,8)
  else:
    print(0,0)
  exit()
def m(a,b):
  r=[[0]*len(b[0]) for i in range(len(a))]
  for i in range(len(a)):
    for k in range(len(b)):
      for j in range(len(b[0])):
        r[i][j]=(r[i][j]+a[i][k]*b[k][j])%mod
  return r

def p(a,n):
  r=[[0]*len(a) for i in range(len(a))]
  b=[]
  for i in range(len(a)):
    r[i][i]=1
    b.append(a[i][:])
  l=n
  while l>0:
    if l&1:
      r=m(b,r)
    b=m(b,b)
    l>>=1
  return r
S='''1,998244352,1,0
1,998244350,4,0,4,0
2,998244348,12,998244346,24,998244350,18,0
2,998244346,22,998244341,88,4,152,16,96,0
3,998244341,44,998244303,254,998244269,734,998244310,1065,10,600,0
3,998244338,65,998244281,572,998244289,2620,452,6580,1284,8520,936,4320,0'''.split('\n')

T='''2,998244350,4,998244350,2,998244352,0
2,998244348,12,998244341,24,998244329,16,998244321,0,998244337,0
4,998244339,44,998244266,196,998244037,456,998243626,588,998243303,396,998243480,108,998244029,0
4,998244337,64,998244224,404,998243615,1240,998241293,1712,998235977,160,998230497,998242305,998231937,998242817,998239745,0
6,998244320,160,998243845,1864,998239525,12468,998212995,52796,998099443,146556,997763045,265176,997105553,296964,996375964,177710,996230188,30300,996966253,998232353,997884353,0
6,998244314,220,998243594,3526,998234408,32144,998148073,179736,997544001,598472,994441937,857496,982989745,996484017,953551505,986209729,904320145,969293153,860989553,960141953,866399105,971201153,923755457,990157313,979581953,0'''.split('\n')

mod=998244353
def solve1(N,K):
  DP1=[[[0]*K for i in range(K)] for j in range(N-1)]
  DP2=[[[0]*K for i in range(K)] for j in range(N-1)]
  for i in range(K):
    for j in range(K):
      if i!=j:
        DP1[0][i][j]=1
        DP2[0][i][j]=i+j
  for i in range(N-2):
    for j in range(K):
      for k in range(K):
        for l in range(K):
          if (min(j,k,l)==k or max(j,k,l)==k) and j!=l and k!=l:
            DP1[i+1][k][l]+=DP1[i][j][k]
            DP2[i+1][k][l]+=DP2[i][j][k]+DP1[i][j][k]*l
  ANS1,ANS2=0,0
  for i in range(K):
    for j in range(K):
      ANS1+=DP1[-1][i][j]
      ANS2+=DP2[-1][i][j]
  return ANS1

def solve2(N,K):
  DP1=[[[0]*K for i in range(K)] for j in range(N-1)]
  DP2=[[[0]*K for i in range(K)] for j in range(N-1)]
  for i in range(K):
    for j in range(K):
      if i!=j:
        DP1[0][i][j]=1
        DP2[0][i][j]=i+j
  for i in range(N-2):
    for j in range(K):
      for k in range(K):
        for l in range(K):
          if (min(j,k,l)==k or max(j,k,l)==k) and j!=l and k!=l:
            DP1[i+1][k][l]+=DP1[i][j][k]
            DP2[i+1][k][l]+=DP2[i][j][k]+DP1[i][j][k]*l
  ANS1,ANS2=0,0
  for i in range(K):
    for j in range(K):
      ANS1+=DP1[-1][i][j]
      ANS2+=DP2[-1][i][j]
  return ANS2

if N<=70:
  print(solve1(N,K)%mod,solve2(N,K)%mod)
  exit()
M=K-4
X=[list(map(int,S[i].split(','))) for i in range(6)]
Y=[list(map(int,T[i].split(','))) for i in range(6)]
A=[[0]*len(X[M]) for i in range(len(X[M]))]
for i in range(len(X[M])):
  A[i][-1]=solve1(len(X[M])-i+2,K)
B=[[0]*len(X[M]) for i in range(len(X[M]))]
for i in range(len(X[M])-1):
  B[i+1][i]=1
for i in range(len(X[M])):
  B[0][i]=X[M][i]
ANS1=m(p(B,N-3),A)[-1][-1]
A=[[0]*len(Y[M]) for i in range(len(Y[M]))]
for i in range(len(Y[M])):
  A[i][-1]=solve2(len(Y[M])-i+2,K)
B=[[0]*len(Y[M]) for i in range(len(Y[M]))]
for i in range(len(Y[M])-1):
  B[i+1][i]=1
for i in range(len(Y[M])):
  B[0][i]=Y[M][i]
print(ANS1,m(p(B,N-3),A)[-1][-1])
0