結果

問題 No.1036 Make One With GCD 2
ユーザー 👑 obakyanobakyan
提出日時 2021-08-02 22:29:52
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 1,562 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 5,228 KB
実行使用メモリ 122,188 KB
最終ジャッジ日時 2023-10-14 20:57:46
合計ジャッジ時間 15,616 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 328 ms
44,680 KB
testcase_01 AC 494 ms
65,048 KB
testcase_02 AC 341 ms
52,444 KB
testcase_03 AC 88 ms
10,436 KB
testcase_04 AC 148 ms
17,224 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 178 ms
23,072 KB
testcase_08 AC 141 ms
19,572 KB
testcase_09 AC 1,809 ms
74,732 KB
testcase_10 AC 1,677 ms
71,332 KB
testcase_11 AC 1,896 ms
75,760 KB
testcase_12 AC 1,771 ms
72,032 KB
testcase_13 TLE -
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 ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
long long atoll(const char*);
]]

local function lltonumber(str)
  return C.atoll(str)
end

local function getgcd(x, y)
  while 0LL < x do
    x, y = y % x, x
  end
  return y
end

local n = io.read("*n", "*l")
local a = {}
local str = io.read()
for w in str:gmatch("%d+") do
  table.insert(a, lltonumber(w))
end
a[n + 1] = 1LL

local suf_idx = {}
local pre_sum, suf_sum = {}, {}
local pre_idx_len, suf_idx_len = 0, 0

local function push(idx)
  suf_idx_len = suf_idx_len + 1
  suf_idx[suf_idx_len] = idx
  if suf_idx_len == 1 then
    suf_sum[suf_idx_len] = a[idx]
  else
    suf_sum[suf_idx_len] = getgcd(suf_sum[suf_idx_len - 1], a[idx])
  end
end
local function pop()
  if pre_idx_len == 0 then
    pre_sum[1] = a[suf_idx[suf_idx_len]]
    for i = 2, suf_idx_len - 1 do
      local j = suf_idx[suf_idx_len + 1 - i]
      pre_sum[i] = getgcd(pre_sum[i - 1], a[j])
    end
    pre_idx_len = suf_idx_len - 1
    suf_idx_len = 0
  else
    pre_idx_len = pre_idx_len - 1
  end
end
local function query()
  if pre_idx_len == 0 then
    return suf_sum[suf_idx_len]
  elseif suf_idx_len == 0 then
    return pre_sum[pre_idx_len]
  else
    return getgcd(suf_sum[suf_idx_len], pre_sum[pre_idx_len])
  end
end

local ret = 0
local cur = a[1]
local right = 1
push(1)
for i = 1, n do
  if right < i then
    push(i)
    right = i
    cur = a[i]
  end
  cur = query()
  while 1LL < cur do
    right = right + 1
    push(right)
    cur = getgcd(cur, a[right])
  end
  ret = ret + n + 1 - right
  pop()
end
print(ret)
0