結果
問題 | No.1595 The Final Digit |
ユーザー |
![]() |
提出日時 | 2021-07-09 21:42:00 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,037 bytes |
コンパイル時間 | 4,552 ms |
コンパイル使用メモリ | 253,200 KB |
最終ジャッジ日時 | 2025-01-22 21:16:18 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/all>using namespace std;using namespace atcoder;using ll = long long;using ull = unsigned long long;using ld = long double;using pii = pair<int, int>;using pdd = pair<double, double>;using pll = pair<ll, ll>;using pli = pair<ll, int>;using pil = pair<int, ll>;template <typename T>using Graph = vector<vector<T>>;const int MOD = 1e9 + 7;const ld PI = acos(-1);template <typename T>struct Matrix {int H, W;vector<vector<T>> M;Matrix(int H_, int W_, T val = 0) : H(H_), W(W_) {M = vector<vector<T>>(H, vector<T>(W, val));}};template <typename T>Matrix<T> operator*(Matrix<T> &A, Matrix<T> &B) {assert(A.W == B.H);Matrix<T> res(A.H, B.W, 0);for (int i = 0; i < A.H; i++) {for (int k = 0; k < A.W; k++) {for (int j = 0; j < B.W; j++) {(res.M[i][j] += A.M[i][k] * B.M[k][j]) %= 10;}}}return res;}template <typename T>Matrix<T> operator+(Matrix<T> &A, Matrix<T> &B) {assert(A.H == B.H);assert(A.W == B.W);Matrix<T> res(A.H, B.W, 0);for (int i = 0; i < A.H; i++) {for (int j = 0; j < B.W; j++) {res.M[i][j] = (A.M[i][j] + B.M[i][j]) % 10;}}return res;}template <typename T>Matrix<T> operator^(Matrix<T> A, ll n) {assert(A.H == A.W);Matrix<T> res(A.H, A.W, 0);for (int i = 0; i < A.H; ++i) {res.M[i][i] = 1;}while (n) {if (n & 1) {res = res * A;}A = A * A;n >>= 1;}return res;}int main() {cin.tie(0);ios::sync_with_stdio(false);int p, q, r;ll K;cin >> p >> q >> r >> K;p %= 10;q %= 10;r %= 10;K -= 3;Matrix<int> A(3, 3);A.M[0][0] = 1;A.M[0][1] = 1;A.M[0][2] = 1;A.M[1][0] = 1;A.M[2][1] = 1;A = A ^ K;int ans = (A.M[0][0] * r + A.M[0][1] * q + A.M[0][2] * p) % 10;cout << ans << endl;return 0;}