Submission #1034931


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)
    {
        var res = new ulong[A.Length * 2];
        for (int ai = 0; ai < A.Length; ai++)
            for (int bi = 0; bi < B.Length; bi++)
            {
                res[ai + bi] += A[ai] * B[bi];
                res[ai + bi] %= mod;
            }
        for (int x = res.Length - 1; x >= A.Length; x--)
            for (int y = 0; y < C.Length; y++)
            {
                res[x - y - 1] += C[C.Length - y - 1] * res[x];
                res[x - y - 1] %= mod;
            }
        var r = new ulong[A.Length];
        Array.Copy(res, r, r.Length);
        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 4002 Byte
Status AC
Exec Time 1156 ms
Memory 2964 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 1062 ms 2964 KB
01 AC 681 ms 2948 KB
02 AC 1156 ms 2952 KB
03 AC 232 ms 2964 KB
04 AC 49 ms 2708 KB
90 AC 49 ms 2744 KB
91 AC 49 ms 2684 KB