結果

問題 No.1595 The Final Digit
ユーザー startcppstartcpp
提出日時 2021-07-09 21:35:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,455 bytes
コンパイル時間 855 ms
コンパイル使用メモリ 90,908 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 07:56:06
合計ジャッジ時間 1,760 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//想定解は周期性だと思うが、行列累乗で殴ります。
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;
typedef vector<int> Array;
typedef vector<Array> Mat;

const int mod = 10;

Mat ident(int n) {
	Mat ret(n, Array(n));
	int i;
	rep(i, n) ret[i][i] = 1;
	return ret;
}

Mat mul(Mat a, Mat b) {
	int n = a.size();
	int m = b[0].size();
	Mat ret(n, Array(m));
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			for (int k = 0; k < a[0].size(); k++) {
				ret[i][j] += a[i][k] * b[k][j];
				ret[i][j] %= mod;
			}
		}
	}
	return ret;
}

Mat powmat(Mat a, int n) {
	if (n == 0) return ident(a.size());
	if (n % 2 == 0) return powmat(mul(a, a), n / 2);
	return mul(a, powmat(a, n - 1));
}

//Ab (bは縦ベクトル)
Array mul(Mat a, Array b) {
	Array ret(a.size());
	for (int i = 0; i < a.size(); i++) {
		for (int j = 0; j < b.size(); j++) {
			ret[i] += a[i][j] * b[j];
			ret[i] %= mod;
		}
	}
	return ret;
}

signed main() {
	int p, q, r, K;
	cin >> p >> q >> r >> K;
	p %= 10;
	q %= 10;
	r %= 10;
	
	Mat mat = {{0, 1, 0}, {0, 0, 1}, {1, 1, 1}};
	Array ini = {{p, q, r}};
	
	Mat m = powmat(mat, K - 1);
	Array res = mul(m, ini);
	
	cout << res[0] << endl;
	return 0;
}
0