結果

問題 No.2653 [Cherry 6th Tune] Re: start! (Make it Zero!)
ユーザー tomeruntomerun
提出日時 2024-02-23 23:30:48
言語 Crystal
(1.11.2)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 928 bytes
コンパイル時間 14,739 ms
コンパイル使用メモリ 292,664 KB
実行使用メモリ 19,032 KB
最終ジャッジ日時 2024-02-23 23:31:10
合計ジャッジ時間 18,948 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 33 ms
6,676 KB
testcase_02 AC 25 ms
6,676 KB
testcase_03 AC 24 ms
6,676 KB
testcase_04 AC 30 ms
11,648 KB
testcase_05 AC 26 ms
6,676 KB
testcase_06 AC 103 ms
6,676 KB
testcase_07 AC 104 ms
6,676 KB
testcase_08 AC 31 ms
16,896 KB
testcase_09 AC 33 ms
17,952 KB
testcase_10 AC 32 ms
16,568 KB
testcase_11 AC 34 ms
17,400 KB
testcase_12 AC 38 ms
17,948 KB
testcase_13 AC 32 ms
6,676 KB
testcase_14 AC 33 ms
6,676 KB
testcase_15 AC 33 ms
6,676 KB
testcase_16 AC 33 ms
6,676 KB
testcase_17 AC 32 ms
6,676 KB
testcase_18 AC 34 ms
6,676 KB
testcase_19 AC 33 ms
6,676 KB
testcase_20 AC 32 ms
6,676 KB
testcase_21 AC 32 ms
6,676 KB
testcase_22 AC 33 ms
6,676 KB
testcase_23 AC 25 ms
6,676 KB
testcase_24 AC 24 ms
6,676 KB
testcase_25 AC 24 ms
6,676 KB
testcase_26 AC 24 ms
6,676 KB
testcase_27 AC 25 ms
6,676 KB
testcase_28 AC 24 ms
6,676 KB
testcase_29 AC 24 ms
6,676 KB
testcase_30 AC 23 ms
6,676 KB
testcase_31 AC 24 ms
6,676 KB
testcase_32 AC 24 ms
6,676 KB
testcase_33 AC 27 ms
7,552 KB
testcase_34 AC 28 ms
9,216 KB
testcase_35 AC 27 ms
7,680 KB
testcase_36 AC 28 ms
9,984 KB
testcase_37 AC 28 ms
9,728 KB
testcase_38 AC 29 ms
10,112 KB
testcase_39 AC 26 ms
8,448 KB
testcase_40 AC 29 ms
10,000 KB
testcase_41 AC 26 ms
8,448 KB
testcase_42 AC 28 ms
12,404 KB
testcase_43 AC 102 ms
6,676 KB
testcase_44 AC 102 ms
6,676 KB
testcase_45 AC 102 ms
6,676 KB
testcase_46 AC 102 ms
6,676 KB
testcase_47 AC 104 ms
6,676 KB
testcase_48 AC 104 ms
6,676 KB
testcase_49 AC 104 ms
6,676 KB
testcase_50 AC 103 ms
6,676 KB
testcase_51 AC 103 ms
6,676 KB
testcase_52 AC 103 ms
6,676 KB
testcase_53 AC 31 ms
15,852 KB
testcase_54 AC 31 ms
16,476 KB
testcase_55 AC 36 ms
19,032 KB
testcase_56 AC 30 ms
15,724 KB
testcase_57 AC 31 ms
14,360 KB
testcase_58 AC 36 ms
18,948 KB
testcase_59 AC 36 ms
18,316 KB
testcase_60 AC 30 ms
15,860 KB
testcase_61 AC 32 ms
18,920 KB
testcase_62 AC 31 ms
16,580 KB
testcase_63 AC 31 ms
6,676 KB
testcase_64 AC 32 ms
6,676 KB
testcase_65 AC 31 ms
6,676 KB
testcase_66 AC 31 ms
6,676 KB
testcase_67 AC 32 ms
6,676 KB
testcase_68 AC 32 ms
6,676 KB
testcase_69 AC 31 ms
6,676 KB
testcase_70 AC 31 ms
6,676 KB
testcase_71 AC 31 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

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