結果

問題 No.1036 Make One With GCD 2
ユーザー 👑 obakyanobakyan
提出日時 2021-08-02 22:23:03
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 1,683 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 5,156 KB
実行使用メモリ 118,744 KB
最終ジャッジ日時 2023-10-14 20:56:44
合計ジャッジ時間 42,754 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 398 ms
55,968 KB
testcase_01 AC 466 ms
49,340 KB
testcase_02 AC 369 ms
56,532 KB
testcase_03 AC 90 ms
10,500 KB
testcase_04 AC 162 ms
17,280 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 199 ms
19,640 KB
testcase_08 AC 144 ms
16,948 KB
testcase_09 AC 1,599 ms
74,724 KB
testcase_10 AC 1,449 ms
71,332 KB
testcase_11 AC 1,634 ms
75,780 KB
testcase_12 AC 1,463 ms
71,712 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 3 ms
4,352 KB
testcase_19 AC 4 ms
4,352 KB
testcase_20 AC 5 ms
4,352 KB
testcase_21 AC 5 ms
4,352 KB
testcase_22 TLE -
testcase_23 AC 1,595 ms
73,780 KB
testcase_24 TLE -
testcase_25 AC 1,932 ms
81,516 KB
testcase_26 TLE -
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 1 ms
4,356 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 3 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 3 ms
4,352 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 366 ms
56,216 KB
testcase_39 AC 380 ms
44,564 KB
testcase_40 AC 1,588 ms
73,632 KB
testcase_41 AC 1,037 ms
102,796 KB
testcase_42 AC 1,029 ms
102,632 KB
testcase_43 AC 958 ms
110,692 KB
testcase_44 AC 985 ms
106,516 KB
権限があれば一括ダウンロードができます

ソースコード

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

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)
0