結果

問題 No.1646 Avoid Palindrome
ユーザー 👑 obakyanobakyan
提出日時 2021-08-13 22:12:53
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 2,296 bytes
コンパイル時間 526 ms
コンパイル使用メモリ 7,196 KB
実行使用メモリ 14,008 KB
最終ジャッジ日時 2024-04-14 18:18:58
合計ジャッジ時間 36,287 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,012 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 AC 2,973 ms
6,940 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 AC 2,935 ms
6,940 KB
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mod = 998244353
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 n = io.read("*n", "*l")
local s = io.read()
local t = {}
for i = 1, n do
  t[i] = s:sub(i, i)
end
for i = 1, n - 1 do
  if t[i] == t[i + 1] and t[i] ~= "?" then
    print(0) os.exit()
  end
end
for i = 1, n - 2 do
  if t[i] == t[i + 2] and t[i] ~= "?" then
    print(0) os.exit()
  end
end
if n == 1 then
  print(t[1] == "?" and 26 or 1) os.exit()
end
local dp1, dp2 = {}, {}
if t[1] == "?" and t[2] == "?" then
  for i = 1, 26 do for j = 1, 26 do
    local v = (i - 1) * 26 + j
    dp1[v] = i == j and 0 or 1
  end end
elseif t[1] == "?" then
  local i2 = t[2]:byte() - 96
  for i = 1, 26 do for j = 1, 26 do
    local v = (i - 1) * 26 + j
    if i == i2 or j ~= i2 then
      dp1[v] = 0
    else
      dp1[v] = 1
    end
  end end
elseif t[2] == "?" then
  local i1 = t[1]:byte() - 96
  for i = 1, 26 do for j = 1, 26 do
    local v = (i - 1) * 26 + j
    if i ~= i1 or j == i1 then
      dp1[v] = 0
    else
      dp1[v] = 1
    end
  end end
else
  local i1 = t[1]:byte() - 96
  local i2 = t[2]:byte() - 96
  for i = 1, 26 do for j = 1, 26 do
    local v = (i - 1) * 26 + j
    if i ~= i1 or j ~= i2 then
      dp1[v] = 0
    else
      dp1[v] = 1
    end
  end end
end
for i = 3, n do
  local src = i % 2 == 1 and dp1 or dp2
  local dst = i % 2 == 1 and dp2 or dp1
  for j = 1, 676 do dst[j] = 0 end
  for prv_state = 1, 676 do
    local srcval = src[prv_state]
    local prvprv = mce(prv_state / 26)
    local prv = prv_state - (prvprv - 1) * 26
    if t[i] == "?" then
      for j = 1, 26 do
        if j ~= prvprv and j ~= prv then
          local d = (prv - 1) * 26 + j
          dst[d] = badd(dst[d], srcval)
        end
      end
    else
      local z = t[i]:byte() - 96
      for j = 1, 26 do
        if j == z and j ~= prvprv and j ~= prv then
          local d = (prv - 1) * 26 + j
          dst[d] = badd(dst[d], srcval)
        end
      end
    end
  end
end
local tbl = n % 2 == 1 and dp2 or dp1
local ret = 0
for i = 1, 676 do
  ret = badd(ret, tbl[i])
end
print(ret)
0