結果

問題 No.1595 The Final Digit
ユーザー dekomori_sanaedekomori_sanae
提出日時 2021-07-09 21:35:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,524 bytes
コンパイル時間 851 ms
コンパイル使用メモリ 89,712 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 07:55:18
合計ジャッジ時間 1,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 10;
const ll INF = 1e16;

/* 
    線形k項間漸化式

    a_k = Σ d_i * a_i (0 <= i < k)
    
    の第n項の値のmodを O(k^2 * log(n)) で求める。(きたまさ法)

    ll k;  // k項間漸化式
    ll n;  // 第n項(nは0-index)の値を求める
    vl a;  // 初期値ベクトル (a_0,a_1,…,a_(k-1)の値)
    vl d;  // 係数ベクトル
*/

vl dfs(ll k, ll m, vl &a, vl &d) {  // a_m = Σ x_i * a_i (0 <= i < k) を満たす ベクトル (x_0,x_1,…,x_(k-1)) を求める
    if(m == k) { 
        return d;
    }
    if(m%2 == 1 || m < 2*k) {
        vvl f(2, vl(k));
        f[0] = dfs(k, m-1, a, d);
        // f[0] から f[1] を求める
        f[1][0] = d[0] * f[0][k-1] % mod;
        exrep(i, 1, k-1) {
            f[1][i] = (f[0][i-1] + d[i] * f[0][k-1]) % mod;
        }
        return f[1];
    }
    else {
        vvl f(k, vl(k));
        f[0] = dfs(k, m/2, a, d);
        // f[0] から f[1],f[2],…,f[k-1] を求める
        exrep(i, 1, k-1) {
            f[i][0] = d[0] * f[i-1][k-1] % mod;
            exrep(j, 1, k-1) {
                f[i][j] = (f[i-1][j-1] + d[j] * f[i-1][k-1]) % mod;
            }
        }
        vl f_2m(k);
        // f[0],f[1],…,f[k-1] から f[2m] を求める 
        rep(j, k) {
            rep(i, k) {
                f_2m[i] = (f_2m[i] + f[0][j] * f[j][i] % mod) % mod;
            }
        }
        return f_2m;
    }
}
 
ll kitamasa(ll k, ll n, vl &a, vl &d) {
    if(n < k) {
        return a[n];
    } 
    vl x = dfs(k, n, a, d);
    ll res = 0;
    rep(i, k) {
        res = (res + x[i] * a[i] % mod) % mod;
    }
    return res;
}
 
int main() {

    ll k = 3;

    vl a(3);
    rep(i, 3) {
        cin >> a[i];
    }

    ll n;
    cin >> n;
    n--;

    vl d(3, 1);
    
    out(kitamasa(k, n, a, d));
    re0;
}
0