結果

問題 No.1097 Remainder Operation
ユーザー outlineoutline
提出日時 2020-12-14 09:43:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,763 bytes
コンパイル時間 1,555 ms
コンパイル使用メモリ 142,200 KB
実行使用メモリ 20,396 KB
最終ジャッジ日時 2023-10-20 04:50:14
合計ジャッジ時間 7,940 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

constexpr int MAX = 2e+6 + 1;
constexpr ll MAXK = 1e+12;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N;
    vector<ll> A(N);
    for(int i = 0; i < N; ++i)  cin >> A[i];

    vector<vector<int>> graph(N), rev(N);
    for(int i = 0; i < N; ++i){
        int j = (i + A[i]) % N;
        graph[i].emplace_back(j);
        rev[j].emplace_back(i);
    }

    vector<int> vs, used(N);
    auto dfs = [&](auto&& self, int from) -> void {
        used[from] = true;
        for(int to : graph[from]){
            if(!used[to])   self(self, to);
        }
        vs.emplace_back(from);
    };
    for(int v = 0; v < N; ++v){
        if(!used[v])    dfs(dfs, v);
    }
    reverse(vs.begin(), vs.end());

    int scc_num = 0;
    vector<int> scc_id(N, -1);
    auto rdfs = [&](auto&& self, int from) -> void {
        scc_id[from] = scc_num;
        for(int to : rev[from]){
            if(scc_id[to] == -1)    self(self, to);
        }
    };
    for(int v = 0; v < N; ++v){
        if(scc_id[v] == -1){
            rdfs(rdfs, v);
            scc_num += 1;
        }
    }

    vector<vector<int>> scc_group(scc_num);
    vector<ll> sum(scc_num);
    for(int v = 0; v < N; ++v){
        scc_group[scc_id[v]].emplace_back(v);
        sum[scc_id[v]] += A[v];
    }

    cin >> Q;
    for(int i = 0; i < Q; ++i){
        ll K;
        cin >> K;
        int v = 0;
        ll ans = 0;
        while(K > 0 && scc_group[scc_id[v]].size() == 1){
            ans += A[v];
            v = graph[v][0];
            --K;
        }
        int sz = scc_group[scc_id[v]].size();
        ll loop_cnt = K / sz;
        ans += sum[scc_id[v]] * loop_cnt;
        K -= loop_cnt * sz;
        while(K > 0){
            ans += A[v];
            v = graph[v][0];
            --K;
        }
        cout << ans << '\n';
    }

    return 0;
}
0