結果

問題 No.1595 The Final Digit
ユーザー さかぽんさかぽん
提出日時 2021-07-12 15:16:30
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 1,788 bytes
コンパイル時間 4,417 ms
コンパイル使用メモリ 106,548 KB
実行使用メモリ 22,832 KB
最終ジャッジ日時 2023-09-14 20:35:27
合計ジャッジ時間 6,023 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,788 KB
testcase_01 AC 59 ms
20,692 KB
testcase_02 AC 58 ms
20,796 KB
testcase_03 AC 58 ms
20,748 KB
testcase_04 AC 58 ms
20,748 KB
testcase_05 AC 60 ms
20,792 KB
testcase_06 AC 58 ms
20,720 KB
testcase_07 AC 59 ms
18,776 KB
testcase_08 AC 59 ms
18,760 KB
testcase_09 AC 59 ms
22,792 KB
testcase_10 AC 58 ms
22,756 KB
testcase_11 AC 58 ms
20,728 KB
testcase_12 AC 58 ms
22,764 KB
testcase_13 AC 59 ms
20,852 KB
testcase_14 AC 58 ms
20,744 KB
testcase_15 AC 58 ms
20,796 KB
testcase_16 AC 58 ms
22,772 KB
testcase_17 AC 58 ms
20,768 KB
testcase_18 AC 59 ms
22,832 KB
testcase_19 AC 59 ms
20,696 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;

class E
{
	static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
	static (int, int) Read2() { var a = Read(); return (a[0], a[1]); }
	static long[] ReadL() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
	static (long, long, long, long) Read4L() { var a = ReadL(); return (a[0], a[1], a[2], a[3]); }
	static void Main() => Console.WriteLine(Solve());
	static object Solve()
	{
		var (p, q, r, k) = Read4L();
		return GetValueWithMod(1, -1, 1, p, q, r, k - 1, 10);
	}

	public static long GetValueWithMod(long p, long q, long r, long a0, long a1, long a2, long n, long mod)
	{
		var m = new ModMatrixOperator(mod);

		var a = new long[3, 3];
		a[0, 0] = p;
		a[0, 1] = -q;
		a[0, 2] = r;
		a[1, 0] = a[2, 1] = 1;
		a = m.Pow(a, n);
		var v = m.Mul(a, new[] { a2, a1, a0 });
		return v[2];
	}
}

public class ModMatrixOperator
{
	long M;
	public ModMatrixOperator(long mod) { M = mod; }
	long MInt(long x) => (x %= M) < 0 ? x + M : x;

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

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

	public long[,] Mul(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] = MInt(r[i, j] + a[i, k] * b[k, j]);
		return r;
	}

	public long[] Mul(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] = MInt(r[i] + a[i, k] * v[k]);
		return r;
	}
}
0