結果

問題 No.2653 [Cherry 6th Tune] Re: start! (Make it Zero!)
ユーザー tomerun
提出日時 2024-02-23 23:28:35
言語 Crystal
(1.14.0)
結果
WA  
実行時間 -
コード長 893 bytes
コンパイル時間 14,134 ms
コンパイル使用メモリ 296,988 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-09-29 09:00:35
合計ジャッジ時間 20,330 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 65
権限があれば一括ダウンロードができます

ソースコード

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
  while i < last
    if x[i] == -1
      ans = ans * m + comb
      ans %= MOD
      comb *= m
      comb %= MOD
    else
      ans += comb
      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