結果

問題 No.1097 Remainder Operation
ユーザー milanis48663220milanis48663220
提出日時 2020-06-26 22:09:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 1,543 bytes
コンパイル時間 696 ms
コンパイル使用メモリ 85,284 KB
実行使用メモリ 5,452 KB
最終ジャッジ日時 2023-09-18 04:51:46
合計ジャッジ時間 4,805 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 17 ms
4,380 KB
testcase_08 AC 17 ms
4,376 KB
testcase_09 AC 16 ms
4,376 KB
testcase_10 AC 17 ms
4,376 KB
testcase_11 AC 17 ms
4,380 KB
testcase_12 AC 154 ms
4,532 KB
testcase_13 AC 154 ms
4,700 KB
testcase_14 AC 152 ms
4,720 KB
testcase_15 AC 153 ms
4,948 KB
testcase_16 AC 155 ms
4,848 KB
testcase_17 AC 153 ms
4,384 KB
testcase_18 AC 149 ms
5,304 KB
testcase_19 AC 154 ms
5,292 KB
testcase_20 AC 151 ms
5,452 KB
testcase_21 AC 152 ms
5,300 KB
testcase_22 AC 155 ms
5,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

int N;
int A[100000];
int B[100000];
int Q;
int idx[100000];
bool used[100000];

int s;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N; i++) cin >> A[i];
    cin >> Q;
    for(int i = 0; i < N; i++){
        int to = (i+A[i])%N;
        B[i] = to;
    }
    int cur = 0;
    int cnt = 0;
    int loop;
    int s;
    ll sum = 0;
    ll loop_sum = 0;
    vector<ll> v, v_loop;
    while(true){
        used[cur] = true;
        idx[cur] = cnt;
        cur = B[cur];
        cnt++;
        if(used[cur]) {
            s = cur;
            break;
        }
    }
    cur = 0;
    while(true){
        if(cur == s) break;
        sum += A[cur];
        v.push_back(sum);
        cur = B[cur];
    }
    cur = s;
    cnt = 0;
    while(true){
        if(cur == s && cnt > 0) break;
        loop_sum += A[cur];
        v_loop.push_back(loop_sum);
        cur = B[cur];
        cnt++;
    }
    for(int i = 0; i < Q; i++){
        ll K;
        ll ans = 0;
        cin >> K;
        if(K > v.size()){
            ans = sum;
            K -= v.size();
            ll m = K/(v_loop.size());
            ll r = K%(v_loop.size());
            ans += m*loop_sum;
            if(r > 0) ans += v_loop[r-1];
        }else{
            ans = v[K-1];
        }
        cout << ans << endl;
    }
}
0