結果

問題 No.64 XORフィボナッチ数列
ユーザー nobigomunobigomu
提出日時 2018-03-05 18:56:02
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,109 bytes
コンパイル時間 114 ms
コンパイル使用メモリ 5,156 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-11 13:24:27
合計ジャッジ時間 881 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,356 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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