結果

問題 No.2144 MM
ユーザー とりゐ
提出日時 2022-12-02 22:23:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 752 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 107,292 KB
最終ジャッジ日時 2024-10-09 23:54:32
合計ジャッジ時間 4,411 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 WA * 5 RE * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

# O(N*M)

n,m=map(int,input().split())
a=list(map(int,input().split()))
odd=0
even=0
for i in range(n):
  if i%2==0:
    odd+=a[i]
  else:
    even+=a[i]
if (odd-even)%m!=0:
  print(-1)
  exit()
if n*m>=5*10**7:
  raise Exception

mod=998244353
M=m-1
powM=[1]

for i in range(n+10):
  powM.append(powM[-1]*M%mod)

ans=0
res=0
for i in range(n):
  ai=a[i]
  k=n-1-i
  x=powM[k]
  if k%2==0:
    y=(x-1)*pow(m,mod-2,mod)
    y%=mod
  else:
    y=(x+1)*pow(m,mod-2,mod)
    y%=mod
  for j in range(ai):
    if k%2==0:
      if (res+j)%m==0:
        ans+=y+1
      else:
        ans+=y
    else:
      if (res-j+1)%m==0:
        ans+=y-1
      else:
        ans+=y
  if i%2==0:
    res+=ai
  else:
    res-=ai
  ans%=mod
  #print(x,y)
ans+=1
print(ans%mod)
0