結果

問題 No.2653 [Cherry 6th Tune] Re: start! (Make it Zero!)
ユーザー tomerun
提出日時 2024-02-23 23:30:48
言語 Crystal
(1.14.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 928 bytes
コンパイル時間 10,208 ms
コンパイル使用メモリ 297,444 KB
実行使用メモリ 17,920 KB
最終ジャッジ日時 2024-09-29 09:03:17
合計ジャッジ時間 15,502 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 72
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353i64

read_line.to_i.times do
  puts solve()
end

def solve
  n, m = read_line.split.map(&.to_i64)
  x = read_line.split.map(&.to_i64)
  if !x.includes?(-1)
    ans = n
    sum = 0
    n.times do |i|
      sum += x[i]
      sum %= m
      ans -= 1 if sum == 0
    end
    return sum == 0 ? ans : 0
  end
  last = n.times.select { |i| x[i] == -1 }.to_a.last
  ans = 0i64
  comb = 1i64
  sum = 0i64
  i = 0
  while x[i] != -1
    sum += x[i]
    sum %= m
    if sum == 0
      ans += 1
    end
    i += 1
  end
  pcomb = 0i64
  while i < last
    if x[i] == -1
      ans = ans * m + comb
      ans %= MOD
      pcomb = comb
      comb *= m
      comb %= MOD
    else
      ans += pcomb
      ans %= MOD
    end
    i += 1
  end
  sum = 0
  (n - 1).downto(last + 1) do |i|
    sum += x[i]
    sum %= m
    if sum == 0
      ans += comb
    end
  end
  ans %= MOD
  return (comb * (n - 1) % MOD - ans + MOD) % MOD
end
0