結果

問題 No.1036 Make One With GCD 2
ユーザー 👑 obakyanobakyan
提出日時 2020-05-01 00:17:00
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 1,977 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 4,892 KB
実行使用メモリ 358,184 KB
最終ジャッジ日時 2023-08-22 21:47:27
合計ジャッジ時間 13,292 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,242 ms
317,448 KB
testcase_01 AC 566 ms
129,940 KB
testcase_02 AC 522 ms
124,332 KB
testcase_03 AC 139 ms
49,832 KB
testcase_04 AC 250 ms
99,700 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 254 ms
59,940 KB
testcase_08 AC 214 ms
56,968 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
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 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local ffi = require("ffi")
local C = ffi.C

local function getgcd(x, y)
  local z = 0LL
  if x < y then
    while 0LL < x do
      if y - x < x then
        x, y = y - x, x
      else
        x, y = y % x, x
      end
    end
    return y
  else
    while 0LL < y do
      if x - y < y then
        y, x = x - y, y
      else
        y, x = x % y, y
      end
    end
    return x
  end
end

local n = io.read("*n", "*l")
ffi.cdef[[
long long atoll(const char*);
]]
local roundcnt = 1
do
  local mul = 1
  while mul < n do
    roundcnt = roundcnt + 1
    mul = mul * 2
  end
end
local len = {}
do
  local tmp, c = 1, 1
  for i = 1, n + 1 do
    if tmp < i then tmp, c = tmp * 2, c + 1 end
    len[i] = c
  end
end

local dp = {}
for i = 1, roundcnt do
  dp[i] = {}
  for j = 1, n do
    dp[i][j] = 0
  end
end
local s = io.read()
do
  local i = 1
  for w in s:gmatch("%d+") do
    dp[1][i] = C.atoll(w)
    i = i + 1
  end
end
-- print(os.clock())
for i = 2, roundcnt do
  local jump = bls(1, i - 2)
  for j = 1, n do
    local dst = j + jump
    if dst <= n then
      dp[i][j] = getgcd(dp[i - 1][j], dp[i - 1][dst])
    else
      dp[i][j] = 1LL
    end
  end
end
-- print(os.clock())

local function solvesub(l, r)
end
local function solve(l, r)
  if dp[1][l] == 1LL then return l end
  local ret = l
  local cur = 0LL
  local tmp = 0LL
  while true do
    -- print(cur, l, r)
    local stage = len[1 + r - l + 1] - 1
    tmp = getgcd(cur, dp[stage][l])
    if tmp == 1LL then
      if l == r then break end
      r = l + bls(1, stage - 1) - 2
    else
      cur = tmp
      ret = l + bls(1, stage - 1) - 1
      l = l + bls(1, stage - 1)
      if r < l then break end
    end
  end
  return ret + 1
end

local ret = 0
local z = n
for i = n, 1, -1 do
  local p = solve(i, z)
  ret = ret + mma(0, n + 1 - p)
  z = mmi(p, n)
end
print(ret)
-- print(os.clock())
0