提出 #29700070


ソースコード 拡げる

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint998244353;
// using mint = modint1000000007;
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep2(i,k,n) for (int i = (k); i < (n); ++i)
using namespace std;
using ll = long long;
// using P = pair<ll,ll>;
using P = pair<int,int>;
using vint = vector<int>;
using vll = vector<ll>;
using vvint = vector<vector<int>>;
using vvll = vector<vector<ll>>;

// const ll INF = (ll)2e18+9;
const int INF = (int)2e9+7;
// const ll MOD = (ll)1e9+9;
template<typename T>
void chmin(T &a, T b) { a = min(a, b); }
template<typename T>
void chmax(T &a, T b) { a = max(a, b); }

template<typename T>
void print(vector<T> v) {
    int n = v.size();
    rep(i,n) {
        if (i == 0) cout << v[i];
        else cout << ' ' << v[i];
    }
    cout << endl;
}

void solve() {
    ll n, k;
    cin >> n >> k;
    vll A(n);
    rep(i,n) cin >> A[i];

    vint used(n);
    ll pos = 0, len = 0;
    ll tot = 0;
    vll memo(n, 0);
    while (!used[pos]) {
        // cout << pos << ' ' << A[pos] << ' ' << tot << endl;
        memo[pos] = len;
        used[pos] = 1;
        tot += A[pos];
        pos = tot % n;
        len++;
    }
    ll tail = memo[pos];
    // cout << pos << ' ' << A[pos] << ' ' << len << ' ' << tail << endl;
    ll tail_pos = pos;

    ll tail_tot = 0;
    int t = tail;
    pos = 0;
    while (t) {
        tail_tot += A[pos];
        pos = tail_tot % n;
        t--;
    }
    // cout << tail_tot << endl;

    ll circle_tot = tot - tail_tot;
    ll circle_len = len - tail;
    // cout << circle_len << endl;

    if (k <= len) {
        pos = 0;
        tot = 0;
        while (k) {
            tot += A[pos];
            pos = tot % n;
            k--;
        }
        cout << tot << endl;
        return;
    }

    ll ans = tail_tot;
    k -= tail;
    ans += circle_tot * (k / circle_len);
    k -= (k / circle_len) * circle_len;

    pos = tail_pos;
    while (k) {
        ans += A[pos];
        pos = ans % n;
        k--;
    }
    cout << ans << endl;
}

int main() {
    solve();
    return 0;
}

提出情報

提出日時
問題 E - Putting Candies
ユーザ goropikari
言語 C++ (GCC 9.2.1)
得点 500
コード長 2303 Byte
結果 AC
実行時間 71 ms
メモリ 7208 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 2
AC × 29
セット名 テストケース
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 8 ms 3536 KiB
example_01.txt AC 3 ms 3436 KiB
hand_00.txt AC 2 ms 3540 KiB
hand_01.txt AC 1 ms 3536 KiB
hand_02.txt AC 39 ms 7208 KiB
hand_03.txt AC 36 ms 6980 KiB
hand_04.txt AC 2 ms 3460 KiB
hand_05.txt AC 62 ms 7032 KiB
hand_06.txt AC 59 ms 7100 KiB
hand_07.txt AC 59 ms 6976 KiB
random_00.txt AC 58 ms 7204 KiB
random_01.txt AC 71 ms 7076 KiB
random_02.txt AC 56 ms 7108 KiB
random_03.txt AC 64 ms 7116 KiB
random_04.txt AC 59 ms 7144 KiB
random_05.txt AC 66 ms 6960 KiB
random_06.txt AC 55 ms 6960 KiB
random_07.txt AC 54 ms 7040 KiB
random_08.txt AC 68 ms 7120 KiB
random_09.txt AC 59 ms 7112 KiB
random_10.txt AC 57 ms 7112 KiB
random_11.txt AC 65 ms 7140 KiB
random_12.txt AC 58 ms 6960 KiB
random_13.txt AC 66 ms 7080 KiB
random_14.txt AC 63 ms 6964 KiB
random_15.txt AC 66 ms 7100 KiB
random_16.txt AC 61 ms 6964 KiB
random_17.txt AC 59 ms 7032 KiB
random_18.txt AC 59 ms 6968 KiB