結果

問題 No.2144 MM
ユーザー とりゐとりゐ
提出日時 2022-12-02 22:45:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 108 ms / 2,000 ms
コード長 911 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 120,600 KB
最終ジャッジ日時 2024-04-18 07:48:48
合計ジャッジ時間 4,080 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,096 KB
testcase_01 AC 34 ms
52,224 KB
testcase_02 AC 36 ms
52,204 KB
testcase_03 AC 34 ms
52,608 KB
testcase_04 AC 35 ms
51,968 KB
testcase_05 AC 36 ms
52,608 KB
testcase_06 AC 35 ms
52,352 KB
testcase_07 AC 78 ms
103,040 KB
testcase_08 AC 108 ms
120,336 KB
testcase_09 AC 77 ms
103,388 KB
testcase_10 AC 108 ms
120,600 KB
testcase_11 AC 103 ms
120,568 KB
testcase_12 AC 106 ms
120,180 KB
testcase_13 AC 104 ms
120,180 KB
testcase_14 AC 105 ms
120,356 KB
testcase_15 AC 105 ms
113,488 KB
testcase_16 AC 66 ms
79,060 KB
testcase_17 AC 96 ms
109,780 KB
testcase_18 AC 51 ms
72,572 KB
testcase_19 AC 102 ms
110,152 KB
testcase_20 AC 98 ms
106,040 KB
testcase_21 AC 77 ms
93,852 KB
testcase_22 AC 59 ms
71,680 KB
testcase_23 AC 90 ms
106,368 KB
testcase_24 AC 59 ms
76,668 KB
testcase_25 AC 37 ms
52,480 KB
testcase_26 AC 38 ms
51,840 KB
testcase_27 AC 35 ms
51,968 KB
testcase_28 AC 36 ms
52,736 KB
testcase_29 AC 36 ms
52,224 KB
testcase_30 AC 38 ms
52,608 KB
testcase_31 AC 35 ms
52,352 KB
testcase_32 AC 37 ms
53,376 KB
testcase_33 AC 34 ms
52,224 KB
testcase_34 AC 37 ms
52,352 KB
testcase_35 AC 36 ms
52,608 KB
testcase_36 AC 36 ms
52,096 KB
testcase_37 AC 35 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

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