Submission #1034940


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;
        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 < N; ai++)
            for (int bi = 0; bi < N; bi++)
            {
                res[ai + bi] += A[ai] * B[bi];
                if (res[ai + bi] > limit) res[ai + bi] %= mod;
            }
        for (int x = N * 2 - 1; x >= N; x--)
            for (int y = 0; y < N; y++)
            {
                res[x - y - 1] += C[N - y - 1] * res[x];
                res[x - y - 1] %= mod;
            }
        var r = new ulong[N];
        Array.Copy(res, r, N);
        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 4057 Byte
Status AC
Exec Time 783 ms
Memory 7900 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 727 ms 7892 KB
01 AC 481 ms 7892 KB
02 AC 783 ms 7900 KB
03 AC 194 ms 7884 KB
04 AC 75 ms 7612 KB
90 AC 75 ms 7624 KB
91 AC 73 ms 7628 KB