結果

問題 No.978 Fibonacci Convolution Easy
ユーザー keymoonkeymoon
提出日時 2020-01-31 19:46:11
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 2,813 bytes
コンパイル時間 4,697 ms
コンパイル使用メモリ 111,188 KB
実行使用メモリ 39,920 KB
最終ジャッジ日時 2023-10-19 00:56:17
合計ジャッジ時間 3,855 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
25,332 KB
testcase_01 AC 90 ms
30,376 KB
testcase_02 AC 61 ms
27,364 KB
testcase_03 AC 177 ms
39,276 KB
testcase_04 AC 69 ms
28,104 KB
testcase_05 AC 38 ms
25,332 KB
testcase_06 AC 82 ms
29,544 KB
testcase_07 AC 127 ms
33,996 KB
testcase_08 AC 97 ms
30,984 KB
testcase_09 AC 141 ms
35,436 KB
testcase_10 AC 184 ms
39,888 KB
testcase_11 AC 74 ms
28,712 KB
testcase_12 AC 37 ms
25,332 KB
testcase_13 AC 82 ms
29,452 KB
testcase_14 AC 44 ms
25,744 KB
testcase_15 AC 89 ms
30,192 KB
testcase_16 AC 183 ms
39,920 KB
testcase_17 AC 183 ms
39,868 KB
testcase_18 AC 29 ms
25,332 KB
testcase_19 AC 30 ms
25,332 KB
testcase_20 AC 29 ms
25,332 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.Linq;

static class P
{
    static void Main()
    {
        var np = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var n = np[0];
        var p = np[1];
        Assert(1 <= n && n <= 2 * 1000000);
        Assert(1 <= p && p <= 1000000000);

        var fib = new ModInt[n];
        if (n != 1) fib[1] = 1;
        for (int i = 2; i < n; i++)
            fib[i] = fib[i - 1] * p + fib[i - 2];

        ModInt side = 0;
        ModInt diagnal = 0;
        for (int i = 0; i < n; i++)
        {
            side += fib[i];
            diagnal += fib[i] * fib[i];
        }
        Console.WriteLine((side * side + diagnal) / 2);
    }
    static void Assert(bool b) { if (!b) throw new Exception(); }
}


struct ModInt
{
    public const int Mod = 1000000007;
    const long POSITIVIZER = ((long)Mod) << 31;
    long Data;
    public ModInt(long data) { if ((Data = data % Mod) < 0) Data += Mod; }
    public static implicit operator long(ModInt modInt) => modInt.Data;
    public static implicit operator ModInt(long val) => new ModInt(val);
    public static ModInt operator +(ModInt a, int b) => new ModInt() { Data = (a.Data + b + POSITIVIZER) % Mod };
    public static ModInt operator +(ModInt a, long b) => new ModInt(a.Data + b);
    public static ModInt operator +(ModInt a, ModInt b) { long res = a.Data + b.Data; return new ModInt() { Data = res >= Mod ? res - Mod : res }; }
    public static ModInt operator -(ModInt a, int b) => new ModInt() { Data = (a.Data - b + POSITIVIZER) % Mod };
    public static ModInt operator -(ModInt a, long b) => new ModInt(a.Data - b);
    public static ModInt operator -(ModInt a, ModInt b) { long res = a.Data - b.Data; return new ModInt() { Data = res < 0 ? res + Mod : res }; }
    public static ModInt operator *(ModInt a, int b) => new ModInt(a.Data * b);
    public static ModInt operator *(ModInt a, long b) => a * new ModInt(b);
    public static ModInt operator *(ModInt a, ModInt b) => new ModInt() { Data = a.Data * b.Data % Mod };
    public static ModInt operator /(ModInt a, ModInt b) => new ModInt() { Data = a.Data * GetInverse(b) % Mod };
    public static bool operator ==(ModInt a, ModInt b) => a.Data == b.Data;
    public static bool operator !=(ModInt a, ModInt b) => a.Data != b.Data;
    public override string ToString() => Data.ToString();
    public override bool Equals(object obj) => (ModInt)obj == this;
    public override int GetHashCode() => (int)Data;
    static long GetInverse(long a)
    {
        long div, p = Mod, x1 = 1, y1 = 0, x2 = 0, y2 = 1;
        while (true)
        {
            if (p == 1) return x2 + Mod; div = a / p; x1 -= x2 * div; y1 -= y2 * div; a %= p;
            if (a == 1) return x1 + Mod; div = p / a; x2 -= x1 * div; y2 -= y1 * div; p %= a;
        }
    }
}
0