結果

問題 No.1595 The Final Digit
ユーザー norikamenorikame
提出日時 2021-07-10 00:09:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,317 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 201,176 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 11:31:42
合計ジャッジ時間 3,110 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

int next_val(int val) {
    int sval = 0, tval = val;
    rep(i, 3) {
        sval += tval % 10;
        tval /= 10;
    }
    return (val * 10 % 1000 + sval % 10);
}

int main() {
    vector<int> a(3);
    rep(i, 3) cin >> a[i];
    ll k;
    cin >> k;
    vector<int> memo(1000, -1);
    int val = a[0] % 10 * 100 + a[1] % 10 * 10 + a[2] % 10;
    ll rcnt = 3;
    int res = -1;
    memo[val] = (int)rcnt;
    while (rcnt <= k) {
        ++rcnt;
        val = next_val(val);
        if (memo[val] == -1) memo[val] = (int)rcnt;
        else {
            ll roop = (int)rcnt - memo[val];
            ll rest = k - rcnt;
            rest %= roop;
            ll tar = memo[val] + (int)rest;
            rep(i, 1000) if (memo[i] == (int)(tar)) {
                res = i % 10;
                break;
            }
        }
        if (rcnt == k) res = val % 10;
        if (res != -1) break;
    }
    if (res == -1) res = val % 10;
    cout << res << endl;
    return 0;
}
0