結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー eitahoeitaho
提出日時 2015-04-28 01:25:43
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 324 ms / 5,000 ms
コード長 6,641 bytes
コンパイル時間 3,305 ms
コンパイル使用メモリ 111,944 KB
実行使用メモリ 32,016 KB
最終ジャッジ日時 2023-09-18 14:01:21
合計ジャッジ時間 10,661 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
25,228 KB
testcase_01 AC 68 ms
25,252 KB
testcase_02 AC 324 ms
27,288 KB
testcase_03 AC 89 ms
27,308 KB
testcase_04 AC 159 ms
23,388 KB
testcase_05 AC 140 ms
25,352 KB
testcase_06 AC 159 ms
23,240 KB
testcase_07 AC 224 ms
25,236 KB
testcase_08 AC 83 ms
25,228 KB
testcase_09 AC 186 ms
23,156 KB
testcase_10 AC 109 ms
27,224 KB
testcase_11 AC 113 ms
25,168 KB
testcase_12 AC 144 ms
25,216 KB
testcase_13 AC 99 ms
25,236 KB
testcase_14 AC 76 ms
25,304 KB
testcase_15 AC 264 ms
25,312 KB
testcase_16 AC 227 ms
25,256 KB
testcase_17 AC 110 ms
27,240 KB
testcase_18 AC 240 ms
25,240 KB
testcase_19 AC 310 ms
27,336 KB
testcase_20 AC 97 ms
31,136 KB
testcase_21 AC 107 ms
32,016 KB
testcase_22 AC 96 ms
31,368 KB
testcase_23 AC 79 ms
26,056 KB
testcase_24 AC 91 ms
29,452 KB
testcase_25 AC 90 ms
28,232 KB
testcase_26 AC 91 ms
28,388 KB
testcase_27 AC 89 ms
28,476 KB
testcase_28 AC 80 ms
26,072 KB
testcase_29 AC 101 ms
30,984 KB
testcase_30 AC 313 ms
27,324 KB
testcase_31 AC 72 ms
25,168 KB
testcase_32 AC 135 ms
27,408 KB
testcase_33 AC 169 ms
25,220 KB
testcase_34 AC 150 ms
25,232 KB
testcase_35 AC 135 ms
27,288 KB
testcase_36 AC 249 ms
27,332 KB
testcase_37 AC 82 ms
27,212 KB
testcase_38 AC 277 ms
25,220 KB
testcase_39 AC 144 ms
25,248 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 Matrix(L, L, Mod);
            for (int c = 0; c < L; c++) M[0, c] = 1;
            for (int r = 1; r < L; r++) M[r, r - 1] = 1;
            var vec = new Matrix(F.Skip(1).Take(L).Reverse().ToArray(), Mod);
            long f, s;
            M.SumPower(N, vec, out f, out s);
            Console.WriteLine(f + " " + s);
        }
    }

    class Matrix
    {
        public readonly int H, W;
        public readonly long Mod;
        private readonly long[][] M;

        public long this[int r, int c]
        {
            get { return M[r][c]; }
            set { M[r][c] = value >= 0 ? value % Mod : value % Mod + Mod; }
        }
        public Matrix(int h, int w, long mod)
        {
            H = h; W = w; Mod = mod; M = new long[h][];
            for (int r = 0; r < H; r++) M[r] = new long[w];
        }
        public Matrix(long[] vec, long mod)
        {
            H = vec.Length; W = 1; Mod = mod; M = new long[H][];
            for (int r = 0; r < H; r++) { M[r] = new long[1]; this[r, 0] = vec[r]; }
        }
        public Matrix(int[] vec, long mod)
        {
            H = vec.Length; W = 1; Mod = mod; M = new long[H][];
            for (int r = 0; r < H; r++) { M[r] = new long[1]; this[r, 0] = vec[r]; }
        }
        public static Matrix operator +(Matrix A, Matrix B)
        {
            Debug.Assert(A.H == B.H && A.W == B.W && A.Mod == B.Mod);
            var res = A.Clone();
            for (int r = 0; r < res.H; r++)
                for (int c = 0; c < res.W; c++)
                    if ((res.M[r][c] += B.M[r][c]) >= A.Mod) res.M[r][c] -= A.Mod;
            return res;
        }
        public static Matrix operator *(Matrix A, Matrix B)
        {
            Debug.Assert(A.W == B.H && A.Mod == B.Mod);
            long mod = A.Mod;
            var res = new Matrix(A.H, B.W, mod);
            int h = res.H, w1 = A.W, w2 = res.W;
            long[][] C = res.M, a = A.M, b = B.M;
            for (int r = 0; r < h; r++)
                for (int i = 0; i < w1; i++)
                    for (int c = 0; c < w2; c++)
                        if ((C[r][c] += a[r][i] * b[i][c] % mod) >= mod) C[r][c] -= mod;
            return res;
        }
        public Matrix Power(long pow)
        {
            Debug.Assert(H == W);
            var A = Clone();
            var res = new Matrix(H, W, Mod);
            for (int i = 0; i < H; i++) res[i, i] = 1;
            while (pow > 0)
            {
                if ((pow & 1) == 1) res = res * A;
                A = A * A;
                pow >>= 1;
            }
            return res;
        }
        // A + A^2 + A^3 + ... + A^pow
        public void SumPower(long pow, Matrix vec, out long A, out long S)
        {
            Debug.Assert(H == W && W == vec.H);
            Debug.Assert(pow >= H);
            var M = new Matrix(H * 2, H * 2, Mod);
            var IO = new Matrix(H * 2, H, Mod);
            for (int r = 0; r < H; r++) for (int c = 0; c < W; c++) M[r, c] = this[r, c];
            for (int i = 0; i < H; i++) M[H + i, i] = M[H + i, H + i] = IO[i, i] = 1;
            M = M.Power(pow - H + 1) * IO;
            for (int i = 0; i < H; i++) M[H + i, i] -= 1;
            M = M * vec;
            A = M[1, 0];
            S = M[H, 0];
            for (int i = 0; i < H; i++) S = (S + vec[i, 0]) % Mod;
        }
        public Matrix Clone()
        {
            var res = new Matrix(H, W, Mod);
            for (int r = 0; r < H; r++) Array.Copy(M[r], res.M[r], res.W);
            return res;
        }
        public override string ToString()
        {
            var s = new StringBuilder();
            int L = 2;
            for (int r = 0; r < H; r++) for (int c = 0; c < W; c++) L = Math.Max(L, this[r, c].ToString().Length);
            for (int r = 0; r < H; r++)
            {
                for (int c = 0; c < W; c++) s.Append(this[r, c].ToString().PadLeft(L + 1, ' '));
                s.AppendLine();
            }
            return s.ToString();
        }
    }
}


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