結果

問題 No.980 Fibonacci Convolution Hard
ユーザー keymoonkeymoon
提出日時 2020-01-30 13:20:00
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 359 ms / 2,000 ms
コード長 2,960 bytes
コンパイル時間 1,651 ms
コンパイル使用メモリ 111,264 KB
実行使用メモリ 60,508 KB
最終ジャッジ日時 2024-09-17 11:24:02
合計ジャッジ時間 10,277 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 342 ms
60,384 KB
testcase_01 AC 348 ms
58,448 KB
testcase_02 AC 355 ms
58,332 KB
testcase_03 AC 347 ms
58,596 KB
testcase_04 AC 351 ms
58,068 KB
testcase_05 AC 353 ms
60,380 KB
testcase_06 AC 359 ms
58,476 KB
testcase_07 AC 351 ms
58,328 KB
testcase_08 AC 342 ms
58,200 KB
testcase_09 AC 346 ms
60,508 KB
testcase_10 AC 348 ms
60,496 KB
testcase_11 AC 341 ms
58,444 KB
testcase_12 AC 341 ms
58,576 KB
testcase_13 AC 349 ms
58,448 KB
testcase_14 AC 347 ms
58,460 KB
testcase_15 AC 340 ms
58,592 KB
testcase_16 AC 326 ms
58,452 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.Numerics;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using static System.Math;
using Debug = System.Diagnostics.Debug;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;


static class P
{
    static void Main()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        Action<bool> assert = (bool b) => { if (!b) throw new Exception(); };
        var p = int.Parse(Console.ReadLine());
        assert(1 <= p && p <= 1000000000);
        var fibs = GetFibs(p, 2000000);
        var res = Solve(fibs);
        //var res = SolveStupid(fibs);
        var Q = int.Parse(Console.ReadLine());
        assert(1 <= Q && Q <= 200000);
        for (int i = 0; i < Q; i++)
        {
            var q = int.Parse(Console.ReadLine());
            assert(2 <= q && q <= 2000000);
            Console.WriteLine(res[q].ToString());
        }
        Console.Out.Flush();
    }

    static ModInt[] Solve(ModInt[] fibs)
    {
        ModInt[] res = new ModInt[fibs.Length];
        res[2] = 0;
        res[3] = 0;
        for (int i = 4; i < res.Length; i++)
        {
            res[i] = (i - 3) * fibs[i - 2] - res[i - 2];
        }
        return res;
    }

    static ModInt[] SolveStupid(ModInt[] fibs)
    {
        ModInt[] res = new ModInt[fibs.Length * 2];
        for (int i = 1; i < fibs.Length; i++)
            for (int j = 1; j < fibs.Length; j++)
                res[i + j] += fibs[i] * fibs[j];
        return res;
    }

    static ModInt[] GetFibs(int p, int count)
    {
        ModInt[] fibs = new ModInt[count + 1];
        fibs[1] = 0;
        fibs[2] = 1;
        for (int i = 3; i < fibs.Length; i++)
            fibs[i] = fibs[i - 1] * p + fibs[i - 2];
        return fibs;
    }
}


struct ModInt
{
    const int MOD = 1000000007;
    long Data;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public ModInt(long data) { if ((Data = data % MOD) < 0) Data += MOD; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator long(ModInt modInt) => modInt.Data;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator ModInt(long val) => new ModInt(val);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt a, ModInt b) { long res = a.Data + b.Data; return new ModInt() { Data = res >= MOD ? res - MOD : res }; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt a, long b) => a.Data + b;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt a, ModInt b) => new ModInt() { Data = a.Data * b.Data % MOD };
    public override string ToString() => Data.ToString();
}
0