結果
| 問題 | No.1036 Make One With GCD 2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-02 22:23:03 |
| 言語 | Lua (LuaJit 2.1.1765228720) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,683 bytes |
| 記録 | |
| コンパイル時間 | 231 ms |
| コンパイル使用メモリ | 6,688 KB |
| 実行使用メモリ | 85,392 KB |
| 最終ジャッジ日時 | 2024-09-16 14:45:56 |
| 合計ジャッジ時間 | 36,389 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 34 TLE * 7 |
ソースコード
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
local pre_idx, 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_idx[1] = suf_idx[suf_idx_len]
pre_sum[1] = a[pre_idx[1]]
for i = 2, suf_idx_len - 1 do
local j = suf_idx[suf_idx_len + 1 - i]
pre_idx[i] = j
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()
-- print(i, cur)
while right <= n and 1LL < cur do
right = right + 1
if right <= n then
push(right)
end
cur = query()
end
-- print(i, right)
ret = ret + n + 1 - right
pop()
end
print(ret)