結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー eitaho
提出日時 2015-04-28 02:25:00
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 259 ms / 5,000 ms
コード長 4,835 bytes
コンパイル時間 1,085 ms
コンパイル使用メモリ 116,736 KB
実行使用メモリ 27,392 KB
最終ジャッジ日時 2024-07-05 05:00:27
合計ジャッジ時間 6,174 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
            long[] X = F.Skip(1).Take(L).Select(x => (long)x).ToArray();
            long f, s;
            LinearRecurrenceRelationSum(M, X, N, out f, out s);
            Console.WriteLine(f + " " + s);
        }
    }

    // Xn = X[N], Sum = X[1] + X[2] + ... + X[N]
    public void LinearRecurrenceRelationSum(long[][] A, long[] X, long N, out long Xn, out long Sum)
    {
        int H = A.Length;
        var M = new long[H * 2][];
        var IO = new long[H * 2][];
        for (int r = 0; r < H * 2; r++)
        {
            M[r] = new long[H * 2]; IO[r] = new long[H];
        }
        for (int r = 0; r < H; r++) for (int c = 0; c < H; c++) M[r][c] = A[r][c];
        for (int i = 0; i < H; i++) M[H + i][i] = M[H + i][H + i] = IO[i][i] = 1;
        M = MatrixModPower(M, N - H + 1, Mod);
        M = MatrixModMult(M, IO, Mod);
        for (int i = 0; i < H; i++) M[H + i][i] = (M[H + i][i] + Mod - 1) % Mod;
        Xn = Sum = 0;
        for (int i = 0; i < H; i++)
        {
            Xn = (Xn + M[1][i] * X[H - 1 - i]) % Mod;
            Sum = (Sum + X[i]) % Mod;
            Sum = (Sum + M[H][i] * X[H - 1 - i]) % Mod;
        }
    }

    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;
    }
}


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