結果
| 問題 |
No.1097 Remainder Operation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-14 09:55:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,974 bytes |
| コンパイル時間 | 2,041 ms |
| コンパイル使用メモリ | 138,256 KB |
| 最終ジャッジ日時 | 2025-01-17 00:26:01 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 19 TLE * 2 |
ソースコード
#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 : vs){
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];
}
int u = 0;
vector<ll> path_sum(1, 0);
while(u != graph[u][0] && scc_group[scc_id[u]].size() == 1){
path_sum.emplace_back(path_sum.back() + A[u]);
u = graph[u][0];
}
cin >> Q;
for(int i = 0; i < Q; ++i){
ll K;
cin >> K;
if(K < (ll)path_sum.size()){
cout << path_sum[K] << '\n';
continue;
}
int v = u;
ll ans = path_sum.back();
K -= (ll)path_sum.size() - 1;
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;
}