結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー nebukuro09nebukuro09
提出日時 2016-12-26 17:23:42
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 2,337 bytes
コンパイル時間 1,549 ms
コンパイル使用メモリ 103,792 KB
実行使用メモリ 11,048 KB
最終ジャッジ日時 2023-09-03 00:13:52
合計ジャッジ時間 3,001 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 24 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 10 ms
4,380 KB
testcase_05 AC 7 ms
4,376 KB
testcase_06 AC 10 ms
4,380 KB
testcase_07 AC 16 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 12 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 20 ms
4,380 KB
testcase_16 AC 16 ms
4,376 KB
testcase_17 AC 6 ms
4,376 KB
testcase_18 AC 18 ms
4,380 KB
testcase_19 AC 24 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 17 ms
11,048 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 8 ms
6,724 KB
testcase_25 AC 7 ms
6,164 KB
testcase_26 AC 7 ms
6,192 KB
testcase_27 AC 8 ms
7,172 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 13 ms
10,536 KB
testcase_30 AC 22 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 7 ms
4,380 KB
testcase_33 AC 11 ms
4,376 KB
testcase_34 AC 9 ms
4,380 KB
testcase_35 AC 8 ms
4,376 KB
testcase_36 AC 19 ms
4,376 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 20 ms
4,376 KB
testcase_39 AC 8 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;


immutable long MOD = 10^^9 + 7;
int N;
long K;
long[] A, B;
 
long[][] matmul(int N, long[][] m1, long[][] m2) {
    auto ret = new long[][](N, N);
    foreach (i; 0..N) {
        foreach (j; 0..N) {
            ret[i][j] = 0;
            foreach (k; 0..N) {
                (ret[i][j] += m1[i][k] * m2[k][j] % MOD) %= MOD;
            }
        }
    }
    return ret;
}

long[][] matpow(int N, long K, long[][] mat) {
    auto ret = new long[][](N, N);
    foreach (i; 0..N)
        foreach (j; 0..N)
            ret[i][j] = i == j ? 1 : 0;
    
    while (K > 0) {
        if (K % 2 == 1)
            ret = matmul(N, ret, mat);
        mat = matmul(N, mat, mat);
        K /= 2;
    }

    return ret;
}

void solve1() {
    // N <= 30
    
    auto F_mat = new long[][](N, N);
    foreach (i; 0..N)
        foreach (j; 0..N)
            F_mat[i][j] = (i == 0 || i == j+1) ? 1 : 0;
    F_mat = matpow(N, K-N, F_mat);
    
    long F = 0;
    foreach (i; 0..N)
        (F += F_mat[0][i] * A[$-i-1] % MOD) %= MOD;
    

    auto S_mat = new long[][](N+1, N+1);
    foreach (i; 0..N+1)
        foreach (j; 0..N+1) {
            if (i == 0 && j == 0) S_mat[i][j] = 2;
            else if (i == 0 && j == N) S_mat[i][j] = -1;
            else if (i == j+1) S_mat[i][j] = 1;
            else S_mat[i][j] = 0;
        }
    S_mat = matpow(N+1, K-N, S_mat);
    
    long S = 0;
    foreach (i; 0..N+1)
        (S += S_mat[0][i] * B[$-i-1] % MOD) %= MOD;

    writeln(F, " ", S>=0 ? S:MOD+S);
}

void solve2() {
    auto F = new long[](K.to!int+1);
    F[N] = 0;
    foreach (i; 0..N) {
        F[i] = A[i];
        (F[N] += A[i] % MOD) %= MOD;
    }
    foreach (i; N+1..K.to!int)
        F[i] = (F[i-1] - F[i-N-1] + F[i-1]) % MOD;
    
    long S = 0;
    foreach (i; 0..K.to!int)
        S = (S + F[i]) % MOD;

    auto f = F[K.to!int-1];
    writeln(f>=0 ? f:MOD+f, " ", S>=0 ? S:MOD+S);
}

void main() {
    auto s = readln.split;
    N = s[0].to!int;
    K = s[1].to!long;
    A = readln.split.map!(to!long).array;
    B = new long[](N+1);
    B[0] = 0;
    foreach (i; 0..N)
        B[i+1] = B[i] + A[i];

    N <= 30 ? solve1 : solve2;

    //solve2;
}
0