結果
| 問題 |
No.64 XORフィボナッチ数列
|
| コンテスト | |
| ユーザー |
nobigomu
|
| 提出日時 | 2018-03-05 18:56:02 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,109 bytes |
| コンパイル時間 | 300 ms |
| コンパイル使用メモリ | 6,812 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-13 12:09:03 |
| 合計ジャッジ時間 | 954 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 11 |
ソースコード
local ffi = require 'ffi'
local C = ffi.C
ffi.cdef [[
long atol(const char *str);
int printf(const char *fmt, ...);
]]
function split(s, p, f)
local t = {}
for e in s:gmatch(p) do
table.insert(t, f(e))
end
return unpack(t)
end
local zero, one = ffi.new("long", 0), ffi.new("long", 1)
local two, three = one + one, one + one + one
function ltob(n)
if n == zero then return "0" end
local s = ""
while n > zero do
s = s .. (n%two ~= zero and "1" or "0")
n = n / two
end
return s
end
function lxor(n1, n2)
local s1, s2 = ltob(n1), ltob(n2)
local n3, i, p = ffi.new("long", 0), 1, ffi.new("long", 1)
while true do
local c1, c2 = s1:sub(i,i), s2:sub(i,i)
if c1 == "" or c2 == "" then break end
n3 = n3 + p * (c1 ~= c2 and one or zero)
i, p = i + 1, p * two
end
(#s1 > #s2 and s1 or #s1 < #s2 and s2 or ""):sub(i):gsub(".", function (e)
n3 = n3 + p * (e == "1" and one or zero)
p = p * two
end)
return n3
end
local f0, f1, n = split(io.stdin:read("*l"), "%d+", C.atol)
local f2 = lxor(f0, f1)
C.printf("%ld\n", n % three == zero and f0 or n % three == one and f1 or f2)
nobigomu