結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー eitahoeitaho
提出日時 2015-04-27 19:31:44
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 911 ms / 5,000 ms
コード長 4,897 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 110,080 KB
実行使用メモリ 27,264 KB
最終ジャッジ日時 2024-07-05 04:54:23
合計ジャッジ時間 12,846 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
22,272 KB
testcase_01 AC 35 ms
22,272 KB
testcase_02 AC 911 ms
26,624 KB
testcase_03 AC 97 ms
25,728 KB
testcase_04 AC 349 ms
26,368 KB
testcase_05 AC 260 ms
26,240 KB
testcase_06 AC 313 ms
26,496 KB
testcase_07 AC 524 ms
26,624 KB
testcase_08 AC 80 ms
25,216 KB
testcase_09 AC 382 ms
26,496 KB
testcase_10 AC 179 ms
26,240 KB
testcase_11 AC 170 ms
26,368 KB
testcase_12 AC 276 ms
26,112 KB
testcase_13 AC 123 ms
26,496 KB
testcase_14 AC 53 ms
23,808 KB
testcase_15 AC 605 ms
26,368 KB
testcase_16 AC 508 ms
26,496 KB
testcase_17 AC 163 ms
26,240 KB
testcase_18 AC 563 ms
26,496 KB
testcase_19 AC 806 ms
26,496 KB
testcase_20 AC 62 ms
25,984 KB
testcase_21 AC 70 ms
27,264 KB
testcase_22 AC 63 ms
25,984 KB
testcase_23 AC 44 ms
23,424 KB
testcase_24 AC 56 ms
25,216 KB
testcase_25 AC 54 ms
24,704 KB
testcase_26 AC 55 ms
24,960 KB
testcase_27 AC 57 ms
24,704 KB
testcase_28 AC 46 ms
23,296 KB
testcase_29 AC 66 ms
26,240 KB
testcase_30 AC 830 ms
26,624 KB
testcase_31 AC 38 ms
22,400 KB
testcase_32 AC 254 ms
26,496 KB
testcase_33 AC 341 ms
26,496 KB
testcase_34 AC 277 ms
26,368 KB
testcase_35 AC 254 ms
26,368 KB
testcase_36 AC 653 ms
26,368 KB
testcase_37 AC 77 ms
24,960 KB
testcase_38 AC 697 ms
26,496 KB
testcase_39 AC 287 ms
26,112 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

class Program
{
    static readonly int SmallN = (int)1e6;
    static readonly int Mod = (int)1e9 + 7;

    public void Solve()
    {
        int L = Reader.Int();
        long N = Reader.Long();
        var F = new[] { 0 }.Concat(Reader.IntArray(L)).Concat(new int[SmallN]).ToArray();
        var S = new int[SmallN + 1];
        for (int i = 1; i <= L; i++) S[i] = (F[i] + S[i - 1]) % Mod;

        if (N <= SmallN)
        {
            for (int i = L + 1; i <= N; i++)
            {
                F[i] = (S[i - 1] - S[i - L - 1] + Mod) % Mod;
                S[i] = (F[i] + S[i - 1]) % Mod;
            }
            Console.WriteLine(F[N] + " " + S[N]);
        }
        else
        {
            var M = new long[L][];
            for (int r = 0; r < L; r++) M[r] = new long[L];
            for (int c = 0; c < L; c++) M[0][c] = 1;
            for (int r = 1; r < L; r++) M[r][r - 1] = 1;
            var MF = MatrixModPower(M, N - L, Mod);
            long f = 0;
            for (int i = 0; i < L; i++) f = (f + MF[0][i] * F[L - i]) % Mod;

            M = Rec(M, N - L, Mod);
            long s = S[L];
            for (int c = 0; c < L; c++) s = (s + M[0][c] * F[L - c]) % Mod;

            Console.WriteLine(f + " " + s);
        }
    }

    long[][] Rec(long[][] A, long pow, long mod)
    {
        if (pow == 1) return A;
        int N = A.Length;
        var B = Rec(A, pow / 2, mod);
        var res = MatrixModMult(MatrixModPower(A, pow / 2, mod), B, mod);
        res = MatrixModAdd(res, B, mod);
        if (pow % 2 == 1)
            res = MatrixModAdd(res, MatrixModPower(A, pow, mod), mod);
        return res;
    }

    static long[][] MatrixModPower(long[][] A, long n, long mod)
    {
        int size = A.Length;
        long[][] res = new long[size][];
        for (int i = 0; i < size; i++) { res[i] = new long[size]; res[i][i] = 1; }
        while (n > 0)
        {
            if ((n & 1) == 1) res = MatrixModMult(res, A, mod);
            A = MatrixModMult(A, A, mod);
            n >>= 1;
        }
        return res;
    }
    static long[][] MatrixModMult(long[][] A, long[][] B, long mod)
    {
        long[][] res = new long[A.Length][];
        for (int i = 0; i < res.Length; i++) res[i] = new long[B[0].Length];
        for (int i = 0; i < A.Length; i++)
            for (int k = 0; k < A[0].Length; k++)
                for (int j = 0; j < B[0].Length; j++)
                    res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % mod;
        return res;
    }
    static long[][] MatrixModAdd(long[][] A, long[][] B, long mod)
    {
        long[][] res = new long[A.Length][];
        for (int i = 0; i < res.Length; i++) res[i] = new long[A[0].Length];
        for (int r = 0; r < A.Length; r++)
            for (int c = 0; c < A[0].Length; c++)
            {
                if ((res[r][c] = A[r][c] + B[r][c]) >= mod) res[r][c] -= mod;
                if (res[r][c] < 0) res[r][c] += mod;
            }
        return res;
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Line()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0