結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー nebukuro09nebukuro09
提出日時 2018-07-27 22:28:42
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,117 bytes
コンパイル時間 803 ms
コンパイル使用メモリ 102,144 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 20:49:53
合計ジャッジ時間 2,127 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable long MOD = 10^^9 + 7;


void main() {
    auto N = readln.chomp.to!long;
    long[][] hoge = [[1, 1], [1, 0]];
    auto F1 = matpow(hoge, N)[0][0];
    auto F2 = matpow(hoge, N-1)[0][0];
    writeln(F1 * F2 % MOD);
}

long[][] matmul(long[][] m1, long[][] m2) {
    int n = m1.length.to!int;
    long[][] ret = new long[][](n, n);

    foreach (i; 0..n) foreach (j; 0..n) foreach (k; 0..n) {
        (ret[i][j] += m1[i][k] * m2[k][j] % MOD) %= MOD;
    }

    return ret;
}

long[][] matpow(long[][] m, long x) {
    int n = m.length.to!int;
    long[][] ret = new long[][](n, n);
    foreach (i; 0..n) ret[i][i] = 1;

    while (x) {
        if (x % 2) ret = matmul(ret, m);
        m = matmul(m, m);
        x /= 2;
    }

    return ret;
}

long powmod(long a, long x) {
    long ret = 1;
    while (x) {
        if (x & 1) ret = ret * a % MOD;
        a = a * a % MOD;
        x /= 2;
    }
    return ret;
}
0