結果

問題 No.2144 MM
ユーザー とりゐ
提出日時 2022-12-02 22:45:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 911 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 120,320 KB
最終ジャッジ日時 2024-10-10 01:00:50
合計ジャッジ時間 4,539 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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()

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

for i in range(n+10):
  powM.append(powM[-1]*M%mod)
invm=pow(m,mod-2,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)*invm
    y%=mod
  else:
    y=(x+1)*invm
    y%=mod
  sgn=1
  if i%2==1:
    sgn=-1
  """
  for j in range(ai):
    if k%2==0:
      if (sgn*res+j)%m==0:
        ans+=y+1
      else:
        ans+=y
    else:
      if (sgn*res+j)%m==m-1:
        ans+=y-1
      else:
        ans+=y
  """
  if k%2==0:
    ans+=y*ai
    J=(-sgn*res)%m
    if J<ai:
      ans+=1
  else:
    ans+=y*ai
    J=(m-1-sgn*res)%m
    if J<ai:
      ans-=1
  
  if i%2==0:
    res+=ai
  else:
    res-=ai
  ans%=mod
  #print(x,y)
ans+=1
print(ans%mod)
0