結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー eitahoeitaho
提出日時 2015-04-27 20:45:20
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,400 ms / 5,000 ms
コード長 5,607 bytes
コンパイル時間 1,056 ms
コンパイル使用メモリ 110,976 KB
実行使用メモリ 27,392 KB
最終ジャッジ日時 2024-07-05 04:55:02
合計ジャッジ時間 17,822 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
22,272 KB
testcase_01 AC 33 ms
22,272 KB
testcase_02 AC 1,400 ms
26,496 KB
testcase_03 AC 131 ms
24,960 KB
testcase_04 AC 529 ms
26,496 KB
testcase_05 AC 389 ms
26,496 KB
testcase_06 AC 456 ms
26,496 KB
testcase_07 AC 781 ms
26,496 KB
testcase_08 AC 106 ms
24,704 KB
testcase_09 AC 574 ms
26,368 KB
testcase_10 AC 252 ms
26,624 KB
testcase_11 AC 243 ms
26,368 KB
testcase_12 AC 405 ms
26,624 KB
testcase_13 AC 168 ms
25,984 KB
testcase_14 AC 61 ms
23,552 KB
testcase_15 AC 907 ms
26,368 KB
testcase_16 AC 783 ms
26,496 KB
testcase_17 AC 234 ms
26,496 KB
testcase_18 AC 849 ms
26,496 KB
testcase_19 AC 1,223 ms
26,368 KB
testcase_20 AC 60 ms
26,240 KB
testcase_21 AC 70 ms
27,392 KB
testcase_22 AC 60 ms
26,240 KB
testcase_23 AC 41 ms
23,424 KB
testcase_24 AC 52 ms
25,088 KB
testcase_25 AC 48 ms
24,832 KB
testcase_26 AC 50 ms
25,088 KB
testcase_27 AC 53 ms
24,576 KB
testcase_28 AC 43 ms
23,552 KB
testcase_29 AC 64 ms
26,240 KB
testcase_30 AC 1,267 ms
26,624 KB
testcase_31 AC 35 ms
22,528 KB
testcase_32 AC 379 ms
26,496 KB
testcase_33 AC 509 ms
26,624 KB
testcase_34 AC 409 ms
26,624 KB
testcase_35 AC 368 ms
26,496 KB
testcase_36 AC 1,006 ms
26,624 KB
testcase_37 AC 100 ms
24,576 KB
testcase_38 AC 1,028 ms
26,368 KB
testcase_39 AC 440 ms
26,624 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 Fmat = new Matrix(F.Skip(1).Take(L).Reverse().ToArray(), Mod);
            long f = (M.Power(N - L) * Fmat)[0, 0];
            long s = (S[L] + (Rec(M, N - L) * Fmat)[0, 0]) % Mod;
            Console.WriteLine(f + " " + s);
        }
    }

    Matrix Rec(Matrix A, long pow)
    {
        if (pow == 1) return A;
        var halfSum = Rec(A, pow / 2);
        var res = A.Power(pow / 2) * halfSum;
        res = res + halfSum;
        if (pow % 2 == 1) res += A.Power(pow);
        return res;
    }

    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 * W + c]; }
            set { M[r * W + c] = (value % Mod + Mod) % Mod; }
        }
        public Matrix(int h, int w, long mod)
        {
            H = h; W = w; Mod = mod; M = new long[h * w];
        }
        public Matrix(long[] vec, long mod)
        {
            H = vec.Length; W = 1; Mod = mod; M = new long[H];
            for (int i = 0; i < H; i++) this[i, 0] = vec[i];
        }
        public Matrix(int[] vec, long mod)
        {
            H = vec.Length; W = 1; Mod = mod; M = new long[vec.Length];
            for (int i = 0; i < H; i++) this[i, 0] = vec[i];
        }
        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 i = 0; i < A.M.Length; i++)
                Add(ref res.M[i], B.M[i], 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);
            for (int r = 0; r < res.H; r++)
                for (int i = 0; i < A.W; i++)
                    for (int c = 0; c < res.W; c++)
                        Add(ref res.M[r * res.W + c], A.M[r * A.W + i] * B.M[i * B.W + c] % mod, 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;
        }
        private static void Add(ref long a, long b, long mod)
        {
            if ((a += b) >= mod) a -= mod;
            if (a < 0) a += mod;
        }
        public Matrix Clone()
        {
            var res = new Matrix(H, W, Mod);
            Array.Copy(M, res.M, M.Length);
            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