結果
| 問題 | No.2219 Re:010 | 
| コンテスト | |
| ユーザー | 👑 | 
| 提出日時 | 2023-02-18 22:56:45 | 
| 言語 | Lua (LuaJit 2.1.1734355927) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 47 ms / 2,000 ms | 
| コード長 | 2,172 bytes | 
| コンパイル時間 | 382 ms | 
| コンパイル使用メモリ | 5,376 KB | 
| 実行使用メモリ | 13,312 KB | 
| 最終ジャッジ日時 | 2024-07-20 05:38:20 | 
| 合計ジャッジ時間 | 2,024 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 21 | 
ソースコード
local mod = 998244353
local mfl = math.floor
local function bmul(x, y)
  local x0, y0 = x % 31596, y % 31596
  local x1, y1 = mfl(x / 31596), mfl(y / 31596)
  return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end
local function bsub(x, y)
  return x < y and x - y + mod or x - y
end
local s = io.read()
local n = #s
local t = {}
for i = 1, n do
  t[i] = s:sub(i, i)
end
local sum0 = {}
local sum1 = {}
if t[1] == "0" then sum0[1] = 1 else sum0[1] = 0 end
if t[1] == "1" then sum1[1] = 1 else sum1[1] = 0 end
for i = 2, n do
  if t[i] == "0" then
    sum0[i] = sum0[i - 1] + 1
  else
    sum0[i] = sum0[i - 1]
  end
  if t[i] == "1" then
    sum1[i] = sum1[i - 1] + 1
  else
    sum1[i] = sum1[i - 1]
  end
end
local tot0 = sum0[n]
local tot1 = sum1[n]
local totq = n - tot0 - tot1
local p2m1 = {1}
for i = 2, 400000 do
  p2m1[i] = (p2m1[i - 1] * 2) % mod
end
local ans = 0
for i = 2, n - 1 do
  local left_0 = sum0[i - 1]
  local right_0 = tot0 - sum0[i]
  local left_1 = sum1[i - 1]
  local right_1 = tot1 - sum1[i]
  local left_q = i - 1 - left_0 - left_1
  local right_q = n - i - right_0 - right_1
  if t[i] == "1" then
    local v = bmul(left_0, right_0)
    v = bmul(v, p2m1[totq + 1])
    ans = badd(ans, v)
    if 0 < left_q then
      v = bmul(left_q, right_0)
      v = bmul(v, p2m1[totq])
      ans = badd(ans, v)
    end
    if 0 < right_q then
      v = bmul(left_0, right_q)
      v = bmul(v, p2m1[totq])
      ans = badd(ans, v)
    end
    if 0 < left_q * right_q then
      v = bmul(left_q, right_q)
      v = bmul(v, p2m1[totq - 1])
      ans = badd(ans, v)
    end
  elseif t[i] == "?" then
    local v = bmul(left_0, right_0)
    v = bmul(v, p2m1[totq])
    ans = badd(ans, v)
    if 0 < left_q then
      v = bmul(left_q, right_0)
      v = bmul(v, p2m1[totq - 1])
      ans = badd(ans, v)
    end
    if 0 < right_q then
      v = bmul(left_0, right_q)
      v = bmul(v, p2m1[totq - 1])
      ans = badd(ans, v)
    end
    if 0 < left_q * right_q then
      v = bmul(left_q, right_q)
      v = bmul(v, p2m1[totq - 2])
      ans = badd(ans, v)
    end
  end
end
print(ans)
            
            
            
        