結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー masaktmasakt
提出日時 2017-06-09 22:38:14
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 1,252 bytes
コンパイル時間 5,224 ms
コンパイル使用メモリ 110,732 KB
実行使用メモリ 296,440 KB
最終ジャッジ日時 2023-10-23 22:10:07
合計ジャッジ時間 3,624 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
24,108 KB
testcase_01 AC 25 ms
24,108 KB
testcase_02 AC 24 ms
24,108 KB
testcase_03 AC 25 ms
24,108 KB
testcase_04 AC 25 ms
24,108 KB
testcase_05 AC 25 ms
24,108 KB
testcase_06 AC 25 ms
24,108 KB
testcase_07 AC 25 ms
24,140 KB
testcase_08 AC 26 ms
24,560 KB
testcase_09 AC 31 ms
28,792 KB
testcase_10 AC 101 ms
77,760 KB
testcase_11 AC 405 ms
296,220 KB
testcase_12 AC 427 ms
296,388 KB
testcase_13 AC 378 ms
296,440 KB
testcase_14 AC 384 ms
296,440 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;
using System.IO;
using System.Collections.Generic;

class Solution
{
    long N, M;
    long[] memo;
    long fib(long i)
    {
        if (i == 0) return 0;
        if (i == 1) return 1;
        if (memo[i] > 0) return memo[i];
        return memo[i] = (fib(i - 1) + fib(i - 2)) % M;
    }
    public void solve(TextReader input, TextWriter output)
    {
        string[] lines = input.ReadLine().Split(' ');
        N = long.Parse(lines[0]);
        M = long.Parse(lines[1]);
        memo = new long[N + 1];
        output.WriteLine(fib(N-1));
    }
}

class CaideConstants {
    public const string InputFile = null;
    public const string OutputFile = null;
}
public class Program {
    public static void Main(string[] args) {
        Solution solution = new Solution();
        using (System.IO.TextReader input =
                CaideConstants.InputFile == null ? System.Console.In :
                        new System.IO.StreamReader(CaideConstants.InputFile))
        using (System.IO.TextWriter output =
                CaideConstants.OutputFile == null ? System.Console.Out:
                        new System.IO.StreamWriter(CaideConstants.OutputFile))

            solution.solve(input, output);
    }
}

0