結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー さかぽんさかぽん
提出日時 2021-05-18 11:41:35
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,276 bytes
コンパイル時間 969 ms
コンパイル使用メモリ 118,552 KB
実行使用メモリ 17,792 KB
最終ジャッジ日時 2024-04-17 01:18:27
合計ジャッジ時間 2,006 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
17,664 KB
testcase_01 AC 26 ms
17,408 KB
testcase_02 AC 27 ms
17,792 KB
testcase_03 AC 26 ms
17,792 KB
testcase_04 AC 26 ms
17,408 KB
testcase_05 AC 26 ms
17,664 KB
testcase_06 AC 26 ms
17,664 KB
testcase_07 AC 26 ms
17,664 KB
testcase_08 AC 27 ms
17,792 KB
testcase_09 AC 26 ms
17,664 KB
testcase_10 AC 26 ms
17,536 KB
testcase_11 AC 27 ms
17,664 KB
testcase_12 AC 27 ms
17,664 KB
testcase_13 AC 26 ms
17,664 KB
testcase_14 AC 26 ms
17,664 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;

class BM
{
	static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
	static (int, int) Read2() { var a = Read(); return (a[0], a[1]); }
	static void Main() => Console.WriteLine(Solve());
	static object Solve()
	{
		var (n, m) = Read2();
		ModuloMatrixHelper.M = m;

		var a = new long[2, 2];
		a[0, 0] = a[0, 1] = a[1, 0] = 1;

		a = ModuloMatrixHelper.MPow(a, n - 1);
		return a[1, 0];
	}
}

static class ModuloMatrixHelper
{
	public static long M = 1000000007;

	public static long[,] Unit(int n)
	{
		var r = new long[n, n];
		for (int i = 0; i < n; i++) r[i, i] = 1;
		return r;
	}

	public static long[,] MPow(long[,] b, long i)
	{
		var r = Unit(b.GetLength(0));
		for (; i != 0; b = MMul(b, b), i >>= 1) if ((i & 1) != 0) r = MMul(r, b);
		return r;
	}

	public static long[,] MMul(long[,] a, long[,] b)
	{
		var n = a.GetLength(0);
		var r = new long[n, n];
		for (var i = 0; i < n; i++)
			for (var j = 0; j < n; j++)
				for (var k = 0; k < n; k++)
					r[i, j] = (r[i, j] + a[i, k] * b[k, j]) % M;
		return r;
	}

	public static long[] MMul(long[,] a, long[] v)
	{
		var n = v.Length;
		var r = new long[n];
		for (var i = 0; i < n; i++)
			for (var k = 0; k < n; k++)
				r[i] = (r[i] + a[i, k] * v[k]) % M;
		return r;
	}
}
0