結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー 👑 obakyanobakyan
提出日時 2019-06-05 23:01:58
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,429 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 5,336 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-23 18:26:22
合計ジャッジ時間 1,879 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

local mod = 1000000007
local mfl = math.floor

local function bmul(x, y)
  local x1, y1 = x % 10000, y % 10000
  local x2, y2 = mfl(x / 10000) % 10000, mfl(y / 10000) % 10000
  local x3, y3 = mfl(x / 100000000), mfl(y / 100000000)
  local ret = (x1 * y1 + (x1 * y2 + x2 * y1) * 10000) % mod
  ret = (ret + (x1 * y3 + x2 * y2 + x3 * y1) * 10000 % mod * 10000 % mod) % mod
  ret = (ret + (x2 * y3 + x3 * y2) * 10000 % mod * 10000 % mod * 10000) % mod
  ret = (ret + x3 * y3 * 930000007) % mod
  return ret
end

local function modpow(src, pow)
  local res = 1
  while (0 < pow) do
    if (pow % 2 == 1) then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res;
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local function goldpow(z, r, pow)
  local resz, resr = 1, 0
  while (0 < pow) do
    if (pow % 2 == 1) then
      resz, resr = (bmul(resz, z) + 5 * bmul(resr, r)) % mod, (bmul(resz, r) + bmul(resr, z)) % mod
      pow = pow - 1
    end
    z, r = (bmul(z, z) + 5 * bmul(r, r)) % mod, bmul(2 * z, r)
    pow = mfl(pow / 2)
  end
  return resz;
end

local n = io.read("*n")
-- ans = (1/5){phi^(2n+1) + (1-phi)^(2n+1)+(-1)^(n+1)}
local a = goldpow(1, 1, 2 * n + 1)
local b = goldpow(1, -1, 2 * n + 1)
local c = modinv(modpow(2, 2 * n + 1))
local ret = bmul((a + b), c)
ret = ret + ((n % 2) * 2 - 1)
ret = bmul(ret, modinv(5))
print(ret)
0