結果
問題 | No.64 XORフィボナッチ数列 |
ユーザー |
![]() |
提出日時 | 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.Cffi.cdef [[long atol(const char *str);int printf(const char *fmt, ...);]]function split(s, p, f)local t = {}for e in s:gmatch(p) dotable.insert(t, f(e))endreturn unpack(t)endlocal zero, one = ffi.new("long", 0), ffi.new("long", 1)local two, three = one + one, one + one + onefunction ltob(n)if n == zero then return "0" endlocal s = ""while n > zero dos = s .. (n%two ~= zero and "1" or "0")n = n / twoendreturn sendfunction 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 dolocal c1, c2 = s1:sub(i,i), s2:sub(i,i)if c1 == "" or c2 == "" then break endn3 = n3 + p * (c1 ~= c2 and one or zero)i, p = i + 1, p * twoend(#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 * twoend)return n3endlocal 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)