結果
| 問題 |
No.1097 Remainder Operation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-10-04 18:24:43 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,061 bytes |
| コンパイル時間 | 2,160 ms |
| コンパイル使用メモリ | 200,464 KB |
| 最終ジャッジ日時 | 2025-01-24 20:13:35 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 10 TLE * 11 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
vector <int> adj[100000];
int A[100000];
vector <int> q;
int n,m;
long long int t;
int history[100000];
long long int visited[100000][2];
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
for(int i=0;i<n;i++)
{
cin >> A[i];
adj[i].push_back((i + A[i])%n);
}
cin >> m;
for(int i=0;i<m;i++)
{
cin >> t;
q.push_back(t);
}
memset(visited,-1,sizeof(visited));
memset(history,-1,sizeof(history));
visited[0][0] = 0;
visited[0][1] = 0;
queue <int> que;
que.push(0);
int pos = -1;
int pos2 = -1;
while(!que.empty())
{
int now = que.front();
que.pop();
for(auto next : adj[now])
{
if(visited[next][0]==-1)
{
visited[next][1] = visited[now][1] + A[now];
visited[next][0] = visited[now][0] + 1;
history[next] = now;
que.push(next);
}
else
{
pos = next;
pos2 = now;
break;
}
}
}
long long int L = visited[pos][0];
long long int P = visited[pos2][0] - visited[pos][0] + 1;
long long int SUM1 = visited[pos][1];
long long int SUM2 = visited[pos2][1] - visited[pos][1] + A[pos2];
for(int i=0;i<q.size();i++)
{
long long int res = 0;
if(q[i] < L)
{
int cnt = 0;
queue <int> que;
que.push(0);
while(!que.empty())
{
int now = que.front();
que.pop();
if(q[i]==cnt) break;
cnt++;
res += A[now];
for(auto next : adj[now])
{
que.push(next);
}
}
}
else
{
res += SUM1;
q[i]-=L;
res += ((q[i]/P)*SUM2);
q[i]%=P;
int cnt = 0;
queue <int> que;
que.push(pos);
while(!que.empty())
{
int now = que.front();
que.pop();
if(q[i]==cnt) break;
cnt++;
res += A[now];
for(auto next : adj[now])
{
que.push(next);
}
}
}
cout << res << '\n';
}
return 0;
}