結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー RisenRisen
提出日時 2017-06-09 22:35:01
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 740 bytes
コンパイル時間 1,350 ms
コンパイル使用メモリ 110,032 KB
実行使用メモリ 478,308 KB
最終ジャッジ日時 2023-10-23 22:00:24
合計ジャッジ時間 12,294 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
25,380 KB
testcase_01 AC 31 ms
25,392 KB
testcase_02 AC 31 ms
25,392 KB
testcase_03 AC 31 ms
25,380 KB
testcase_04 AC 31 ms
25,392 KB
testcase_05 AC 30 ms
25,380 KB
testcase_06 AC 31 ms
25,400 KB
testcase_07 AC 31 ms
25,520 KB
testcase_08 AC 33 ms
26,764 KB
testcase_09 AC 49 ms
36,048 KB
testcase_10 AC 301 ms
119,712 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Collections.Generic;
using System.Linq;

class Solution
{
    static Dictionary<ulong, ulong> memo = new Dictionary<ulong, ulong>();

    static ulong GetFib(ulong n, ulong m)
    {
        if (n == 1)
        {
            return 0;
        }

        if (n == 2)
        {
            return 1 % m;
        }

        if (memo.ContainsKey(n))
        {
            return memo[n];
        }

        var fib = (GetFib(n - 1, m) + GetFib(n - 2, m)) % m;
        memo[n] = fib;
        return fib;
    }

    static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(ulong.Parse).ToArray();
        var n = vals[0];
        var m = vals[1];

        Console.WriteLine(GetFib(n, m));
    }
}
0