結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー pianonekopianoneko
提出日時 2018-09-22 15:19:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,490 bytes
コンパイル時間 1,461 ms
コンパイル使用メモリ 119,320 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 11:04:19
合計ジャッジ時間 2,735 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>

/* 
PascalCase  クラス名
camelCase   メソッド名,関数名,変数名
snake_case  ファイル名,名前空間,std のメソッド名,変数名,型名に準じる場合
SNAKE_CASE	マクロ名,static const 定数,列挙体
*/

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for(int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (int)(obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second

using namespace std;
using lint = long long int;
using vvlint = vector<vector<lint>>;
using vlint = vector<lint>;
using vvint = vector<vector<int>>;
using vint = vector<int>;


const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

vvlint mul(vvlint &A, vvlint &B)
{
    vvlint C(Size(A), vlint(B[0].size()));

    REP(i, Size(A))
    {
        REP(j, Size(B))
        {
            REP(k, Size(B[0]))
            {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
            }
        }
    }

    return C;
}

vvlint pow(vvlint A, lint n)
{
    vvlint B(Size(A), vlint(Size(A)));

    REP(i, Size(A))
    {
        B[i][i] = 1;
    }

    while(n > 0)
    {
        if(n & 1) B = mul(B, A);

        A = mul(A, A);

        n >>= 1;
    }

    return B;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    vvlint A(2, vlint(2));
    A[0][0] = A[0][1] = A[1][0] = 1;
    A[1][1] = 0;

    lint N;
    cin >> N;

    lint n, n_1;

    A = pow(A, N);
    n = A[1][0];

    A[0][0] = A[0][1] = A[1][0] = 1;
    A[1][1] = 0;

    A = pow(A, N + 1);
    n_1 = A[1][0];

    cout << (n * n_1) % MOD << endl;

    return 0;
}
0