Submission #1034924


Source Code Expand

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

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

    public void Solve()
    {
        int N = Reader.Int(), X = Reader.Int();
        var C = new long[N + 1];
        var Y = new long[N + 1];
        for (int i = 1; i <= N; i++) C[i] = Y[i] = 1;
        var ans = LinearRecurrenceRelation.Calc(C, Y, X, Mod);
        Console.WriteLine(ans);
        Console.ReadLine();
    }
}

class LinearRecurrenceRelation
{
    // F(n + 1) = C[0] + C[1] * Y(1) + C[2] * Y(2) + ... + C[n] * Y(n)
    // return F(x)
    public static long Calc(long[] C, long[] Y, long x, long mod)
    {
        if (x < Y.Length) return Y[x];
        int N = C.Length;
        C = Power(C, x, mod);
        long res = 0;
        for (int i = 0; i < C.Length; i++)
            res = (res + C[i] * Y[i]) % mod;
        return res;
    }

    static long[] Power(long[] A, long p, long mod)
    {
        long[] x = new long[A.Length], res = new long[A.Length];
        x[1] = res[0] = 1;
        for (; p > 0; p >>= 1, Mult(x, x, A, x, mod))
            if (p % 2 == 1) Mult(res, x, A, res, mod);
        return res;
    }
    static long[] Mult(long[] A, long[] B, long[] C, long[] res, long mod)
    {
        var result = new long[A.Length * 2];
        for (int ai = 0; ai < A.Length; ai++)
            for (int bi = 0; bi < B.Length; bi++)
            {
                result[ai + bi] += A[ai] * B[bi];
                result[ai + bi] %= mod;
            }
        for (int x = result.Length - 1; x >= A.Length; x--)
            for (int y = 0; y < C.Length; y++)
            {
                result[x - y - 1] += C[C.Length - y - 1] * result[x];
                result[x - y - 1] %= mod;
            }
        Array.Copy(result, res, res.Length);
        return res;
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    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 Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    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;
    }
}

Submission Info

Submission Time
Task T - フィボナッチ
User eitaho
Language C# (Mono 2.10.8.1)
Score 8
Code Size 3877 Byte
Status AC
Exec Time 1356 ms
Memory 7884 KB

Judge Result

Set Name All
Score / Max Score 8 / 8
Status
AC × 7
Set Name Test Cases
All 00, 01, 02, 03, 04, 90, 91
Case Name Status Exec Time Memory
00 AC 1248 ms 7880 KB
01 AC 807 ms 7880 KB
02 AC 1356 ms 7872 KB
03 AC 287 ms 7884 KB
04 AC 75 ms 7680 KB
90 AC 76 ms 7616 KB
91 AC 76 ms 7676 KB