結果

問題 No.1097 Remainder Operation
ユーザー uchiiiiuchiiii
提出日時 2020-06-27 13:05:24
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,198 bytes
コンパイル時間 2,984 ms
コンパイル使用メモリ 134,576 KB
実行使用メモリ 5,112 KB
最終ジャッジ日時 2023-08-20 15:46:49
合計ジャッジ時間 5,678 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma region
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std; typedef long double ld; typedef long long ll;
typedef unsigned long long ull;
#define endl "\n"
#define MP make_pair
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(x,arr) for(auto& x:arr)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define VPII vector<PII>
#define VPLL vector<PLL>
#define FI first 
#define SE second
#define ALL(x) (x).begin(), (x).end()
constexpr int INF=1<<30; constexpr ll LINF=1LL<<60; constexpr ll mod=1e9+7; constexpr int NIL = -1;
template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
#pragma endregion
//-------------------
int n;
const int MX = 1000000 + 5;
int A[MX];
vector<bool> visited(100000+5, false);

int main(){
    cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(15);
    cin >> n;
    FOR(i,0,n-1) {
        cin >> A[i];
    }
    int x = 0;
    vector<int> v;
    while(true) {
        v.emplace_back(x);
        if(visited[x]) break;
        visited[x] = true; 
        (x += A[x]) %= n;
    }


    int twice = v.back();
    v.pop_back();
    vector<int> loop;
    while(v.back() != twice) {
        loop.emplace_back(v.back());
        v.pop_back();
    }
    // cout << v.size() << endl;
    
    loop.emplace_back(twice);
    v.pop_back();
    reverse(ALL(loop));

    int p = v.size();
    int q = loop.size();
    // for(auto &x: v) cout << x << " ";
    // for(auto &x:loop) cout << x << " ";
    // cout << p << " " << q << endl; 
    vector<ll> front_sum(p+1, 0LL);
    vector<ll> loop_sum(q+1, 0LL);
    FOR(i,1,p) front_sum[i] = front_sum[i-1] + A[v[i-1]];
    FOR(i,1,q) loop_sum[i] = loop_sum[i-1] + A[loop[i-1]];

    int Q; cin >> Q;
    while(Q--) {
        ll k; cin >> k;
        if(k <= p) {
            cout << front_sum[k] << endl;
            continue;
        }
        ll ans = front_sum[p];
        k -= p;
        cout <<  ans + loop_sum[q] * (k/q) + loop_sum[k%q] << endl;
    }
    return 0;
}
0