結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー bananbobananbo
提出日時 2020-05-20 11:05:23
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 3,523 bytes
コンパイル時間 1,032 ms
コンパイル使用メモリ 114,056 KB
実行使用メモリ 27,280 KB
最終ジャッジ日時 2024-04-09 22:53:54
合計ジャッジ時間 1,996 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
24,856 KB
testcase_01 AC 27 ms
25,104 KB
testcase_02 AC 28 ms
27,152 KB
testcase_03 AC 27 ms
25,248 KB
testcase_04 AC 27 ms
27,032 KB
testcase_05 AC 26 ms
25,108 KB
testcase_06 AC 26 ms
24,984 KB
testcase_07 AC 26 ms
24,856 KB
testcase_08 AC 27 ms
25,112 KB
testcase_09 AC 27 ms
25,120 KB
testcase_10 AC 27 ms
24,988 KB
testcase_11 AC 27 ms
25,376 KB
testcase_12 AC 28 ms
25,240 KB
testcase_13 AC 27 ms
27,280 KB
testcase_14 AC 26 ms
25,008 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.Collections.Generic;
using System.Linq;

namespace YukiCoder526
{
    class Program
    {
        static void Main(string[] args)
        {
            var NM = ReadInt();
            int N = NM[0];
            int M = NM[1];

            long[,] A = new long[2, 2];
            A[0, 0] = 1; A[0, 1] = 1;
            A[1, 0] = 1; A[1, 1] = 0;

            A = Pow(A, N - 1, M);
            //DrawMapforDebug(A);

            Console.WriteLine(A[1, 0]);
            Console.ReadKey();
        }

        /// <summary>
        /// 行列積
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <param name="mod"></param>
        /// <returns></returns>
        static long[,] Multi(long[,] A, long[,] B, long mod)
        {
            long[,] R = new long[A.GetLength(0), B.GetLength(1)];
            for (long i = 0; i < A.GetLength(0); i++)
            {
                for (long k = 0; k < B.GetLength(0); k++)
                {
                    for (long j = 0; j < B.GetLength(1); j++)
                    {
                        R[i, j] = (long)(R[i, j] + (long)A[i, k] * B[k, j]) % mod;
                    }
                }
            }
            return R;
        }

        /// <summary>
        /// 行列累乗
        /// </summary>
        /// <param name="A"></param>
        /// <param name="n"></param>
        /// <param name="mod"></param>
        /// <returns></returns>
        static long[,] Pow(long[,] A, long n, long mod)
        {
            long width = A.GetLength(0);
            if (width != A.GetLength(1))
            {
                Console.WriteLine("it is not square matrix.");
            }

            long[,] B = new long[width, width];
            Array.Copy(A, B, A.Length);

            long[,] R = new long[width, width];
            for (long i = 0; i < width; i++)
            {
                R[i, i] = 1;
            }

            while (n > 0)
            {
                //DrawMapforDebug(B);
                if ((n & 1) > 0) R = Multi(R, B, mod);
                B = Multi(B, B, mod);
                n >>= 1;
            }
            return R;
        }
        /// <summary>
        /// 二次元配列の可視化メソッド
        /// </summary>
        /// <param name="map"></param>
        static void DrawMapforDebug(int[,] map)
        {
            int W = map.GetLength(0);
            int H = map.GetLength(1);

            int[,] map2 = new int[W + 1, H + 1];
            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    if (i == 0 && j == 0) map2[i, j] = 0;
                    else if (i == 0) map2[i, j] = j - 1;
                    else if (j == 0) map2[i, j] = i - 1;
                    else map2[i, j] = map[i - 1, j - 1];
                }
            }

            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    Console.Write(map2[i, j] % 10);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }
    }
}
0