Submission #1034962


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

class LinearRecurrenceRelation
{
    // F(n + 1) = 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;
        var A = new ulong[C.Length];
        for (int i = 0; i < C.Length; i++) A[i] = (ulong)C[i];
        A = Power(A, x, (ulong)mod);
        long res = 0;
        for (int i = 0; i < C.Length; i++)
            res = (res + (long)A[i] * Y[i]) % mod;
        return res;
    }

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


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 4221 Byte
Status AC
Exec Time 350 ms
Memory 3012 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 325 ms 2896 KB
01 AC 222 ms 2956 KB
02 AC 350 ms 3012 KB
03 AC 98 ms 2884 KB
04 AC 48 ms 2868 KB
90 AC 49 ms 2772 KB
91 AC 49 ms 2800 KB