結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー Lin143rdLin143rd
提出日時 2022-10-25 12:12:44
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,679 bytes
コンパイル時間 14,824 ms
コンパイル使用メモリ 147,320 KB
実行使用メモリ 165,428 KB
最終ジャッジ日時 2023-09-16 11:09:48
合計ジャッジ時間 16,585 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
28,248 KB
testcase_01 AC 46 ms
28,172 KB
testcase_02 AC 48 ms
30,212 KB
testcase_03 AC 47 ms
28,240 KB
testcase_04 AC 48 ms
27,972 KB
testcase_05 AC 47 ms
28,152 KB
testcase_06 AC 47 ms
28,244 KB
testcase_07 AC 47 ms
28,480 KB
testcase_08 AC 46 ms
28,264 KB
testcase_09 AC 47 ms
28,132 KB
testcase_10 AC 48 ms
30,352 KB
testcase_11 AC 48 ms
30,236 KB
testcase_12 AC 47 ms
28,060 KB
testcase_13 AC 47 ms
28,284 KB
testcase_14 AC 47 ms
165,428 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 119 ms).
.NET 向け Microsoft (R) Build Engine バージョン 17.0.0-preview-21470-01+cb055d28f
Copyright (C) Microsoft Corporation.All rights reserved.

  プレビュー版の .NET を使用しています。https://aka.ms/dotnet-core-preview をご覧ください
  main -> /home/judge/data/code/bin/Release/net6.0/main.dll
  main -> /home/judge/data/code/bin/Release/net6.0/publish/

ソースコード

diff #

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Numerics;
using System.Linq.Expressions;
using System.Linq;
using System.Diagnostics;
public class Csharp_Work
{    
    static long m;
    static long[, ] Mul(long[, ] a, long[, ] b)
    {
        var res = new long[a.GetLength(0), b.GetLength(1)];
        for(int i = 0; i < a.GetLength(0); ++i) {
            for(int j = 0; j < b.GetLength(1); ++j) {
                for(int k = 0; k < a.GetLength(1); ++k) {
                    res[i, j] += a[i, k] * b[k, j];
                    res[i, j] %= m;
                }
            }
        }
        return res;
    }
    // 正方行列の累乗
    static long[, ] Pow(long[, ] a, long n)
    {
        long lng = a.GetLength(0);
        var res = new long[lng, lng];
        var a_tmp = a.Clone() as long[, ];
        // 単位行列
        for(int i = 0; i < lng; ++i) {
            res[i, i] = 1;
        }
        long num = n;
        while(num > 0) {
            if((num & 1) != 0) {
                res = Mul(res, a_tmp);
                num -= 1;
                continue;
            }
            a_tmp = Mul(a_tmp, a_tmp);
            num >>= 1;
        }
        return res;
    }
    static void Main(string[] args)
    {
        var fibo = new long[2, 2];
        fibo[0, 0] = 1;
        fibo[0, 1] = 1;
        fibo[1, 0] = 1;
        fibo[1, 1] = 0;
        var tmp = Console.ReadLine().Split();
        long n = long.Parse(tmp[0]);
        m = long.Parse(tmp[1]);
        var res = Pow(fibo, n - 2);
        Console.WriteLine(res[0, 0]);
    }   
}
0