problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02684
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < n; i++) #define REP(i, a, b) for (ll i = a; i <= b; i++) #define out(x) std::cout << x << '\n' #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define sz(x) ((int)(x).size()) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int INF = INT_MAX / 2; const ll inf = LLONG_MAX / 2; const int mod = 1000000007; const int MAX_N = 101010; const double PI = acos(-1); int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); /*------------------------------*/ ll n, k; cin >> n >> k; vector<int> a(n); rep(i, n) { cin >> a[i]; a[i]--; } vector<int> visited(n, 0); vector<int> start; int next = 0; rep(i, n + 1) { if (visited[next] > 0) { start.push_back(next); start.push_back(i); break; } visited[next]++; next = a[next]; } next = start[0]; rep(i, n) { if (a[next] == start[0]) { start.push_back(i); break; } next = a[next]; } if (k < start[1]) { next = 0; rep(i, k) { next = a[next]; } out(next + 1); } else { k -= start[1]; k = k % start[2]; next = start[0]; rep(i, k) { next = a[next]; } out(next + 1); } return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < n; i++) #define REP(i, a, b) for (ll i = a; i <= b; i++) #define out(x) std::cout << x << '\n' #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define sz(x) ((int)(x).size()) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int INF = INT_MAX / 2; const ll inf = LLONG_MAX / 2; const int mod = 1000000007; const int MAX_N = 101010; const double PI = acos(-1); int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); /*------------------------------*/ ll n, k; cin >> n >> k; vector<int> a(n); rep(i, n) { cin >> a[i]; a[i]--; } vector<int> visited(n, 0); vector<int> start; int next = 0; rep(i, n + 1) { if (visited[next] > 0) { start.push_back(next); start.push_back(i); break; } visited[next]++; next = a[next]; } next = start[0]; rep(i, n) { if (a[next] == start[0]) { start.push_back(i + 1); break; } next = a[next]; } if (k < start[1]) { next = 0; rep(i, k) { next = a[next]; } out(next + 1); } else { k -= start[1]; k = k % start[2]; next = start[0]; rep(i, k) { next = a[next]; } out(next + 1); } return 0; }
replace
59
60
59
60
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < n; i++) #define eb(t) emplace_back(t) typedef long long ll; typedef long long unsigned int llu; ll INF = 1000000009; ll MOD = 1000000007; ll a[20010]; void solve() { ll n, k; cin >> n >> k; rep(i, n) { cin >> a[i]; a[i]--; } set<ll> st; vector<ll> v; v.eb(0); st.insert(0); ll pointer = 0; ll t1 = 0; ll count1 = 1; while (true) { // cout << pointer << endl; decltype(st)::iterator it = st.find(a[pointer]); if (it != st.end()) { // 見つかった // cout << "find" << *it << std::endl; t1 = *it; break; } st.insert(a[pointer]); pointer = a[pointer]; count1++; v.eb(pointer); } ll count2 = 1; ll tm = 0; while (true) { if (a[tm] != t1) { tm = a[tm]; count2++; } else { break; } } /* for(auto e:v){ cout << e << "!" << endl; } */ // cout << count1 << " " << count2 << endl; ll y, x; if (count1 == count2) { y = count1; x = 0; } else { y = count1 - count2; x = count2; } if (k < x) { cout << v[k] + 1 << endl; return; } /* cout << v.size() << endl; cout << k << endl; cout << x << endl; cout << y << endl; cout << (k-x)%y+x << endl; */ cout << v[(k - x) % y + x] + 1 << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < n; i++) #define eb(t) emplace_back(t) typedef long long ll; typedef long long unsigned int llu; ll INF = 1000000009; ll MOD = 1000000007; ll a[200100]; void solve() { ll n, k; cin >> n >> k; rep(i, n) { cin >> a[i]; a[i]--; } set<ll> st; vector<ll> v; v.eb(0); st.insert(0); ll pointer = 0; ll t1 = 0; ll count1 = 1; while (true) { // cout << pointer << endl; decltype(st)::iterator it = st.find(a[pointer]); if (it != st.end()) { // 見つかった // cout << "find" << *it << std::endl; t1 = *it; break; } st.insert(a[pointer]); pointer = a[pointer]; count1++; v.eb(pointer); } ll count2 = 1; ll tm = 0; while (true) { if (a[tm] != t1) { tm = a[tm]; count2++; } else { break; } } /* for(auto e:v){ cout << e << "!" << endl; } */ // cout << count1 << " " << count2 << endl; ll y, x; if (count1 == count2) { y = count1; x = 0; } else { y = count1 - count2; x = count2; } if (k < x) { cout << v[k] + 1 << endl; return; } /* cout << v.size() << endl; cout << k << endl; cout << x << endl; cout << y << endl; cout << (k-x)%y+x << endl; */ cout << v[(k - x) % y + x] + 1 << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
replace
9
10
9
10
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 2e5 + 5; const int INF = 0x3f3f3f3f; #define DEBUG(x) cout << x << '\n' int n, a[MAXN], vis[MAXN], pos[MAXN]; ll k; int dfs(int u) { if (~vis[a[u]]) return a[u]; vis[a[u]] = vis[u] + 1; pos[vis[a[u]]] = a[u]; dfs(a[u]); } int main() { #ifdef Irene freopen("in.txt", "r", stdin); #endif // Irene ios::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i], vis[i] = -1; vis[1] = 0, pos[0] = 1; int x = dfs(1); int mx = *max_element(vis + 1, vis + n + 1) + 1; int ans; if (k < mx) ans = pos[k]; else { int circle = mx - vis[x]; k -= vis[x]; k %= circle; ans = pos[vis[x] + k]; } cout << ans; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 2e5 + 5; const int INF = 0x3f3f3f3f; #define DEBUG(x) cout << x << '\n' int n, a[MAXN], vis[MAXN], pos[MAXN]; ll k; int dfs(int u) { if (~vis[a[u]]) return a[u]; vis[a[u]] = vis[u] + 1; pos[vis[a[u]]] = a[u]; return dfs(a[u]); } int main() { #ifdef Irene freopen("in.txt", "r", stdin); #endif // Irene ios::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i], vis[i] = -1; vis[1] = 0, pos[0] = 1; int x = dfs(1); int mx = *max_element(vis + 1, vis + n + 1) + 1; int ans; if (k < mx) ans = pos[k]; else { int circle = mx - vis[x]; k -= vis[x]; k %= circle; ans = pos[vis[x] + k]; } cout << ans; }
replace
14
15
14
15
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define INF 1001001001 using ll = long long; using dd = long double; const int D = 60; const int MAX_N = 20005; int to[D][MAX_N]; int main() { int n; ll k; cin >> n >> k; rep(i, n) { cin >> to[0][i]; to[0][i]--; } rep(i, D - 1) rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; } int v = 0; for (int i = D - 1; i >= 0; --i) { ll l = 1ll << i; if (l <= k) { v = to[i][v]; k -= l; } } cout << v + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define INF 1001001001 using ll = long long; using dd = long double; const int D = 60; const int MAX_N = 200005; int to[D][MAX_N]; int main() { int n; ll k; cin >> n >> k; rep(i, n) { cin >> to[0][i]; to[0][i]--; } rep(i, D - 1) rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; } int v = 0; for (int i = D - 1; i >= 0; --i) { ll l = 1ll << i; if (l <= k) { v = to[i][v]; k -= l; } } cout << v + 1 << endl; return 0; }
replace
8
9
8
9
0
p02684
C++
Runtime Error
/* Author : Nishant Gupta 2.0 */ #include <bits/stdc++.h> #define LL long long int #define getcx getchar_unlocked #define X first #define Y second #define PB push_back #define MP make_pair #define MAX 200005 #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL (LL(1e18)) #define rep(i, a, n) for (i = a; i < n; i++) #define rev(i, a, n) for (i = a; i >= n; i--) #define MSV(A, a) memset(A, a, sizeof(A)) #define rep_itr(itr, c) for (itr = (c).begin(); itr != (c).end(); itr++) using namespace std; inline void inp(int &n) { n = 0; int ch = getcx(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getcx(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getcx(); n = n * sign; } inline void inp1(LL &n) { n = 0; LL ch = getcx(); LL sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getcx(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getcx(); n = n * sign; } vector<int> adj[MAX]; int st, e; int vis[MAX]; int cnt; int offset; void dfs(int node, int p) { if (vis[node]) { st = node; e = p; offset = vis[node] - 1; return; } vis[node] = cnt++; for (int i = 0; i < adj[node].size(); i++) { dfs(adj[node][i], node); } return; } vector<int> chain; void get_chain(int node, int dest) { if (node == dest) { chain.PB(dest); return; } chain.PB(node); for (int i = 0; i < adj[node].size(); i++) { get_chain(adj[node][i], dest); } } int main() { LL n, k; int i; cin >> n >> k; int x; for (i = 1; i <= n; i++) { cin >> x; adj[i].PB(x); } cnt = 1; dfs(1, 1); k -= offset; get_chain(st, e); int cn = chain.size(); k = k % cn; cout << chain[k] << endl; return 0; }
/* Author : Nishant Gupta 2.0 */ #include <bits/stdc++.h> #define LL long long int #define getcx getchar_unlocked #define X first #define Y second #define PB push_back #define MP make_pair #define MAX 200005 #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL (LL(1e18)) #define rep(i, a, n) for (i = a; i < n; i++) #define rev(i, a, n) for (i = a; i >= n; i--) #define MSV(A, a) memset(A, a, sizeof(A)) #define rep_itr(itr, c) for (itr = (c).begin(); itr != (c).end(); itr++) using namespace std; inline void inp(int &n) { n = 0; int ch = getcx(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getcx(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getcx(); n = n * sign; } inline void inp1(LL &n) { n = 0; LL ch = getcx(); LL sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getcx(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getcx(); n = n * sign; } vector<int> adj[MAX]; int st, e; int vis[MAX]; int cnt; int offset; void dfs(int node, int p) { if (vis[node]) { st = node; e = p; offset = vis[node] - 1; return; } vis[node] = cnt++; for (int i = 0; i < adj[node].size(); i++) { dfs(adj[node][i], node); } return; } vector<int> chain; void get_chain(int node, int dest) { if (node == dest) { chain.PB(dest); return; } chain.PB(node); for (int i = 0; i < adj[node].size(); i++) { get_chain(adj[node][i], dest); } } int main() { LL n, k; int i; cin >> n >> k; int x; for (i = 1; i <= n; i++) { cin >> x; adj[i].PB(x); } cnt = 1; dfs(1, 1); if (k < offset) { get_chain(1, st); cout << chain[k] << endl; return 0; } k -= offset; get_chain(st, e); int cn = chain.size(); k = k % cn; cout << chain[k] << endl; return 0; }
insert
98
98
98
103
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long N, K; cin >> N >> K; vector<long> array(N + 1); array[0] = 0; vector<int> reach(N + 1); vector<long> loop(0); for (int i = 0; i < N; i++) cin >> array[i + 1]; long place = 1; long count = 0; long mark = 0; long j = 0; while (reach[place] < 3) { reach[place]++; if (reach[place] == 2 && count == 0) mark = j - 1; if (reach[place] == 2) { loop.push_back(place); count++; } place = array[place]; j++; } long ans = (K - mark) % count - 1; if (ans == -1) ans = count - 1; cout << loop[ans] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long N, K; cin >> N >> K; vector<long> array(N + 1); array[0] = 0; vector<int> reach(N + 1); vector<long> loop(0); for (int i = 0; i < N; i++) cin >> array[i + 1]; long place = 1; long count = 0; long mark = 0; long j = 0; while (reach[place] < 3) { reach[place]++; if (reach[place] == 2 && count == 0) mark = j - 1; if (reach[place] == 2) { loop.push_back(place); count++; } place = array[place]; j++; if (j == K) { cout << place << endl; return 0; } } long ans = (K - mark) % count - 1; if (ans == -1) ans = count - 1; cout << loop[ans] << endl; }
insert
27
27
27
31
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long N, K; cin >> N >> K; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; a[i]--; } long now = 0; vector<int> v(N); while (K > 0) { now = a[now]; K--; // cout<<now+1<<endl; if (v[now] == 1) { v[now]++; break; } else v[now]++; } // cout<<"K"<<K<<endl; long loop = 0; while (K > 0) { now = a[now]; K--; loop++; // cout<<now+1<<endl; if (v[now] == 2) { v[now]++; break; } else v[now]++; } // cout<<"K"<<K<<endl; // cout<<loop<<endl; K %= loop; while (K > 0) { now = a[now]; K--; // cout<<now+1<<endl; } cout << now + 1 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long N, K; cin >> N >> K; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; a[i]--; } long now = 0; vector<int> v(N); while (K > 0) { now = a[now]; K--; // cout<<now+1<<endl; if (v[now] == 1) { v[now]++; break; } else v[now]++; } // cout<<"K"<<K<<endl; long loop = 0; while (K > 0) { now = a[now]; K--; loop++; // cout<<now+1<<endl; if (v[now] == 2) { v[now]++; break; } else v[now]++; } // cout<<"K"<<K<<endl; // cout<<loop<<endl; if (loop > 0) K %= loop; while (K > 0) { now = a[now]; K--; // cout<<now+1<<endl; } cout << now + 1 << endl; }
replace
39
40
39
41
0
p02684
C++
Runtime Error
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; void print() {} template <class H, class... T> void print(H &&h, T &&...t) { cout << h << " \n"[sizeof...(t) == 0]; print(forward<T>(t)...); } int nxt[62][200010] = {}; void solve() { ll N, K; cin >> N >> K; ll A[200010]; rep(i, 0, N) { cin >> A[i]; A[i]--; } rep(i, 0, N) { nxt[0][i] = A[i]; } rep(k, 0, 62) { rep(i, 0, N) { if (nxt[k][i] == -1) { nxt[k + 1][i] = -1; } else { nxt[k + 1][i] = nxt[k][nxt[k][i]]; } } } ll pos = 0; rrep(b, 62, 0) { if ((K >> b) & 1) { pos = nxt[b][pos]; } } print(pos + 1); } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; void print() {} template <class H, class... T> void print(H &&h, T &&...t) { cout << h << " \n"[sizeof...(t) == 0]; print(forward<T>(t)...); } int nxt[65][200010] = {}; void solve() { ll N, K; cin >> N >> K; ll A[200010]; rep(i, 0, N) { cin >> A[i]; A[i]--; } rep(i, 0, N) { nxt[0][i] = A[i]; } rep(k, 0, 62) { rep(i, 0, N) { if (nxt[k][i] == -1) { nxt[k + 1][i] = -1; } else { nxt[k + 1][i] = nxt[k][nxt[k][i]]; } } } ll pos = 0; rrep(b, 62, 0) { if ((K >> b) & 1) { pos = nxt[b][pos]; } } print(pos + 1); } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
replace
18
19
18
19
0
p02684
C++
Runtime Error
/* AtCoderBeginnerContest167 Problem D - Teleporter / Time Limit: 2 sec / Memory Limit: 1024 MB Score: 400 points */ #include <iostream> #include <vector> int main() { int N; long K; std::vector<int> A; A.push_back(0); std::cin >> N >> K; for (int i = 0; i < N; i++) { int currA; std::cin >> currA; A.push_back(currA); } std::vector<int> vis(N + 1, -1); std::vector<int> loc; int T = 0; int pos = 1; while (vis[pos] == -1) { vis[pos] = T; loc.push_back(pos); T++; pos = A[pos]; } int prevT = vis[pos]; if (K >= prevT) { int slack = K - prevT; int period = T - prevT; slack %= period; K = prevT + slack; } std::cout << loc[K] << "\n"; }
/* AtCoderBeginnerContest167 Problem D - Teleporter / Time Limit: 2 sec / Memory Limit: 1024 MB Score: 400 points */ #include <iostream> #include <vector> int main() { int N; long K; std::vector<int> A; A.push_back(0); std::cin >> N >> K; for (int i = 0; i < N; i++) { int currA; std::cin >> currA; A.push_back(currA); } std::vector<int> vis(N + 1, -1); std::vector<int> loc; int T = 0; int pos = 1; while (vis[pos] == -1) { vis[pos] = T; loc.push_back(pos); T++; pos = A[pos]; } int prevT = vis[pos]; if (K >= prevT) { long slack = K - prevT; int period = T - prevT; slack %= period; K = prevT + slack; } std::cout << loc[K] << "\n"; }
replace
35
36
35
36
0
p02684
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> // #include <stack> // #include <queue> // #include <cstdio> #include <cmath> #include <iterator> #include <map> // #include <list> #include <iomanip> using namespace std; #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define prec std::cout << std::fixed << std::setprecision(15); #define endl "\n" #define MOD 1000000007 #define Int int64_t #define PI 3.14159265358979 #define ssort(z) sort(z.begin(), z.end()) #define rsort(z) sort(z.rbegin(), z.rend()) #define eerase(z) z.erase(unique(z.begin(), z.end()), z.end()) #define ccnt(z, w) count(z.begin(), z.end(), w) #define rep(i, a, n) for (Int(i) = (a); (i) < (n); (i)++) #define repq(i, a, n) for (Int(i) = (a); (i) <= (n); (i)++) const int MAX_N = 1000000; const Int MAX_N_Int = 1000000000000; template <typename T> void printV(const std::vector<T> &v, const char *const separator = " ") { if (!v.empty()) { std::copy(v.begin(), --v.end(), std::ostream_iterator<T>(std::cout, separator)); std::cout << v.back() << "\n"; } } bool isPrime(int num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; // 偶数はあらかじめ除く double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) { // 素数ではない return false; } } // 素数である return true; } Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; } Int lcm(Int a, Int b) { return a / gcd(a, b) * b; // a*bは64bit integer overflow } int Max(int a, int b, int c) { int temp = max(a, b); return max(temp, c); } int Min(int a, int b, int c) { int temp = min(a, b); return min(temp, c); } bool integer(double num) { return floor(num) == num; } Int fact(int num) { if (num == 0) return 1; else return num * fact(num - 1); } Int yakusu(int n) { int cnt = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { cnt++; if (i * i != n) cnt++; } } return cnt; } Int fact_mod(Int n, Int mod) { Int f = 1; repq(i, 2, n) f = f * (i % MOD) % MOD; return f; } // 繰り返し二乗法 (modの世界での累乗) Int mod_pow(Int x, Int n, Int mod) { Int res = 1; while (n > 0) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n >>= 1; } return res; } // 組み合わせ nCr を求める Int combination_mod(Int n, Int r, Int mod) { if (r > n - r) r = n - r; if (r == 0) return 1; Int a = 1; rep(i, 0, r) a = a * ((n - i) % mod) % mod; Int b = mod_pow(fact_mod(r, mod), mod - 2, mod); return (a % mod) * (b % mod) % mod; } bool rev(string s) { string t = s; reverse(s.begin(), s.end()); return s == t; } int main() { // COMinit(); IOS; prec; Int a, b, c, n, m, k, x, y = 0, ans = 0; string str, s, t, u; cin >> n >> k; pair<Int, Int> p; vector<pair<Int, Int>> v; for (int i = 0; i < n; ++i) { cin >> a; a--; // if(i!=a){ p.first = i; p.second = a; v.push_back(p); //} } // for (int j = 0; j < v.size(); ++j) { // cout<<v[j].first<<" "<<v[j].second+1<<endl; // } vector<bool> used(n); vector<Int> id(n); vector<Int> num; bool isLoop = false; Int now = 0, cnt = 1; Int loopSize = 0; Int offSet = 0; while (not isLoop) { // cout<<"now; "<<now<<endl; if (used[v.at(now).second]) { isLoop = true; // cout<<cnt<<endl; // printV(used); loopSize = (cnt)-id[v.at(now).second]; offSet = cnt - loopSize - 1; } else { num.push_back(v.at(now).second + 1); used[v.at(now).second] = true; id[v.at(now).second] = cnt; cnt++; now = v.at(now).second; } } // printV(num); k -= offSet; if (k <= 0) { cout << num.at(k - 1); } else { b = k % loopSize; if (b != 0) cout << num.at(offSet + b - 1); else { cout << num[num.size() - 1]; } } cout << endl; return 0; } /* テレビも無ェ ラジオも無ェ 自動車もそれほど走って無ェ ピアノも無ェ バーも無ェ 巡査 毎日ぐーるぐる 朝起ぎで 牛連れで 二時間ちょっとの散歩道 電話も無ェ 瓦斯も無ェ バスは一日一度来る 俺らこんな村いやだ 俺らこんな村いやだ 東京へ出るだ 東京へ出だなら 銭コァ貯めで 東京でベコ(牛)飼うだ */
#include <algorithm> #include <iostream> #include <string> #include <vector> // #include <stack> // #include <queue> // #include <cstdio> #include <cmath> #include <iterator> #include <map> // #include <list> #include <iomanip> using namespace std; #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define prec std::cout << std::fixed << std::setprecision(15); #define endl "\n" #define MOD 1000000007 #define Int int64_t #define PI 3.14159265358979 #define ssort(z) sort(z.begin(), z.end()) #define rsort(z) sort(z.rbegin(), z.rend()) #define eerase(z) z.erase(unique(z.begin(), z.end()), z.end()) #define ccnt(z, w) count(z.begin(), z.end(), w) #define rep(i, a, n) for (Int(i) = (a); (i) < (n); (i)++) #define repq(i, a, n) for (Int(i) = (a); (i) <= (n); (i)++) const int MAX_N = 1000000; const Int MAX_N_Int = 1000000000000; template <typename T> void printV(const std::vector<T> &v, const char *const separator = " ") { if (!v.empty()) { std::copy(v.begin(), --v.end(), std::ostream_iterator<T>(std::cout, separator)); std::cout << v.back() << "\n"; } } bool isPrime(int num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; // 偶数はあらかじめ除く double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) { // 素数ではない return false; } } // 素数である return true; } Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; } Int lcm(Int a, Int b) { return a / gcd(a, b) * b; // a*bは64bit integer overflow } int Max(int a, int b, int c) { int temp = max(a, b); return max(temp, c); } int Min(int a, int b, int c) { int temp = min(a, b); return min(temp, c); } bool integer(double num) { return floor(num) == num; } Int fact(int num) { if (num == 0) return 1; else return num * fact(num - 1); } Int yakusu(int n) { int cnt = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { cnt++; if (i * i != n) cnt++; } } return cnt; } Int fact_mod(Int n, Int mod) { Int f = 1; repq(i, 2, n) f = f * (i % MOD) % MOD; return f; } // 繰り返し二乗法 (modの世界での累乗) Int mod_pow(Int x, Int n, Int mod) { Int res = 1; while (n > 0) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n >>= 1; } return res; } // 組み合わせ nCr を求める Int combination_mod(Int n, Int r, Int mod) { if (r > n - r) r = n - r; if (r == 0) return 1; Int a = 1; rep(i, 0, r) a = a * ((n - i) % mod) % mod; Int b = mod_pow(fact_mod(r, mod), mod - 2, mod); return (a % mod) * (b % mod) % mod; } bool rev(string s) { string t = s; reverse(s.begin(), s.end()); return s == t; } int main() { // COMinit(); IOS; prec; Int a, b, c, n, m, k, x, y = 0, ans = 0; string str, s, t, u; cin >> n >> k; pair<Int, Int> p; vector<pair<Int, Int>> v; for (int i = 0; i < n; ++i) { cin >> a; a--; // if(i!=a){ p.first = i; p.second = a; v.push_back(p); //} } // for (int j = 0; j < v.size(); ++j) { // cout<<v[j].first<<" "<<v[j].second+1<<endl; // } vector<bool> used(n); vector<Int> id(n); vector<Int> num; bool isLoop = false; Int now = 0, cnt = 1; Int loopSize = 0; Int offSet = 0; while (not isLoop) { // cout<<"now; "<<now<<endl; if (used[v.at(now).second]) { isLoop = true; // cout<<cnt<<endl; // printV(used); loopSize = (cnt)-id[v.at(now).second]; offSet = cnt - loopSize - 1; } else { num.push_back(v.at(now).second + 1); used[v.at(now).second] = true; id[v.at(now).second] = cnt; cnt++; now = v.at(now).second; } } // printV(num); k -= offSet; if (k <= 0) { cout << num.at(k + offSet - 1); } else { b = k % loopSize; if (b != 0) cout << num.at(offSet + b - 1); else { cout << num[num.size() - 1]; } } cout << endl; return 0; } /* テレビも無ェ ラジオも無ェ 自動車もそれほど走って無ェ ピアノも無ェ バーも無ェ 巡査 毎日ぐーるぐる 朝起ぎで 牛連れで 二時間ちょっとの散歩道 電話も無ェ 瓦斯も無ェ バスは一日一度来る 俺らこんな村いやだ 俺らこんな村いやだ 東京へ出るだ 東京へ出だなら 銭コァ貯めで 東京でベコ(牛)飼うだ */
replace
178
179
178
179
0
p02684
C++
Runtime Error
#include <stdio.h> int main(void) { int N, i, j, x, y, MOD, A[200000] = {0}, check[200000] = {}, move[200000] = {0}; long long K; x = scanf("%d %lld", &N, &K); for (i = 0; i < N; i++) x = scanf("%d", &A[i]), check[i] = -1; for (i = 0, j = 0;; i = A[i] - 1, j++) if (check[i] < 0) check[i] = j, move[j] = i; else { x = j - 1, y = check[i], MOD = x - y - 1; break; } if (K > x) K = (K - y) % MOD + y; return printf("%d\n", move[K] + 1), 0; }
#include <stdio.h> int main(void) { int N, i, j, x, y, MOD, A[200000] = {0}, check[200000] = {}, move[200000] = {0}; long long K; x = scanf("%d %lld", &N, &K); for (i = 0; i < N; i++) x = scanf("%d", &A[i]), check[i] = -1; for (i = 0, j = 0;; i = A[i] - 1, j++) if (check[i] < 0) check[i] = j, move[j] = i; else { x = j - 1, y = check[i], MOD = x - y + 1; break; } if (K > x) K = (K - y) % MOD + y; return printf("%d\n", move[K] + 1), 0; }
replace
12
13
12
13
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define FIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int32_t main() { FIO; int n, k; cin >> n >> k; vi v; v.pb(1); mk(arr, n + 1, int); for (int i = 1; i <= n; ++i) cin >> arr[i]; int cur = arr[1]; set<int> vis; vis.insert(1); while (!vis.count(cur)) { v.pb(cur); vis.insert(cur); cur = arr[cur]; } int id; for (int i = 0; i < v.size(); ++i) { if (cur == v[i]) { id = i; break; } } if (k < id) { cout << v[k] << '\n'; } vi tmp; for (int i = id; i < v.size(); ++i) tmp.pb(v[i]); v = tmp; k -= id; n = v.size(); k %= n; cout << v[k] << '\n'; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define FIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int32_t main() { FIO; int n, k; cin >> n >> k; vi v; v.pb(1); mk(arr, n + 1, int); for (int i = 1; i <= n; ++i) cin >> arr[i]; int cur = arr[1]; set<int> vis; vis.insert(1); while (!vis.count(cur)) { v.pb(cur); vis.insert(cur); cur = arr[cur]; } int id; for (int i = 0; i < v.size(); ++i) { if (cur == v[i]) { id = i; break; } } if (k < id) { cout << v[k] << '\n'; return 0; } vi tmp; for (int i = id; i < v.size(); ++i) tmp.pb(v[i]); v = tmp; k -= id; n = v.size(); k %= n; cout << v[k] << '\n'; return 0; }
insert
68
68
68
69
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1e9 + 7; constexpr ll INF = 1e18; int main() { ll n, m, i, j, k; cin >> n >> k; vector<ll> A(n); for (auto &x : A) { cin >> x; } ll cnt = 0, now = 0; vector<ll> used(n, -1); used[0] = 0; for (i = 0; i < n; i++) { now = A[now] - 1; cnt++; if (used[now] >= 0) break; used[now] = i + 1; } // now が 始点 ll ch = 0, s = 0; if (now != 0) { for (i = 0; i < n; i++) { ch = A[ch] - 1; s++; if (ch == now) break; } } vector<ll> loop; ch = now; do { loop.push_back(ch); ch = A[ch] - 1; } while (ch != now); ll c = (k - s) % (cnt - s); cout << loop[c] + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1e9 + 7; constexpr ll INF = 1e18; int main() { ll n, m, i, j, k; cin >> n >> k; vector<ll> A(n); for (auto &x : A) { cin >> x; } ll cnt = 0, now = 0; vector<ll> used(n, -1); used[0] = 0; for (i = 0; i < n; i++) { now = A[now] - 1; cnt++; if (used[now] >= 0) break; used[now] = i + 1; if (cnt == k) { cout << now + 1 << endl; return 0; } } // now が 始点 ll ch = 0, s = 0; if (now != 0) { for (i = 0; i < n; i++) { ch = A[ch] - 1; s++; if (ch == now) break; } } vector<ll> loop; ch = now; do { loop.push_back(ch); ch = A[ch] - 1; } while (ch != now); ll c = (k - s) % (cnt - s); cout << loop[c] + 1 << endl; return 0; }
insert
24
24
24
28
0
p02684
C++
Runtime Error
#include <algorithm> #include <cmath> #include <ctime> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <stdlib.h> #include <string> #include <vector> using namespace std; int main() { long long n, k; cin >> n >> k; vector<long long> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; a[i]--; } vector<bool> was(n, false); long long cycle = 1, now = 0; vector<long long> path; while (!was[now]) { was[now] = true; now = a[now]; k--; } long long st = now; path.push_back(st); now = a[now]; while (now != st) { path.push_back(now); cycle++; now = a[now]; } k %= cycle; cout << path[k] + 1; }
#include <algorithm> #include <cmath> #include <ctime> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <stdlib.h> #include <string> #include <vector> using namespace std; int main() { long long n, k; cin >> n >> k; vector<long long> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; a[i]--; } vector<bool> was(n, false); long long cycle = 1, now = 0; vector<long long> path; while (!was[now]) { was[now] = true; now = a[now]; k--; if (!k) { cout << now + 1; return 0; } } long long st = now; path.push_back(st); now = a[now]; while (now != st) { path.push_back(now); cycle++; now = a[now]; } k %= cycle; cout << path[k] + 1; }
insert
32
32
32
36
0
p02684
C++
Runtime Error
// cand <genesis?> #include <bits/stdc++.h> #define ll long long #define ld long double using namespace std; const ll base = 7; const ll N = 1e5 + 9; const ll inf = 1000100009; typedef pair<ll, ll> LL; ll n, k, a[N], i, chk[N]; vector<ll> pos; ll getpos(ll x) { for (ll i = 0; i < pos.size(); i++) if (pos[i] == x) return i; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); // freopen("test.INP", "r", stdin); cin >> n >> k; for (i = 1; i <= n; i++) cin >> a[i]; i = 1; while (chk[i] != 1) { // cout<<i<<"x\n"; chk[i] = 1; pos.push_back(i); i = a[i]; } ll p = getpos(i); // cout<<i<<"\n"; if (k <= p) cout << pos[k]; else { k -= p; k %= (pos.size() - p); cout << pos[p + k]; } }
// cand <genesis?> #include <bits/stdc++.h> #define ll long long #define ld long double using namespace std; const ll base = 7; const ll N = 3e5 + 9; const ll inf = 1000100009; typedef pair<ll, ll> LL; ll n, k, a[N], i, chk[N]; vector<ll> pos; ll getpos(ll x) { for (ll i = 0; i < pos.size(); i++) if (pos[i] == x) return i; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); // freopen("test.INP", "r", stdin); cin >> n >> k; for (i = 1; i <= n; i++) cin >> a[i]; i = 1; while (chk[i] != 1) { // cout<<i<<"x\n"; chk[i] = 1; pos.push_back(i); i = a[i]; } ll p = getpos(i); // cout<<i<<"\n"; if (k <= p) cout << pos[k]; else { k -= p; k %= (pos.size() - p); cout << pos[p + k]; } }
replace
6
7
6
7
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; int main() { int N, K, x, d1 = -1, d2 = -1; set<int> s; s.insert(0); cin >> N >> K; vector<int> A(N), t(N, 0); rep(i, N) cin >> A.at(i); rep(i, N - 1) { t.at(i + 1) = A.at(t.at(i)) - 1; if (s.count(t.at(i + 1))) { x = t.at(i + 1); break; } s.insert(t.at(i + 1)); } rep(i, N) if (t.at(i) == x) if (d1 == -1) { d1 = i; } else { d2 = i; break; } if (K <= d1) cout << t.at(K) + 1 << endl; else cout << t.at(((K - d1) % (d2 - d1)) + d1) + 1 << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; int main() { int N, x, d1 = -1, d2 = -1; ll K; set<int> s; s.insert(0); cin >> N >> K; vector<int> A(N), t(N, 0); rep(i, N) cin >> A.at(i); rep(i, N - 1) { t.at(i + 1) = A.at(t.at(i)) - 1; if (s.count(t.at(i + 1))) { x = t.at(i + 1); break; } s.insert(t.at(i + 1)); } rep(i, N) if (t.at(i) == x) if (d1 == -1) { d1 = i; } else { d2 = i; break; } if (K <= d1) cout << t.at(K) + 1 << endl; else cout << t.at(((K - d1) % (d2 - d1)) + d1) + 1 << endl; }
replace
6
7
6
8
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; int main(void) { int N; long long K; int a; cin >> N >> K; Graph G(N); for (int i = 0; i < N; i++) { cin >> a; G[i].push_back(a); } vector<int> seen; vector<int> ord(N + 1, -1); int now = 1; int l = 0; int c = 1; while (1) { if (ord[now] != -1) { break; } ord[now] = seen.size(); seen.push_back(now); now = G[now][0]; } c = seen.size() - ord[now]; l = ord[now]; if (K < l) { cout << seen[K] << endl; } else { cout << seen[l + ((K - l) % c)] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; int main(void) { int N; long long K; int a; cin >> N >> K; Graph G(N); for (int i = 0; i < N; i++) { cin >> a; G[i].push_back(a); } vector<int> seen; vector<int> ord(N + 1, -1); int now = 1; int l = 0; int c = 1; while (1) { if (ord[now] != -1) { break; } ord[now] = seen.size(); seen.push_back(now); now = G[now - 1][0]; } c = seen.size() - ord[now]; l = ord[now]; if (K < l) { cout << seen[K] << endl; } else { cout << seen[l + ((K - l) % c)] << endl; } return 0; }
replace
28
29
28
29
-11
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, n) for (int i = (int)(n); i >= 0; i--) #define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++) #define all(v) v.begin(), v.end() typedef long long ll; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; int main() { int n; ll k; cin >> n >> k; vector<int> a(n); rep(i, n) { cin >> a[i]; a[i]--; } vector<bool> b(n, false); vector<int> c; int x = 0; int pos = 0; rep(i, n * 2) { if (!b[pos]) { b[pos] = true; c.push_back(pos); pos = a[pos]; } else { x = pos; break; } } vector<int> d; bool ok = false; rep(i, c.size()) { if (c[i] == x) ok = true; if (ok) d.push_back(c[i]); } int l = d.size(); int jama = c.size() - l; cout << d[((k - jama) % l)] + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, n) for (int i = (int)(n); i >= 0; i--) #define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++) #define all(v) v.begin(), v.end() typedef long long ll; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; int main() { int n; ll k; cin >> n >> k; vector<int> a(n); rep(i, n) { cin >> a[i]; a[i]--; } vector<bool> b(n, false); vector<int> c; int x = 0; int pos = 0; rep(i, n * 2) { if (!b[pos]) { b[pos] = true; c.push_back(pos); pos = a[pos]; } else { x = pos; break; } } vector<int> d; bool ok = false; rep(i, c.size()) { if (c[i] == x) ok = true; if (ok) d.push_back(c[i]); } int l = d.size(); int jama = c.size() - l; if (k < jama) { cout << c[k] + 1 << endl; } else { cout << d[(k - jama) % l] + 1 << endl; } return 0; }
replace
61
62
61
66
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int p; cin >> p; vector<int> a(n), t(n, -1); for (int i = 0; i < n; i++) { cin >> a[i]; --a[i]; } int start = 0; int m_time = 0; t[start] = m_time++; int cycle_length = 0; vector<pair<int, int>> cycle; while (true) { int prev = start; start = a[start]; if (t[start] != -1) { cycle_length = t[prev] - t[start] + 1; for (int i = 0; i < n; i++) { if (t[i] >= t[start]) { cycle.push_back({t[i], i}); } } break; } t[start] = m_time++; } p -= m_time - cycle_length; sort(cycle.begin(), cycle.end()); p %= cycle_length; cout << cycle[p].second + 1 << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int p; cin >> p; vector<int> a(n), t(n, -1); for (int i = 0; i < n; i++) { cin >> a[i]; --a[i]; } int start = 0; int m_time = 0; t[start] = m_time++; int cycle_length = 0; vector<pair<int, int>> cycle; while (true) { int prev = start; start = a[start]; if (t[start] != -1) { cycle_length = t[prev] - t[start] + 1; for (int i = 0; i < n; i++) { if (t[i] >= t[start]) { cycle.push_back({t[i], i}); } } break; } t[start] = m_time++; if (1ll * m_time == p + 1) { cout << start + 1 << "\n"; return 0; } } p -= m_time - cycle_length; sort(cycle.begin(), cycle.end()); p %= cycle_length; cout << cycle[p].second + 1 << "\n"; return 0; }
insert
32
32
32
36
0
p02684
C++
Time Limit Exceeded
#include "bits/stdc++.h" #define rep(i, j) for (int i = 0; i < j; i++) using namespace std; using ll = long long; /*struct mint { ll x; // typedef long long ll; mint(ll x = 0) :x((x% mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream& operator>>(istream & is, const mint & a) { return is >> a.x; } ostream& operator<<(ostream & os, const mint & a) { return os << a.x; }*/ int main() { ll n, k; cin >> n >> k; vector<ll> a(n); vector<ll> exp(n, 0); vector<ll> rode(0); rep(i, n) cin >> a[i]; int cu_to = 0; int kuri = 0; while (1) { exp[cu_to]++; if (exp[cu_to] == 2) { while (1) { if (rode[0] != cu_to) { k--; rode.erase(rode.begin() + 0); } else { break; } } break; } rode.emplace_back(cu_to); cu_to = a[cu_to] - 1; kuri++; if (kuri == k) { cout << cu_to + 1 << endl; return 0; } } int rem = k % rode.size(); cout << rode[rem] + 1 << endl; return 0; }
#include "bits/stdc++.h" #define rep(i, j) for (int i = 0; i < j; i++) using namespace std; using ll = long long; /*struct mint { ll x; // typedef long long ll; mint(ll x = 0) :x((x% mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream& operator>>(istream & is, const mint & a) { return is >> a.x; } ostream& operator<<(ostream & os, const mint & a) { return os << a.x; }*/ int main() { ll n, k; cin >> n >> k; vector<int> a(n); vector<int> exp(n, 0); vector<int> rode(0); rep(i, n) cin >> a[i]; int cu_to = 0; int kuri = 0; while (1) { exp[cu_to]++; if (exp[cu_to] == 2) { while (1) { if (rode[0] != cu_to) { k--; rode.erase(rode.begin() + 0); } else { break; } } break; } rode.emplace_back(cu_to); cu_to = a[cu_to] - 1; kuri++; if (kuri == k) { cout << cu_to + 1 << endl; return 0; } } int rem = k % rode.size(); cout << rode[rem] + 1 << endl; return 0; }
replace
40
43
40
43
TLE
p02684
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #include <math.h> #include <queue> #include <stdio.h> #define ll long long #define N 10000 #define mod 1000000007 #define LL 100 using namespace std; int main() { ll n, k; cin >> n >> k; ll num[n + 1]; for (int i = 1; i <= n; i++) cin >> num[i]; map<ll, ll> data; ll count1 = 1; ll count2 = 0; ll ans = 1; // 現在の位置 vector<ll> pp; data[1]++; // cout<<num[1]; while (true) { // cout<<ans<<endl; ans = num[ans]; if (data[ans] == 0) { count1++; data[ans]++; } else if (data[ans] == 1) { pp.push_back(ans); count2++; data[ans]++; } else break; } // cout<<pp[2]<<endl; ll p = k - count1; // cout<<count1<<endl; ll r = p % count2; // cout<<count2<<endl; cout << pp[r] << endl; }
#include <bits/stdc++.h> #include <iostream> #include <math.h> #include <queue> #include <stdio.h> #define ll long long #define N 10000 #define mod 1000000007 #define LL 100 using namespace std; int main() { ll n, k; cin >> n >> k; ll num[n + 1]; for (int i = 1; i <= n; i++) cin >> num[i]; map<ll, ll> data; ll count1 = 1; ll count2 = 0; ll ans = 1; // 現在の位置 vector<ll> pp; data[1]++; // cout<<num[1]; while (true) { // cout<<ans<<endl; ans = num[ans]; if (data[ans] == 0) { count1++; data[ans]++; } else if (data[ans] == 1) { pp.push_back(ans); count2++; data[ans]++; } else break; } if (k >= count1) { ll p = k - count1; // cout<<count1<<endl; ll r = p % count2; // cout<<count2<<endl; cout << pp[r] << endl; } else { ans = 1; for (int i = 0; i < k; i++) { ans = num[ans]; } cout << ans << endl; } }
replace
39
45
39
52
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define enld '\n' #define rep(i, n) for (int i = 0; i < (n); i++) #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("Ofast") constexpr ll INF = 1e18; constexpr int inf = 1e9; constexpr ll mod = 1000000007; constexpr ll mod2 = 998244353; const double PI = 3.1415926535897932384626433832795028841971; const int dx[6] = {1, 0, -1, 0, 1, 1}; const int dy[6] = {0, 1, 0, -1, 1, -1}; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } // --------------------------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N, K; cin >> N >> K; vector<ll> A(N); for (int i = 1; i <= N; i++) { cin >> A[i]; } vector<ll> dist(N + 1, -1); ll now = A[1]; dist[now] = 1; K--; if (K == 1) { cout << now << enld; return 0; } while (1) { if (dist[A[now]] != -1) { ll m = dist[now] + 1 - dist[A[now]]; K %= m; while (K > 0) { now = A[now]; K--; } break; } else { dist[A[now]] = dist[now] + 1; if (K == 0) { break; } now = A[now]; K--; } } cout << now << enld; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define enld '\n' #define rep(i, n) for (int i = 0; i < (n); i++) #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("Ofast") constexpr ll INF = 1e18; constexpr int inf = 1e9; constexpr ll mod = 1000000007; constexpr ll mod2 = 998244353; const double PI = 3.1415926535897932384626433832795028841971; const int dx[6] = {1, 0, -1, 0, 1, 1}; const int dy[6] = {0, 1, 0, -1, 1, -1}; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } // --------------------------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N, K; cin >> N >> K; vector<ll> A(N + 1); for (int i = 1; i <= N; i++) { cin >> A[i]; } vector<ll> dist(N + 1, -1); ll now = A[1]; dist[now] = 1; K--; if (K == 1) { cout << now << enld; return 0; } while (1) { if (dist[A[now]] != -1) { ll m = dist[now] + 1 - dist[A[now]]; K %= m; while (K > 0) { now = A[now]; K--; } break; } else { dist[A[now]] = dist[now] + 1; if (K == 0) { break; } now = A[now]; K--; } } cout << now << enld; return 0; }
replace
36
37
36
37
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vector<int>>; using vvvi = vector<vector<vector<int>>>; using vl = vector<ll>; using vvl = vector<vector<ll>>; using vvvl = vector<vector<vector<ll>>>; #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define FORR(i, m, n) for (int i = (m); i >= (n); i--) #define REP(i, n) FOR(i, 0, (n)) #define REPR(i, n) FORR(i, (n)-1, 0) #define REP1(i, n) FOR(i, 1, (n) + 1) #define REPS(c, s) for (char c : s) #define ALL(c) (c).begin(), (c).end() #define SORT(c) sort(ALL(c)) #define REV(c) reverse(ALL(c)) #define sz(v) (int)v.size() #define endl '\n' template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline void prn(vector<T> &v) { int n = sz(v); REP(i, n) cout << v[i] << ' '; } template <class T> inline void printv(vector<T> &v) { int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' '); } template <class T> inline void printvv(vector<vector<T>> &v) { for (auto u : v) printv(u); } template <class T> inline void printlnv(vector<T> &v) { int n = sz(v); REP(i, n) cout << v[i] << endl; } const int MOD = 1000000007; const int INF = 1000000001; const ll LINF = 1000000001000000001LL; void solve(); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(numeric_limits<double>::max_digits10); solve(); return 0; } void solve() { ll n, k; cin >> n >> k; vi a(n); REP(i, n) { cin >> a[i]; a[i]--; } vector<bool> visited(n, false); int p = 0; while (!visited[p]) { visited[p] = true; p = a[p]; } REP(i, n) visited[i] = false; int q = 0; ll s = 0; while (q != p) { visited[q] = true; q = a[q]; s++; } vi root; REP(i, n) visited[i] = false; ll c = 0; while (!visited[p]) { visited[p] = true; root.emplace_back(p); p = a[p]; c++; } // cout << c << endl; // printv(root); cout << root[(k - s) % c] + 1 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vector<int>>; using vvvi = vector<vector<vector<int>>>; using vl = vector<ll>; using vvl = vector<vector<ll>>; using vvvl = vector<vector<vector<ll>>>; #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define FORR(i, m, n) for (int i = (m); i >= (n); i--) #define REP(i, n) FOR(i, 0, (n)) #define REPR(i, n) FORR(i, (n)-1, 0) #define REP1(i, n) FOR(i, 1, (n) + 1) #define REPS(c, s) for (char c : s) #define ALL(c) (c).begin(), (c).end() #define SORT(c) sort(ALL(c)) #define REV(c) reverse(ALL(c)) #define sz(v) (int)v.size() #define endl '\n' template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline void prn(vector<T> &v) { int n = sz(v); REP(i, n) cout << v[i] << ' '; } template <class T> inline void printv(vector<T> &v) { int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' '); } template <class T> inline void printvv(vector<vector<T>> &v) { for (auto u : v) printv(u); } template <class T> inline void printlnv(vector<T> &v) { int n = sz(v); REP(i, n) cout << v[i] << endl; } const int MOD = 1000000007; const int INF = 1000000001; const ll LINF = 1000000001000000001LL; void solve(); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(numeric_limits<double>::max_digits10); solve(); return 0; } void solve() { ll n, k; cin >> n >> k; vi a(n); REP(i, n) { cin >> a[i]; a[i]--; } vector<bool> visited(n, false); int p = 0; while (!visited[p]) { visited[p] = true; p = a[p]; } REP(i, n) visited[i] = false; int q = 0; ll s = 0; while (q != p) { visited[q] = true; q = a[q]; s++; if (s >= k) { cout << q + 1 << endl; return; } } vi root; REP(i, n) visited[i] = false; ll c = 0; while (!visited[p]) { visited[p] = true; root.emplace_back(p); p = a[p]; c++; } // cout << c << endl; // printv(root); cout << root[(k - s) % c] + 1 << endl; }
insert
89
89
89
93
0
p02684
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int main() { int N; long long K; cin >> N >> K; int A[N]; rep(i, N) cin >> A[i]; // int m[N + 1]; rep(i, N) m[i + 1] = A[i]; pair<int, bool> visit[N + 1]; rep(i, N + 1) visit[i] = make_pair(0, false); int now = 1; int roop; int count; rep(i, K) { if (visit[now].second == true) { roop = i - visit[now].first; count = i; break; } visit[now] = make_pair(i, true); now = m[now]; } K -= (long long)count; roop = K % (long long)roop; rep(i, roop) now = m[now]; cout << now << endl; return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int main() { int N; long long K; cin >> N >> K; int A[N]; rep(i, N) cin >> A[i]; // int m[N + 1]; rep(i, N) m[i + 1] = A[i]; pair<int, bool> visit[N + 1]; rep(i, N + 1) visit[i] = make_pair(0, false); int now = 1; int roop = 1; int count = 1; rep(i, K) { if (visit[now].second == true) { roop = i - visit[now].first; count = i; break; } visit[now] = make_pair(i, true); now = m[now]; } K -= (long long)count; roop = K % (long long)roop; rep(i, roop) now = m[now]; cout << now << endl; return 0; }
replace
23
25
23
25
0
p02684
C++
Runtime Error
// https://atcoder.jp/contests/abc167/tasks/abc167_d #include <algorithm> #include <cassert> #include <chrono> #include <cstdint> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <vector> #define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define RREP(i, n) for (ll i = (n)-1; i >= 0; --i) #define SREP(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; ++i) #define ALL(v) (v).begin(), (v).end() using namespace std; typedef long long int ll; typedef unsigned long long int ull; template <typename T> T gcd(T a, T b) { if (a < b) gcd(b, a); if (b == 1) return 1; T r; while ((r = a % b)) { a = b; b = r; } return b; } template <typename T> struct UnionFind { vector<T> par; vector<T> rank; UnionFind(T n = 1) { init(n); } void init(T n = 1) { par.resize(n); rank.resize(n); for (T i = 0; i < n; ++i) { par[i] = i; rank[i] = 0; } } int root(T x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(T x, T y) { return root(x) == root(y); } bool merge(T x, T y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; return true; } }; // #define INF (ll(1e18)) template <typename Monoid, typename OperatorMonoid = Monoid> struct LazySegmentTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; ll sz, height; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(ll n, const F f, const G g, const H h, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; height = 0; while (sz < n) sz <<= 1, height++; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(ll k, const Monoid &x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } inline void propagate(ll k) { if (lazy[k] != OM0) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); data[k] = reflect(k); lazy[k] = OM0; } } inline Monoid reflect(ll k) { return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]); } inline void recalc(ll k) { while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1)); } inline void thrust(ll k) { for (int i = height; i > 0; i--) propagate(k >> i); } void update(ll a, ll b, const OperatorMonoid &x) { thrust(a += sz); thrust(b += sz - 1); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) lazy[l] = h(lazy[l], x), ++l; if (r & 1) --r, lazy[r] = h(lazy[r], x); } recalc(a); recalc(b); } Monoid query(ll a, ll b) { thrust(a += sz); thrust(b += sz - 1); Monoid L = M1, R = M1; for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) L = f(L, reflect(l++)); if (r & 1) R = f(reflect(--r), R); } return f(L, R); } Monoid operator[](const int &k) { return query(k, k + 1); } template <typename C> int find_subtree(ll a, const C &check, Monoid &M, bool type) { while (a < sz) { propagate(a); Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type)); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <typename C> int find_first(ll a, const C &check) { Monoid L = M1; if (a <= 0) { if (check(f(L, reflect(1)))) return find_subtree(1, check, L, false); return -1; } thrust(a + sz); int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, reflect(a)); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <typename C> int find_last(ll b, const C &check) { Monoid R = M1; if (b >= sz) { if (check(f(reflect(1), R))) return find_subtree(1, check, R, true); return -1; } thrust(b + sz - 1); int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(reflect(--b), R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; int msb(ll x) { int n = 0; while (x > 0) { x /= 2; n++; } return n; } template <ll Modulus> class modint { public: ll a; constexpr modint(const ll x = 0) noexcept : a(x % Modulus) {} constexpr ll &value() noexcept { return a; } constexpr operator int() const { return a; } constexpr operator long long() const { return a; } constexpr const ll &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator+(const ll rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator-(const ll rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator*(const ll rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint operator/(const ll rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { ll exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; #define MOD (ll(1e9 + 7)) #define INF (ll(1e15)) int main(int argc, const char *argv[]) { (void)argc; (void)argv; cin.tie(0); ios::sync_with_stdio(false); ull N, K; cin >> N >> K; vector<ll> A(N + 1); REP(i, N) { cin >> A[i + 1]; } vector<vector<ll>> R(64, vector<ll>(N)); R[0] = A; REP(k, 63) { REP(i, N) { R[k + 1][i + 1] = R[k][R[k][i + 1]]; } } ll x = 1; REP(i, 63) { if ((K >> i) & 1LL) { x = R[i][x]; } } cout << x << endl; }
// https://atcoder.jp/contests/abc167/tasks/abc167_d #include <algorithm> #include <cassert> #include <chrono> #include <cstdint> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <vector> #define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define RREP(i, n) for (ll i = (n)-1; i >= 0; --i) #define SREP(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; ++i) #define ALL(v) (v).begin(), (v).end() using namespace std; typedef long long int ll; typedef unsigned long long int ull; template <typename T> T gcd(T a, T b) { if (a < b) gcd(b, a); if (b == 1) return 1; T r; while ((r = a % b)) { a = b; b = r; } return b; } template <typename T> struct UnionFind { vector<T> par; vector<T> rank; UnionFind(T n = 1) { init(n); } void init(T n = 1) { par.resize(n); rank.resize(n); for (T i = 0; i < n; ++i) { par[i] = i; rank[i] = 0; } } int root(T x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(T x, T y) { return root(x) == root(y); } bool merge(T x, T y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; return true; } }; // #define INF (ll(1e18)) template <typename Monoid, typename OperatorMonoid = Monoid> struct LazySegmentTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; ll sz, height; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(ll n, const F f, const G g, const H h, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; height = 0; while (sz < n) sz <<= 1, height++; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(ll k, const Monoid &x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } inline void propagate(ll k) { if (lazy[k] != OM0) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); data[k] = reflect(k); lazy[k] = OM0; } } inline Monoid reflect(ll k) { return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]); } inline void recalc(ll k) { while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1)); } inline void thrust(ll k) { for (int i = height; i > 0; i--) propagate(k >> i); } void update(ll a, ll b, const OperatorMonoid &x) { thrust(a += sz); thrust(b += sz - 1); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) lazy[l] = h(lazy[l], x), ++l; if (r & 1) --r, lazy[r] = h(lazy[r], x); } recalc(a); recalc(b); } Monoid query(ll a, ll b) { thrust(a += sz); thrust(b += sz - 1); Monoid L = M1, R = M1; for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) L = f(L, reflect(l++)); if (r & 1) R = f(reflect(--r), R); } return f(L, R); } Monoid operator[](const int &k) { return query(k, k + 1); } template <typename C> int find_subtree(ll a, const C &check, Monoid &M, bool type) { while (a < sz) { propagate(a); Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type)); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <typename C> int find_first(ll a, const C &check) { Monoid L = M1; if (a <= 0) { if (check(f(L, reflect(1)))) return find_subtree(1, check, L, false); return -1; } thrust(a + sz); int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, reflect(a)); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <typename C> int find_last(ll b, const C &check) { Monoid R = M1; if (b >= sz) { if (check(f(reflect(1), R))) return find_subtree(1, check, R, true); return -1; } thrust(b + sz - 1); int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(reflect(--b), R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; int msb(ll x) { int n = 0; while (x > 0) { x /= 2; n++; } return n; } template <ll Modulus> class modint { public: ll a; constexpr modint(const ll x = 0) noexcept : a(x % Modulus) {} constexpr ll &value() noexcept { return a; } constexpr operator int() const { return a; } constexpr operator long long() const { return a; } constexpr const ll &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator+(const ll rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator-(const ll rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator*(const ll rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint operator/(const ll rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { ll exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; #define MOD (ll(1e9 + 7)) #define INF (ll(1e15)) int main(int argc, const char *argv[]) { (void)argc; (void)argv; cin.tie(0); ios::sync_with_stdio(false); ull N, K; cin >> N >> K; vector<ll> A(N + 1); REP(i, N) { cin >> A[i + 1]; } vector<vector<ll>> R(64, vector<ll>(N + 1)); R[0] = A; REP(k, 63) { REP(i, N) { R[k + 1][i + 1] = R[k][R[k][i + 1]]; } } ll x = 1; REP(i, 63) { if ((K >> i) & 1LL) { x = R[i][x]; } } cout << x << endl; }
replace
308
309
308
309
0
p02684
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; long N, K; vector<long> A, visited; int main(void) { cin >> N >> K; A.resize(N); visited.resize(N, false); for (long i = 1; i <= N; i++) cin >> A[i]; long x = 1; long s = 0, t = 0; while (!visited[x]) { visited[x] = true; x = A[x]; s++; } long y = x; do { y = A[y]; t++; } while (x != y); long u = 0; if (K <= s) { u = K; } else { u = s + (K - s) % t; } long ans = 1; for (long i = 0; i < u; i++) ans = A[ans]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long N, K; vector<long> A, visited; int main(void) { cin >> N >> K; A.resize(N + 1); visited.resize(N + 1, false); for (long i = 1; i <= N; i++) cin >> A[i]; long x = 1; long s = 0, t = 0; while (!visited[x]) { visited[x] = true; x = A[x]; s++; } long y = x; do { y = A[y]; t++; } while (x != y); long u = 0; if (K <= s) { u = K; } else { u = s + (K - s) % t; } long ans = 1; for (long i = 0; i < u; i++) ans = A[ans]; cout << ans << endl; return 0; }
replace
9
11
9
11
TLE
p02684
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> ipair; typedef pair<ll, ll> lpair; typedef tuple<int, int, int> ituple; // const int INF = INT_MAX; // const ll INF = LLONG_MAX; // const int MOD = ((int)1e9 + 7); // const ld EPS = (1e-10); #define PI acosl(-1) #define MAX_N (100000 + 2) int n, a[MAX_N]; int firstVisited[MAX_N]; ll k; void exec() { cin >> n >> k; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int ans = 0, i = 1, current = 1; firstVisited[1] = i; bool isFindLoop = false; while (true) { if (k <= 0) { ans = current; break; } k--; i++; current = a[current - 1]; // printf("loop %d: current = %d, k = %lld\n", i, current, k); if (!isFindLoop && firstVisited[current]) { ll loop = i - firstVisited[current]; if (loop > 0) { // printf("loop = %lld, k = %lld\n", loop, k); k %= loop; // printf("after k = %lld\n", k); } isFindLoop = true; } firstVisited[current] = i; } cout << ans << endl; } void solve() { int t = 1; // scanf("%d", &t); for (int i = 0; i < t; i++) { exec(); } } int main() { solve(); return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> ipair; typedef pair<ll, ll> lpair; typedef tuple<int, int, int> ituple; // const int INF = INT_MAX; // const ll INF = LLONG_MAX; // const int MOD = ((int)1e9 + 7); // const ld EPS = (1e-10); #define PI acosl(-1) #define MAX_N (200000 + 2) int n, a[MAX_N]; int firstVisited[MAX_N]; ll k; void exec() { cin >> n >> k; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int ans = 0, i = 1, current = 1; firstVisited[1] = i; bool isFindLoop = false; while (true) { if (k <= 0) { ans = current; break; } k--; i++; current = a[current - 1]; // printf("loop %d: current = %d, k = %lld\n", i, current, k); if (!isFindLoop && firstVisited[current]) { ll loop = i - firstVisited[current]; if (loop > 0) { // printf("loop = %lld, k = %lld\n", loop, k); k %= loop; // printf("after k = %lld\n", k); } isFindLoop = true; } firstVisited[current] = i; } cout << ans << endl; } void solve() { int t = 1; // scanf("%d", &t); for (int i = 0; i < t; i++) { exec(); } } int main() { solve(); return 0; }
replace
26
27
26
27
0
p02684
C++
Runtime Error
#define _DEBUG 1 #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef _DEBUG #define dump(x) cerr << #x << "=" << x << endl #define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl #define dump3(x, y, z) \ cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \ << endl #define check(s) cerr << s << endl #else #define dump(x) #define dump2(x, y) #define dump3(x, y, z) #define check(s) #endif #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); const ll LINF = 2e18; const int INF = 1e9; ll N, K; vector<ll> A; void solve() { vector<ll> r; map<ll, ll> seen; rep(i, N) { seen[i] = -1; } ll idx = 0; ll len = 0; while (true) { if (seen[idx] != -1) { break; } if (K <= len) { cout << idx + 1 << endl; return; } seen[idx] = len; r.push_back(idx); idx = A.at(idx); len++; } ll d; if (K - idx > 0) { d = (K - idx) % (len - idx); } else { d = K; } cout << r.at(d) + 1 << endl; } int main() { cin >> N >> K; A.resize(N); rep(i, N) { cin >> A.at(i); A.at(i)--; // 0-indexed } solve(); return 0; }
#define _DEBUG 1 #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef _DEBUG #define dump(x) cerr << #x << "=" << x << endl #define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl #define dump3(x, y, z) \ cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \ << endl #define check(s) cerr << s << endl #else #define dump(x) #define dump2(x, y) #define dump3(x, y, z) #define check(s) #endif #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); const ll LINF = 2e18; const int INF = 1e9; ll N, K; vector<ll> A; void solve() { vector<ll> r; map<ll, ll> seen; rep(i, N) { seen[i] = -1; } ll idx = 0; ll len = 0; while (true) { if (seen[idx] != -1) { break; } if (K <= len) { cout << idx + 1 << endl; return; } seen[idx] = len; r.push_back(idx); idx = A.at(idx); len++; } ll d; if (K - seen[idx] >= 0) { if (len - seen[idx] <= 0) { d = seen[idx] + (K - seen[idx]); } else { d = seen[idx] + (K - seen[idx]) % (len - seen[idx]); } } else { d = K; } cout << r.at(d) + 1 << endl; } int main() { cin >> N >> K; A.resize(N); rep(i, N) { cin >> A.at(i); A.at(i)--; // 0-indexed } solve(); return 0; }
replace
59
61
59
65
0
p02684
C++
Runtime Error
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; const double PI = acos(-1); int main() { long n, k; cin >> n >> k; vector<long> l(n); vector<long> lb(n, -1); vector<long> m; for (long i = 0; i < n; i++) { cin >> l[i]; } long cnt = 0, next = 0, loop = -1; while (lb[next] == -1) { lb[next] = cnt; m.push_back(next + 1); next = l[next] - 1; cnt++; } m.erase(m.begin(), m.begin() + lb[next]); long remain = k - lb[next], hoge = remain % (cnt - lb[next]); cout << m[hoge] << endl; return 0; }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; const double PI = acos(-1); int main() { long n, k; cin >> n >> k; vector<long> l(n); vector<long> lb(n, -1); vector<long> m; for (long i = 0; i < n; i++) { cin >> l[i]; } long cnt = 0, next = 0; while (lb[next] == -1 && cnt <= k) { lb[next] = cnt; m.push_back(next + 1); next = l[next] - 1; cnt++; } m.erase(m.begin(), m.begin() + lb[next]); long remain = k - lb[next], hoge = remain % (cnt - lb[next]); cout << m[hoge] << endl; return 0; }
replace
24
26
24
26
0
p02684
C++
Runtime Error
// #include<bits/stdc++.h> // using namespace std; // const int N = 10000, MAXI = 10000; // int main() { // freopen("output.txt", "r", stdin); // freopen("input.txt", "w", stdout); // srand(time(nullptr)); // int tc = 10; // cout << tc << "\n"; // while (tc--) // { // int n = rand()%N+1; // cout << n << " "; // cout << rand()%n + 1 << "\n"; // for(int i=1;i<=n;i++) // cout << rand()%MAXI + 1 << " "; // cout << "\n"; // cout << "~\n"; // } // return 0; // } //---------------------------------------------// // /* while(!cin.eof()) */ #include <bits/stdc++.h> using namespace std; #define int long long #define mod (int)(1e9 + 9) #define endl '\n' #define MAXI (int)(3e17 + 10) #define N 100005 int vis[N], a[N], last[N]; int n, k, ti = 0, ans; vector<int> v; void fun(int cur) { if (k == 0) { ans = cur; return; } if (vis[cur] == 1) { vector<int> x; for (int i = last[cur]; i < v.size(); i++) x.push_back(v[i]); int len = x.size(); int pos = k % len; ans = x[pos]; return; } v.push_back(cur); vis[cur] = 1; last[cur] = ti++; k--; fun(a[cur]); } // Driver code to test above functions int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } fun(1); cout << ans; return 0; }
// #include<bits/stdc++.h> // using namespace std; // const int N = 10000, MAXI = 10000; // int main() { // freopen("output.txt", "r", stdin); // freopen("input.txt", "w", stdout); // srand(time(nullptr)); // int tc = 10; // cout << tc << "\n"; // while (tc--) // { // int n = rand()%N+1; // cout << n << " "; // cout << rand()%n + 1 << "\n"; // for(int i=1;i<=n;i++) // cout << rand()%MAXI + 1 << " "; // cout << "\n"; // cout << "~\n"; // } // return 0; // } //---------------------------------------------// // /* while(!cin.eof()) */ #include <bits/stdc++.h> using namespace std; #define int long long #define mod (int)(1e9 + 9) #define endl '\n' #define MAXI (int)(3e17 + 10) #define N 200005 int vis[N], a[N], last[N]; int n, k, ti = 0, ans; vector<int> v; void fun(int cur) { if (k == 0) { ans = cur; return; } if (vis[cur] == 1) { vector<int> x; for (int i = last[cur]; i < v.size(); i++) x.push_back(v[i]); int len = x.size(); int pos = k % len; ans = x[pos]; return; } v.push_back(cur); vis[cur] = 1; last[cur] = ti++; k--; fun(a[cur]); } // Driver code to test above functions int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } fun(1); cout << ans; return 0; }
replace
35
36
35
36
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t, i, j, temp1, temp2, x, y, z, sum = 0, o, n, m, k, d; cin >> n >> k; vector<ll> a(n); for (i = 0; i < n; i++) { cin >> a[i]; } vector<int> path; unordered_set<int> s; j = 1; for (i = 0; i <= k; i++) { if (s.find(j) == s.end()) { path.push_back(j); s.insert(j); j = a[j - 1]; } else break; } for (i = 0; i < path.size(); i++) { if (path[i] == j) { break; } sum++; } temp1 = k - sum; cout << path[sum + (temp1 % (path.size() - sum))]; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t, i, j, temp1, temp2, x, y, z, sum = 0, o, n, m, k, d; cin >> n >> k; vector<ll> a(n); for (i = 0; i < n; i++) { cin >> a[i]; } vector<int> path; unordered_set<int> s; j = 1; for (i = 0; i <= k; i++) { if (s.find(j) == s.end()) { path.push_back(j); s.insert(j); j = a[j - 1]; } else break; } for (i = 0; i < path.size(); i++) { if (path[i] == j) { break; } sum++; } if (sum == path.size()) { cout << path[path.size() - 1]; } else { temp1 = k - sum; cout << path[sum + (temp1 % (path.size() - sum))]; } }
replace
31
33
31
37
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define ll long long #define dd double #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define vll vector<long long> #define vdd vector<double> #define vpii vector<pair<int, int>> #define vpll vector<pair<long long, long long>> #define vvi vector<vector<int>> #define vvl vector<vector<long long>> #define mii map<int, int> #define mll map<long long, long long> #define umii unordered_map<int, int> #define umll unordered_map<long long, long long> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, a, b) for (int i = a; i < b; i++) #define rev(i, n) for (int i = n; i >= 0; i--) #define rev2(i, a, b) for (int i = a; i >= b; i--) #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define inf 9e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl '\n' #define all(x) x.begin(), x.end() #define d0(x) cout << x << " " #define d1(x) cout << x << endl #define d2(x, y) cout << x << " " << y << endl #define d3(x, y, z) cout << x << " " << y << " " << z << endl #define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << endl #define read2(x, y) cin >> x >> y #define read3(x, y, z) cin >> x >> y >> z #define read4(x, y, z, w) cin >> x >> y >> z >> w #define read5(x, y, z, w, a) cin >> x >> y >> z >> w >> a #define sz(a) (int)a.size() const ll mod = 1e9 + 7; const double PI = 3.14159265359; const ll MOD = 998244353; void i_o() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main() { fio i_o(); ll n, k; cin >> n >> k; vll A(n + 1); rep2(i, 1, n + 1) cin >> A[i]; ll curr = 1; ll rep = -1; if (k < 100005) { ll k1 = k; while (k1--) { if (curr == A[curr]) rep = curr; curr = A[curr]; } d1(curr); return 0; } if (rep != -1) { d1(rep); return 0; } // d1("ok"); map<ll, ll> m; ll k2 = k; curr = 1; ll used = 0; ll st = -1; // for(int i=1;i<=n;i++) d0(A[i]); // cout <<endl; while (k2--) { if (m.count(curr)) { st = curr; // d2("curr is ",curr); break; } m[curr] = used; curr = A[curr]; used++; } // m.clear(); ll k3 = k; curr = st; ll use = m[st]; ll ini = use; m.clear(); // d2("St is ",st); ll fin; while (1 > 0) { if (m.count(curr)) { fin = use; break; } m[curr] = use; curr = A[curr]; use++; } k = k % (fin - ini); // d2(fin,ini); for (auto it : m) { if (it.second % (fin - ini) == k) { d1(it.first); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define ll long long #define dd double #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define vll vector<long long> #define vdd vector<double> #define vpii vector<pair<int, int>> #define vpll vector<pair<long long, long long>> #define vvi vector<vector<int>> #define vvl vector<vector<long long>> #define mii map<int, int> #define mll map<long long, long long> #define umii unordered_map<int, int> #define umll unordered_map<long long, long long> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, a, b) for (int i = a; i < b; i++) #define rev(i, n) for (int i = n; i >= 0; i--) #define rev2(i, a, b) for (int i = a; i >= b; i--) #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define inf 9e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl '\n' #define all(x) x.begin(), x.end() #define d0(x) cout << x << " " #define d1(x) cout << x << endl #define d2(x, y) cout << x << " " << y << endl #define d3(x, y, z) cout << x << " " << y << " " << z << endl #define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << endl #define read2(x, y) cin >> x >> y #define read3(x, y, z) cin >> x >> y >> z #define read4(x, y, z, w) cin >> x >> y >> z >> w #define read5(x, y, z, w, a) cin >> x >> y >> z >> w >> a #define sz(a) (int)a.size() const ll mod = 1e9 + 7; const double PI = 3.14159265359; const ll MOD = 998244353; void i_o() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main() { fio i_o(); ll n, k; cin >> n >> k; vll A(n + 1); rep2(i, 1, n + 1) cin >> A[i]; ll curr = 1; ll rep = -1; if (k < 400005) { ll k1 = k; while (k1--) { if (curr == A[curr]) rep = curr; curr = A[curr]; } d1(curr); return 0; } if (rep != -1) { d1(rep); return 0; } // d1("ok"); map<ll, ll> m; ll k2 = k; curr = 1; ll used = 0; ll st = -1; // for(int i=1;i<=n;i++) d0(A[i]); // cout <<endl; while (k2--) { if (m.count(curr)) { st = curr; // d2("curr is ",curr); break; } m[curr] = used; curr = A[curr]; used++; } // m.clear(); ll k3 = k; curr = st; ll use = m[st]; ll ini = use; m.clear(); // d2("St is ",st); ll fin; while (1 > 0) { if (m.count(curr)) { fin = use; break; } m[curr] = use; curr = A[curr]; use++; } k = k % (fin - ini); // d2(fin,ini); for (auto it : m) { if (it.second % (fin - ini) == k) { d1(it.first); return 0; } } return 0; }
replace
71
72
71
72
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02684
C++
Runtime Error
#include <assert.h> #include <cmath> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; int main() { unsigned long long int n, k; cin >> n >> k; vector<int> a(n + 1, -1); for (int i = 1; i <= n; ++i) { cin >> a[i]; } int now = 1; // naive if (k <= 200000) { // assert(now > 0); for (int i = 0; i < k; ++i) { // cout << now << " " << a[now] << endl; now = a[now]; } } else { vector<int> isVisited(n, false); queue<int> q; int roop_point = 0; long long int roop_size = 0; while (1) { if (!isVisited[now]) { q.push(now); isVisited[now] = true; now = a[now]; } else { roop_point = now; break; } } while (1) { int tmp = q.front(); if (tmp != roop_point) { --k; q.pop(); } else { roop_size = (long long int)q.size(); break; } } long long int remnant = 0; if (roop_size != 0) { remnant = k % roop_size; } for (long long int i = 0; i < remnant; ++i) { now = a[now]; } } cout << now << endl; return 0; }
#include <assert.h> #include <cmath> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; int main() { unsigned long long int n, k; cin >> n >> k; vector<int> a(n + 1, -1); for (int i = 1; i <= n; ++i) { cin >> a[i]; } int now = 1; // naive if (k <= 200000) { // assert(now > 0); for (int i = 0; i < k; ++i) { // cout << now << " " << a[now] << endl; now = a[now]; } } else { vector<int> isVisited(n + 1, false); queue<int> q; int roop_point = 0; long long int roop_size = 0; while (1) { if (!isVisited[now]) { q.push(now); isVisited[now] = true; now = a[now]; } else { roop_point = now; break; } } while (1) { int tmp = q.front(); if (tmp != roop_point) { --k; q.pop(); } else { roop_size = (long long int)q.size(); break; } } long long int remnant = 0; if (roop_size != 0) { remnant = k % roop_size; } for (long long int i = 0; i < remnant; ++i) { now = a[now]; } } cout << now << endl; return 0; }
replace
28
29
28
29
0
p02684
C++
Runtime Error
#include "bits/stdc++.h" #define REP(i, n) for (int i = 0; i < (n); i++) #define REPR(i, n) for (int i = (n); i >= 0; i--) #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define FORR(i, m, n) for (int i = (m); i >= (n); i--) #define SORT(v, n) std::sort(v, v + n); #define VSORT(v) std::sort(v.begin(), v.end()); #define VRSORT(v) std::sort(v.rbegin(), v.rend()); #define ll long long #define pb(a) push_back(a) #define ALL(obj) (obj).begin(), (obj).end() using namespace std; const int INF = 99999999; const ll LINF = 9999999999999; typedef pair<int, int> P; typedef pair<ll, ll> LP; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; ll n, k; int main() { cin >> n >> k; vector<ll> history; vector<ll> ord(n + 1, -1); vector<ll> a(n); ll c = 1, l = 0; { int v = 1; while (ord[v] == -1) { ord[v] = history.size(); history.push_back(v); v = a[v - 1]; } // 2回目にきたとき-1回目のとき c = history.size() - ord[v]; l = ord[v]; } if (k < l) cout << history[k] << endl; else { k -= l; k %= c; cout << history[l + k] << endl; } return 0; }
#include "bits/stdc++.h" #define REP(i, n) for (int i = 0; i < (n); i++) #define REPR(i, n) for (int i = (n); i >= 0; i--) #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define FORR(i, m, n) for (int i = (m); i >= (n); i--) #define SORT(v, n) std::sort(v, v + n); #define VSORT(v) std::sort(v.begin(), v.end()); #define VRSORT(v) std::sort(v.rbegin(), v.rend()); #define ll long long #define pb(a) push_back(a) #define ALL(obj) (obj).begin(), (obj).end() using namespace std; const int INF = 99999999; const ll LINF = 9999999999999; typedef pair<int, int> P; typedef pair<ll, ll> LP; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; ll n, k; int main() { cin >> n >> k; vector<ll> history; vector<ll> ord(n + 1, -1); vector<ll> a(n); REP(i, n) cin >> a[i]; ll c = 1, l = 0; { int v = 1; while (ord[v] == -1) { ord[v] = history.size(); history.push_back(v); v = a[v - 1]; } // 2回目にきたとき-1回目のとき c = history.size() - ord[v]; l = ord[v]; } if (k < l) cout << history[k] << endl; else { k -= l; k %= c; cout << history[l + k] << endl; } return 0; }
insert
30
30
30
31
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; int main() { int N; ll K; cin >> N >> K; vector<int> A(N + 1); for (int i = 0; i < N; i++) cin >> A[i + 1]; vector<ll> arrive(N + 1, -1); int cur = 1; ll t = 0; while (K > 0) { if (arrive[cur] != -1) { ll cycle = t - arrive[cur]; K %= cycle; t += cycle; } else { arrive[cur] = t; } if (K > 0) { t++; K--; cur = A[cur]; } } cout << cur << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; int main() { int N; ll K; cin >> N >> K; vector<int> A(N + 1); for (int i = 0; i < N; i++) cin >> A[i + 1]; vector<ll> arrive(N + 1, -1); int cur = 1; ll t = 0; while (K > 0) { if (arrive[cur] != -1) { ll cycle = t - arrive[cur]; if (cycle != 0) K %= cycle; } else { arrive[cur] = t; } if (K > 0) { t++; K--; cur = A[cur]; } } cout << cur << endl; return 0; }
replace
18
20
18
20
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define tie() \ ios_base ::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define int long long const int N = 1e5 + 20; bool vis[N]; int cnt = 0; vector<int> g[N]; int last[N]; int dfs(int x, int k) { if (k == 0 or g[x].empty()) return x; cnt++; if (vis[x]) { if (k > (cnt - last[x])) k %= (cnt - last[x]); if (k == 0 or g[x].empty()) return x; return dfs(g[x][0], k - 1); } vis[x] = true; last[x] = cnt; return dfs(g[x][0], k - 1); } int32_t main() { tie(); int t = 1; // cin >> t; while (t--) { int n, k; cin >> n >> k; int to; for (int i = 1; i <= n; i++) { cin >> to; g[i].push_back(to); } memset(vis, false, sizeof vis); cout << dfs(1, k) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define tie() \ ios_base ::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define int long long const int N = 2e5 + 20; bool vis[N]; int cnt = 0; vector<int> g[N]; int last[N]; int dfs(int x, int k) { if (k == 0 or g[x].empty()) return x; cnt++; if (vis[x]) { if (k > (cnt - last[x])) k %= (cnt - last[x]); if (k == 0 or g[x].empty()) return x; return dfs(g[x][0], k - 1); } vis[x] = true; last[x] = cnt; return dfs(g[x][0], k - 1); } int32_t main() { tie(); int t = 1; // cin >> t; while (t--) { int n, k; cin >> n >> k; int to; for (int i = 1; i <= n; i++) { cin >> to; g[i].push_back(to); } memset(vis, false, sizeof vis); cout << dfs(1, k) << endl; } return 0; }
replace
10
11
10
11
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define mod 1000000007 using namespace std; int main() { long long N, K; cin >> N >> K; vector<int> town(N); for (int i = 0; i < N; i++) { cin >> town.at(i); town.at(i) = town.at(i) - 1; } int now = 0; vector<int> count(N, 0); long long roop_town, rest; bool flag = false; for (int i = 0; i < K; i++) { now = town.at(now); if (count.at(i) != 0) { flag = true; roop_town = i - count.at(now); rest = K - i - 1; break; } count.at(now) = i; } if (flag) { rest = rest % roop_town; for (int j = 0; j < rest; j++) now = town.at(now); } cout << now + 1 << endl; }
#include <bits/stdc++.h> #define mod 1000000007 using namespace std; int main() { long long N, K; cin >> N >> K; vector<int> town(N); for (int i = 0; i < N; i++) { cin >> town.at(i); town.at(i) = town.at(i) - 1; } int now = 0; vector<int> count(N, 0); long long roop_town, rest; bool flag = false; for (int i = 0; i < K; i++) { now = town.at(now); if (count.at(now) != 0) { flag = true; roop_town = i - count.at(now); rest = K - i - 1; break; } count.at(now) = i; } if (flag) { rest = rest % roop_town; for (int j = 0; j < rest; j++) now = town.at(now); } cout << now + 1 << endl; }
replace
22
23
22
23
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, t; long long int k; int A[100001]; map<int, bool> visited; int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> A[i]; } vector<int> vec; int now = 1; while (1) { if (visited[now]) { break; } visited[now] = true; vec.push_back(now); now = A[now]; } int idx = 0; for (idx = 0; idx < vec.size(); idx++) { if (now == vec[idx]) { break; } } if (k < idx) { cout << vec[k] << '\n'; } else { vector<int> circle; for (int j = idx; j < vec.size(); j++) { circle.push_back(vec[j]); } k -= idx; int m = circle.size(); cout << circle[k % m] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, t; long long int k; int A[200001]; map<int, bool> visited; int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> A[i]; } vector<int> vec; int now = 1; while (1) { if (visited[now]) { break; } visited[now] = true; vec.push_back(now); now = A[now]; } int idx = 0; for (idx = 0; idx < vec.size(); idx++) { if (now == vec[idx]) { break; } } if (k < idx) { cout << vec[k] << '\n'; } else { vector<int> circle; for (int j = idx; j < vec.size(); j++) { circle.push_back(vec[j]); } k -= idx; int m = circle.size(); cout << circle[k % m] << '\n'; } return 0; }
replace
7
8
7
8
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) #define repit(it, li) for (auto it = li.begin(); it != li.end(); it++) #define ll long long int int main() { int n; ll k; cin >> n >> k; int a[200000]; rep(i, n) cin >> a[i], a[i]--; map<int, int> reached; int c = 0; vector<int> li; rep(i, k) { li.push_back(c); reached.insert(make_pair(c, i)); c = a[c]; if (reached.find(c) != reached.end()) break; } int s = reached.at(c); int l = li.size() - s; int ansi = (k <= s) ? k : (s + (k - s) % l); int ans = li[ansi]; cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) #define repit(it, li) for (auto it = li.begin(); it != li.end(); it++) #define ll long long int int main() { int n; ll k; cin >> n >> k; int a[200000]; rep(i, n) cin >> a[i], a[i]--; map<int, int> reached; int c = 0; vector<int> li; rep(i, n + 1) { li.push_back(c); reached.insert(make_pair(c, i)); c = a[c]; if (reached.find(c) != reached.end()) break; } int s = reached.at(c); int l = li.size() - s; int ansi = (k <= s) ? k : (s + (k - s) % l); int ans = li[ansi]; cout << ans + 1 << endl; return 0; }
replace
17
18
17
18
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define pii pair<int, int> #define fi first #define pb push_back #define si second #define int long long int using namespace std; int a[100005], s, vis[100005]; vector<int> v, u; void dfs(int v) { if (vis[v]) { s = v; return; } vis[v] = 1; dfs(a[v]); } void dfs1(int v) { if (v == s) return; u.pb(v); dfs1(a[v]); } int32_t main() { int n, k, i; cin >> n >> k; for (i = 1; i <= n; i++) { cin >> a[i]; // mp[a[i]]=i; } dfs(1); i = 1; while (i != s) { v.pb(i); i = a[i]; } u.pb(s); dfs1(a[s]); /*for(auto x:u) cout<<x<<" "; cout<<"\n"; for(auto x:v) cout<<x<<" "; cout<<"\n";*/ if (k < v.size()) cout << v[k] << "\n"; else { k -= v.size(); int sz = u.size(); cout << u[k % sz] << "\n"; } }
#include <bits/stdc++.h> #define pii pair<int, int> #define fi first #define pb push_back #define si second #define int long long int using namespace std; int a[200005], s, vis[200005]; vector<int> v, u; void dfs(int v) { if (vis[v]) { s = v; return; } vis[v] = 1; dfs(a[v]); } void dfs1(int v) { if (v == s) return; u.pb(v); dfs1(a[v]); } int32_t main() { int n, k, i; cin >> n >> k; for (i = 1; i <= n; i++) { cin >> a[i]; // mp[a[i]]=i; } dfs(1); i = 1; while (i != s) { v.pb(i); i = a[i]; } u.pb(s); dfs1(a[s]); /*for(auto x:u) cout<<x<<" "; cout<<"\n"; for(auto x:v) cout<<x<<" "; cout<<"\n";*/ if (k < v.size()) cout << v[k] << "\n"; else { k -= v.size(); int sz = u.size(); cout << u[k % sz] << "\n"; } }
replace
7
8
7
8
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INCANT \ cin.tie(0), ios::sync_with_stdio(false), cout << fixed << setprecision(20) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, n) FOR(i, 0, n) #define all(x) (x).begin(), (x).end() #define out(a) cout << a << endl #define PRINT(V) \ for (auto v : (V)) \ cout << v << " " const int MOD = 1000000007; const double PI = acos(-1.0); void solve() { ll N, K; cin >> N >> K; vector<int> A(N); vector<int> B; vector<int> C(N, -1); rep(i, N) { int ak; cin >> ak; A.at(i) = ak - 1; } int node = 0; ll i = 0; B.push_back(node); C[node] = i; ll init; ll loop; while (i < K) { node = A.at(node); i++; if (C[node] > -1) { init = C[node]; loop = i - C[node]; break; } else { C[node] = i; B.push_back(node); } } if (i == K - 1) out(C[node] + 1); else out(B.at(init + ((K - init) % loop)) + 1); } int main() { INCANT; solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INCANT \ cin.tie(0), ios::sync_with_stdio(false), cout << fixed << setprecision(20) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, n) FOR(i, 0, n) #define all(x) (x).begin(), (x).end() #define out(a) cout << a << endl #define PRINT(V) \ for (auto v : (V)) \ cout << v << " " const int MOD = 1000000007; const double PI = acos(-1.0); void solve() { ll N, K; cin >> N >> K; vector<int> A(N); vector<int> B; vector<int> C(N, -1); rep(i, N) { int ak; cin >> ak; A.at(i) = ak - 1; } int node = 0; ll i = 0; B.push_back(node); C[node] = i; ll init; ll loop; while (i < K) { node = A.at(node); i++; if (C[node] > -1) { init = C[node]; loop = i - C[node]; break; } else { C[node] = i; B.push_back(node); } } if (i == K) out(node + 1); else out(B.at(init + ((K - init) % loop)) + 1); } int main() { INCANT; solve(); return 0; }
replace
46
48
46
48
0
p02684
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ls (o << 1) #define rs (o << 1 | 1) #define pb push_back const double PI = acos(-1.0); const int M = 2e5 + 7; /* int head[M],cnt; void init(){cnt=0,memset(head,0,sizeof(head));} struct EDGE{int to,nxt,val;}ee[M*2]; void add(int x,int y){ee[++cnt].nxt=head[x],ee[cnt].to=y,head[x]=cnt;} */ int to[M]; int vs[M]; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> to[i]; int tp = 1; ll nm = 0; while (1) { vs[tp]++; nm++; if (nm == k) { cout << to[tp] << endl; return 0; } tp = to[tp]; if (vs[tp] == 2) break; } ll nw = 0; for (int i = 1; i <= n; i++) if (vs[i] == 2) nw++; k -= nm; k %= nw; while (k) { tp = to[tp]; } cout << tp << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ls (o << 1) #define rs (o << 1 | 1) #define pb push_back const double PI = acos(-1.0); const int M = 2e5 + 7; /* int head[M],cnt; void init(){cnt=0,memset(head,0,sizeof(head));} struct EDGE{int to,nxt,val;}ee[M*2]; void add(int x,int y){ee[++cnt].nxt=head[x],ee[cnt].to=y,head[x]=cnt;} */ int to[M]; int vs[M]; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> to[i]; int tp = 1; ll nm = 0; while (1) { vs[tp]++; nm++; if (nm == k) { cout << to[tp] << endl; return 0; } tp = to[tp]; if (vs[tp] == 2) break; } ll nw = 0; for (int i = 1; i <= n; i++) if (vs[i] == 2) nw++; k -= nm; k %= nw; while (k) { tp = to[tp]; k--; } cout << tp << endl; return 0; }
insert
47
47
47
48
TLE
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; long long K; cin >> N >> K; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A.at(i); A.at(i)--; } vector<int> B; int index = 0; int count = 0; set<int> gone; gone.insert(0); long long right = 0, left = 0; while (count < N) { B.push_back(A.at(index)); // cout << "B = " << B.at(count) << endl; if (gone.count(A.at(index))) { right = A.at(index); left = count; break; } gone.insert(A.at(index)); index = A.at(index); count++; } if (right == 0) right = -1; // cout << "count = " << count << endl; // cout << "right = " << right << endl; for (int i = 0; i < count; i++) { if (B.at(i) == right) { right = i; break; } } int ans = 0; if (left + 1 >= K) ans = B.at(K - 1); else { // cout << "left = " << left << ", right = " << right << endl; long long loop = left - right; // cout << "loop = " << loop << endl; long long pos; if (loop) pos = right + (K - right - 1) % loop; else pos = right; // cout << "pos = " << pos << endl; ans = B.at(pos); } cout << ans + 1 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; long long K; cin >> N >> K; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A.at(i); A.at(i)--; } vector<int> B; int index = 0; int count = 0; set<int> gone; gone.insert(0); long long right = 0, left = 0; while (count < N) { B.push_back(A.at(index)); // cout << "B = " << B.at(count) << endl; if (gone.count(A.at(index))) { right = A.at(index); left = count; break; } gone.insert(A.at(index)); index = A.at(index); count++; } if (right == 0) right = -1; // cout << "count = " << count << endl; // cout << "right = " << right << endl; for (int i = 0; i < count; i++) { if (B.at(i) == right) { right = i; break; } } int ans = 0; if (left + 1 >= K) ans = B.at(K - 1); else { // cout << "left = " << left << ", right = " << right << endl; long long loop = left - right; // cout << "loop = " << loop << endl; long long pos; if (loop) pos = right + (K - right - 1) % loop; else pos = right; // cout << "pos = " << pos << endl; if (pos != -1) ans = B.at(pos); else ans = 0; } cout << ans + 1 << endl; }
replace
53
54
53
57
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using namespace std; int main() { int n; ll k; cin >> n >> k; vector<int> as(n); rep(i, n) { cin >> as[i]; } vector<int> s; vector<int> ord(n + 1, -1); int c = 1, l = 0; int v = 1; while (ord[v] != -1) { ord[v] = s.size(); s.push_back(v); v = as[v - 1]; } c = s.size() - ord[v]; l = ord[v]; if (k < l) { cout << s[k] << endl; } else { k -= l; k %= c; cout << s[l + k] << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using namespace std; int main() { int n; ll k; cin >> n >> k; vector<int> as(n); rep(i, n) { cin >> as[i]; } vector<int> s; vector<int> ord(n + 1, -1); int c = 1, l = 0; int v = 1; while (ord[v] == -1) { ord[v] = s.size(); s.push_back(v); v = as[v - 1]; } c = s.size() - ord[v]; l = ord[v]; if (k < l) { cout << s[k] << endl; } else { k -= l; k %= c; cout << s[l + k] << endl; } return 0; }
replace
17
18
17
18
-11
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define fo(a, b) for (int a = 0; a < b; a++) #define Sort(a) sort(a.begin(), a.end()) #define rev(a) reverse(a.begin(), a.end()) #define fi first #define se second #define bgn begin() #define sz size() #define en end() #define pb push_back #define pp() pop_back() #define V vector #define P pair #define yuko(a) setprecision(a) #define uni(a) a.erase(unique(a.begin(), a.end()), a.end()) #define Q queue #define pri priority_queue #define Pri priority_queue<int, vector<int>, greater<int>> #define PriP \ priority_queue<P<int, int>, vector<P<int, int>>, greater<P<int, int>>> #define all(a) (a).begin(), (a).end() #define ff first.first #define fs first.second #define sf second.first #define ss second.second int low(V<int> &a, int b) { auto c = lower_bound(a.begin(), a.end(), b); int d = c - a.bgn; return d; } int upp(V<int> &a, int b) { auto c = upper_bound(a.begin(), a.end(), b); int d = c - a.bgn; return d; } int wari(int a, int b) { if (a % b == 0) return a / b; else return a / b + 1; } int keta(int a) { double b = a; b = log10(b); int c = b; return c + 1; } int souwa(int a) { return a * (a + 1) / 2; } int gcm(int a, int b) { if (a % b == 0) return b; return gcm(b, a % b); } bool prime(int a) { if (a < 2) return false; else if (a == 2) return true; else if (a % 2 == 0) return false; for (int i = 3; i <= sqrt(a) + 1; i += 2) { if (a % i == 0) return false; } return true; } struct Union { vector<int> par; Union(int a) { par = vector<int>(a, -1); } int find(int a) { if (par[a] < 0) return a; else return par[a] = find(par[a]); } bool same(int a, int b) { return find(a) == find(b); } int Size(int a) { return -par[find(a)]; } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (Size(b) > Size(a)) swap<int>(a, b); par[a] += par[b]; par[b] = a; } }; int ketas(int a) { string b = to_string(a); int c = 0; fo(i, keta(a)) { c += b[i] - '0'; } return c; } bool fe(int a, int b) { a %= 10; b %= 10; if (a == 0) a = 10; if (b == 0) b = 10; if (a > b) return true; else return false; } int INF = 1000000007; struct edge { int s, t, d; }; V<int> mojisyu(string a) { V<int> b(26, 0); fo(i, a.sz) { b[a[i] - 'a']++; } return b; } int wa2(int a) { if (a % 2 == 1) return a / 2; return a / 2 - 1; } /*signed main(){ int a,b,c; cin>>a>>b>>c; V<V<edge>> d(a); fo(i,b){ edge e; cin>>e.s>>e.t>>e.d; d[e.s].pb(e); } V<int> e(a,INF); e[c]=0; priority_queue<P<int,int>,V<P<int,int>>,greater<P<int,int>>> f; f.push({0,c}); int h=INF; while(!f.empty()){ P<int,int> g; g=f.top(); f.pop(); int v = g.second, i = g.first; for(edge l : d[v]){ if(e[l.t] > i + l.d){ e[l.t] = i + l.d; f.push({i+l.d , l.t}); } } } fo(i,a){ if(e[i]==INF) cout<<"INF"<<endl; else cout<<e[i]<<endl; } } ?*/ int nCr(int n, int r) { int a = 1; r = min(r, n - r); for (int i = n; i > n - r; i--) { a *= i; a /= n - i + 1; } return a; } /*void sea(int x,int y){ if(x<0||a<=x||y<0||b<=y||c[x][y]=='#') return; if(d[x][y]) return; d[x][y]++; sea(x+1,y); sea(x-1,y); sea(x,y+1); sea(x,y-1); }*/ int kaijou(int a) { int b = 1; fo(i, a) b *= i + 1; return b; } int nPr(int a, int b) { if (a < b) return 0; if (b == 0) return 1; int c = 1; for (int i = a; i > a - b; i--) { c *= i; c %= INF; } return c; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int lcm(int a, int b) { int c = modinv(gcm(a, b), INF); return ((a * c) % INF) * (b % INF) % INF; } int MOD = INF; int fac[1000010], finv[1000010], inv[1000010]; // テーブルを作る前処理 // 先にCOMinit()で前処理をする // ABC145D void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < 1000010; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 int COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } bool naka(int a, int b, V<V<char>> c) { return (a >= 0 && b >= 0 && a < c.sz && b < c[0].sz); } V<P<int, int>> mawari8 = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {-1, -1}, {1, 1}, {1, -1}, {-1, -1}}; int inf = 1000000000000000007; /* signed main(){ int a,b,c; cin>>a>>b>>c; V<V<edge>> d(a); fo(i,b){ edge e; cin>>e.s>>e.t>>e.d; d[e.s].pb(e); } V<int> e(a,INF); e[c]=0; priority_queue<P<int,int>,V<P<int,int>>,greater<P<int,int>>> f; f.push({0,c}); int h=INF; while(!f.empty()){ P<int,int> g; g=f.top(); f.pop(); int v = g.second, i = g.first; for(edge l : d[v]){ if(e[l.t] > i + l.d){ e[l.t] = i + l.d; f.push({i+l.d , l.t}); } } } fo(i,a){ if(e[i]==INF) cout<<"INF"<<endl; else cout<<e[i]<<endl; } }*/ V<P<int, int>> mawari4 = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; // 最短経路の表 a(全部INFで初期化) // 縦横 x,y // 迷路 f // スタートsx,sy // ゴールgx,gy // 文字はgから使おうね /*int bfs_haba(){ Q<P<int,int>> b; a[sx][sy]=0; b.push({sx,sy}); while(!b.empty()){ P<int,int> c=b.front(); b.pop(); if(c.fi==gx&&c.se==gy){ break; } fo(i,4){ int d=c.fi+mawari4[i].fi; int e=c.se+mawari4[i].se; if(0<=d&&0<=e&&d<x&&e<y&&f[d][e]!='#'&&a[d][e]==INF){ b.push({d,e}); a[d][e]=1+a[c.fi][c.se]; } } } return a[gx][gy]; }*/ V<int> onajibubun(string a) { V<int> b(a.sz); for (int i = 1, j = 0; i < a.sz; i++) { if (i + b[i - j] < j + b[j]) b[i] = b[i - j]; else { int c = max<int>(0, j + b[j] - i); while (i + c < a.sz && a[c] == a[i + c]) c++; b[i] = c; j = i; } } b[0] = a.sz; return b; } // 各頂点ごとにどこに辺が出てるかの表がc // 各頂点ごとの色を表すV<int>(頂点数max)のcolorを用意する // aはどこ塗るか、bは何で塗るかなので、(0,1,c)でよぶとおけ V<int> color(205); bool nibu_hantei(int a, int b, V<V<int>> c) { color[a] = b; fo(i, c[a].sz) { if (b == color[c[a][i]]) return false; if (color[c[a][i]] == 0 && !nibu_hantei(c[a][i], -b, c)) return false; } return true; } // aは頂点数 // nibu_hanteiの上にcolorを用意する // 各頂点ごとにどこに辺が出てるかの表がc bool renketujanai_nibu_hantei(int a, V<V<int>> c) { fo(i, a) { if (color[i] == 0) { if (!nibu_hantei(i, 1, c)) return false; } } return true; } struct segmin { vector<int> seg; int b; segmin(V<int> a) { b = 1; while (b < a.sz) b *= 2; seg = vector<int>(2 * b - 1, inf); fo(i, a.sz) { seg[i + b - 1] = a[i]; } for (int i = b - 2; i >= 0; i--) { seg[i] = min(seg[2 * i + 1], seg[2 * i + 2]); } } void update(int i, int a) { i += b - 1; seg[i] = a; while (i) { i = (i - 1) / 2; seg[i] = min(seg[2 * i + 1], seg[2 * i + 2]); } } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ int getmin(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return INF; if (x <= l && r <= y) return seg[a]; int a1 = getmin(x, y, 2 * a + 1, l, (l + r) / 2); int a2 = getmin(x, y, 2 * a + 2, (l + r) / 2, r); return min(a1, a2); } }; struct segadd { vector<int> seg; int b; segadd(V<int> a) { b = 1; while (b < a.sz) b *= 2; seg = vector<int>(2 * b - 1, 0); fo(i, a.sz) { seg[i + b - 1] = a[i]; } for (int i = b - 2; i >= 0; i--) { seg[i] = seg[2 * i + 1] + seg[2 * i + 2]; } } void update(int i, int a) { i += b - 1; seg[i] = a; while (i) { i = (i - 1) / 2; seg[i] = seg[2 * i + 1] + seg[2 * i + 2]; } } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ int getadd(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return 0; if (x <= l && r <= y) return seg[a]; int a1 = getadd(x, y, 2 * a + 1, l, (l + r) / 2); int a2 = getadd(x, y, 2 * a + 2, (l + r) / 2, r); return a1 + a2; } }; // 12だったら{(2,2),(3,1)}って返してくれるはず V<P<int, int>> factorize(int n) { V<P<int, int>> res; for (int i = 2; i * i <= n; i++) { if (n % i) continue; res.emplace_back(i, 0); while (n % i == 0) { n /= i; res.back().second++; } } if (n != 1) res.emplace_back(n, 1); return res; } const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; signed main() { int n, m, x; cin >> n >> m >> x; V<V<int>> d(n, V<int>(m)); int ans = 1000000007; V<int> c(n); fo(i, n) { cin >> c[i]; fo(j, m) { cin >> d[i][j]; } } int g = 0; for (int bit = 0; bit < (1 << n); bit++) { bool can = 1; int o = 0; fo(i, m) { int sm = 0; fo(j, n) { if (bit & (1 << j)) sm += d[j][i]; } if (sm < x) { can = 0; } } if (can == 1) { fo(h, n) { if (bit & (1 << h)) o += c[h]; g = 1; } ans = min(ans, o); } } if (g) cout << ans << endl; else cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define fo(a, b) for (int a = 0; a < b; a++) #define Sort(a) sort(a.begin(), a.end()) #define rev(a) reverse(a.begin(), a.end()) #define fi first #define se second #define bgn begin() #define sz size() #define en end() #define pb push_back #define pp() pop_back() #define V vector #define P pair #define yuko(a) setprecision(a) #define uni(a) a.erase(unique(a.begin(), a.end()), a.end()) #define Q queue #define pri priority_queue #define Pri priority_queue<int, vector<int>, greater<int>> #define PriP \ priority_queue<P<int, int>, vector<P<int, int>>, greater<P<int, int>>> #define all(a) (a).begin(), (a).end() #define ff first.first #define fs first.second #define sf second.first #define ss second.second int low(V<int> &a, int b) { auto c = lower_bound(a.begin(), a.end(), b); int d = c - a.bgn; return d; } int upp(V<int> &a, int b) { auto c = upper_bound(a.begin(), a.end(), b); int d = c - a.bgn; return d; } int wari(int a, int b) { if (a % b == 0) return a / b; else return a / b + 1; } int keta(int a) { double b = a; b = log10(b); int c = b; return c + 1; } int souwa(int a) { return a * (a + 1) / 2; } int gcm(int a, int b) { if (a % b == 0) return b; return gcm(b, a % b); } bool prime(int a) { if (a < 2) return false; else if (a == 2) return true; else if (a % 2 == 0) return false; for (int i = 3; i <= sqrt(a) + 1; i += 2) { if (a % i == 0) return false; } return true; } struct Union { vector<int> par; Union(int a) { par = vector<int>(a, -1); } int find(int a) { if (par[a] < 0) return a; else return par[a] = find(par[a]); } bool same(int a, int b) { return find(a) == find(b); } int Size(int a) { return -par[find(a)]; } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (Size(b) > Size(a)) swap<int>(a, b); par[a] += par[b]; par[b] = a; } }; int ketas(int a) { string b = to_string(a); int c = 0; fo(i, keta(a)) { c += b[i] - '0'; } return c; } bool fe(int a, int b) { a %= 10; b %= 10; if (a == 0) a = 10; if (b == 0) b = 10; if (a > b) return true; else return false; } int INF = 1000000007; struct edge { int s, t, d; }; V<int> mojisyu(string a) { V<int> b(26, 0); fo(i, a.sz) { b[a[i] - 'a']++; } return b; } int wa2(int a) { if (a % 2 == 1) return a / 2; return a / 2 - 1; } /*signed main(){ int a,b,c; cin>>a>>b>>c; V<V<edge>> d(a); fo(i,b){ edge e; cin>>e.s>>e.t>>e.d; d[e.s].pb(e); } V<int> e(a,INF); e[c]=0; priority_queue<P<int,int>,V<P<int,int>>,greater<P<int,int>>> f; f.push({0,c}); int h=INF; while(!f.empty()){ P<int,int> g; g=f.top(); f.pop(); int v = g.second, i = g.first; for(edge l : d[v]){ if(e[l.t] > i + l.d){ e[l.t] = i + l.d; f.push({i+l.d , l.t}); } } } fo(i,a){ if(e[i]==INF) cout<<"INF"<<endl; else cout<<e[i]<<endl; } } ?*/ int nCr(int n, int r) { int a = 1; r = min(r, n - r); for (int i = n; i > n - r; i--) { a *= i; a /= n - i + 1; } return a; } /*void sea(int x,int y){ if(x<0||a<=x||y<0||b<=y||c[x][y]=='#') return; if(d[x][y]) return; d[x][y]++; sea(x+1,y); sea(x-1,y); sea(x,y+1); sea(x,y-1); }*/ int kaijou(int a) { int b = 1; fo(i, a) b *= i + 1; return b; } int nPr(int a, int b) { if (a < b) return 0; if (b == 0) return 1; int c = 1; for (int i = a; i > a - b; i--) { c *= i; c %= INF; } return c; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int lcm(int a, int b) { int c = modinv(gcm(a, b), INF); return ((a * c) % INF) * (b % INF) % INF; } int MOD = INF; int fac[1000010], finv[1000010], inv[1000010]; // テーブルを作る前処理 // 先にCOMinit()で前処理をする // ABC145D void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < 1000010; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 int COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } bool naka(int a, int b, V<V<char>> c) { return (a >= 0 && b >= 0 && a < c.sz && b < c[0].sz); } V<P<int, int>> mawari8 = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {-1, -1}, {1, 1}, {1, -1}, {-1, -1}}; int inf = 1000000000000000007; /* signed main(){ int a,b,c; cin>>a>>b>>c; V<V<edge>> d(a); fo(i,b){ edge e; cin>>e.s>>e.t>>e.d; d[e.s].pb(e); } V<int> e(a,INF); e[c]=0; priority_queue<P<int,int>,V<P<int,int>>,greater<P<int,int>>> f; f.push({0,c}); int h=INF; while(!f.empty()){ P<int,int> g; g=f.top(); f.pop(); int v = g.second, i = g.first; for(edge l : d[v]){ if(e[l.t] > i + l.d){ e[l.t] = i + l.d; f.push({i+l.d , l.t}); } } } fo(i,a){ if(e[i]==INF) cout<<"INF"<<endl; else cout<<e[i]<<endl; } }*/ V<P<int, int>> mawari4 = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; // 最短経路の表 a(全部INFで初期化) // 縦横 x,y // 迷路 f // スタートsx,sy // ゴールgx,gy // 文字はgから使おうね /*int bfs_haba(){ Q<P<int,int>> b; a[sx][sy]=0; b.push({sx,sy}); while(!b.empty()){ P<int,int> c=b.front(); b.pop(); if(c.fi==gx&&c.se==gy){ break; } fo(i,4){ int d=c.fi+mawari4[i].fi; int e=c.se+mawari4[i].se; if(0<=d&&0<=e&&d<x&&e<y&&f[d][e]!='#'&&a[d][e]==INF){ b.push({d,e}); a[d][e]=1+a[c.fi][c.se]; } } } return a[gx][gy]; }*/ V<int> onajibubun(string a) { V<int> b(a.sz); for (int i = 1, j = 0; i < a.sz; i++) { if (i + b[i - j] < j + b[j]) b[i] = b[i - j]; else { int c = max<int>(0, j + b[j] - i); while (i + c < a.sz && a[c] == a[i + c]) c++; b[i] = c; j = i; } } b[0] = a.sz; return b; } // 各頂点ごとにどこに辺が出てるかの表がc // 各頂点ごとの色を表すV<int>(頂点数max)のcolorを用意する // aはどこ塗るか、bは何で塗るかなので、(0,1,c)でよぶとおけ V<int> color(205); bool nibu_hantei(int a, int b, V<V<int>> c) { color[a] = b; fo(i, c[a].sz) { if (b == color[c[a][i]]) return false; if (color[c[a][i]] == 0 && !nibu_hantei(c[a][i], -b, c)) return false; } return true; } // aは頂点数 // nibu_hanteiの上にcolorを用意する // 各頂点ごとにどこに辺が出てるかの表がc bool renketujanai_nibu_hantei(int a, V<V<int>> c) { fo(i, a) { if (color[i] == 0) { if (!nibu_hantei(i, 1, c)) return false; } } return true; } struct segmin { vector<int> seg; int b; segmin(V<int> a) { b = 1; while (b < a.sz) b *= 2; seg = vector<int>(2 * b - 1, inf); fo(i, a.sz) { seg[i + b - 1] = a[i]; } for (int i = b - 2; i >= 0; i--) { seg[i] = min(seg[2 * i + 1], seg[2 * i + 2]); } } void update(int i, int a) { i += b - 1; seg[i] = a; while (i) { i = (i - 1) / 2; seg[i] = min(seg[2 * i + 1], seg[2 * i + 2]); } } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ int getmin(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return INF; if (x <= l && r <= y) return seg[a]; int a1 = getmin(x, y, 2 * a + 1, l, (l + r) / 2); int a2 = getmin(x, y, 2 * a + 2, (l + r) / 2, r); return min(a1, a2); } }; struct segadd { vector<int> seg; int b; segadd(V<int> a) { b = 1; while (b < a.sz) b *= 2; seg = vector<int>(2 * b - 1, 0); fo(i, a.sz) { seg[i + b - 1] = a[i]; } for (int i = b - 2; i >= 0; i--) { seg[i] = seg[2 * i + 1] + seg[2 * i + 2]; } } void update(int i, int a) { i += b - 1; seg[i] = a; while (i) { i = (i - 1) / 2; seg[i] = seg[2 * i + 1] + seg[2 * i + 2]; } } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ int getadd(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return 0; if (x <= l && r <= y) return seg[a]; int a1 = getadd(x, y, 2 * a + 1, l, (l + r) / 2); int a2 = getadd(x, y, 2 * a + 2, (l + r) / 2, r); return a1 + a2; } }; // 12だったら{(2,2),(3,1)}って返してくれるはず V<P<int, int>> factorize(int n) { V<P<int, int>> res; for (int i = 2; i * i <= n; i++) { if (n % i) continue; res.emplace_back(i, 0); while (n % i == 0) { n /= i; res.back().second++; } } if (n != 1) res.emplace_back(n, 1); return res; } const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; signed main() { int n, k, g = 0, h, c = 1; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v.at(i); v.at(i)--; } if (n >= k) { for (int i = 0; i < k; i++) g = v.at(g); cout << g + 1 << endl; } else { for (int i = 0; i < n; i++) g = v.at(g); h = g; while (h != v.at(g)) { g = v.at(g); c++; } c = (k - n) % c; for (int i = 0; i <= c; i++) g = v.at(g); cout << g + 1 << endl; } }
replace
436
475
436
461
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; using vvl = vector<vector<ll>>; /* short */ #define pb push_back #define eb emplace_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) rbegin(v), rend(v) /* REPmacro */ #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) for (int i = 0; i < (n); i++) /* exchange */ #define chmin(a, b) (a) = min((ll)(a), (ll)(b)) #define chmax(a, b) (a) = max((ll)(a), (ll)(b)) /* output */ #define I(x) cin >> x; #define D(x) cerr << (x) << " "; #define BR cerr << "\n"; #define P(x) cout << (x) << endl; #define FIX cout << fixed << setprecision(10); /* const */ const int ARRAY = 100005; const int INF = 1001001001; // 10^9 const ll LINF = 1001001001001001001; // 10^18 const int MOD = 1e9 + 7; ll N = 0; ll K; ll ret = 0; vvl table(61, vl(ARRAY)); int main(void) { I(N); I(K); FOR(i, 1, N + 1) { I(table[1][i]); } FOR(i, 2, 61) { FOR(j, 1, N + 1) { table[i][j] = table[i - 1][table[i - 1][j]]; } } ll now = 1; FOR(i, 1, 61) { if ((K >> (i - 1)) & 1) { now = table[i][now]; } } P(now); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; using vvl = vector<vector<ll>>; /* short */ #define pb push_back #define eb emplace_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) rbegin(v), rend(v) /* REPmacro */ #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) for (int i = 0; i < (n); i++) /* exchange */ #define chmin(a, b) (a) = min((ll)(a), (ll)(b)) #define chmax(a, b) (a) = max((ll)(a), (ll)(b)) /* output */ #define I(x) cin >> x; #define D(x) cerr << (x) << " "; #define BR cerr << "\n"; #define P(x) cout << (x) << endl; #define FIX cout << fixed << setprecision(10); /* const */ const int ARRAY = 100005; const int INF = 1001001001; // 10^9 const ll LINF = 1001001001001001001; // 10^18 const int MOD = 1e9 + 7; ll N = 0; ll K; ll ret = 0; vvl table(61, vl(200005)); int main(void) { I(N); I(K); FOR(i, 1, N + 1) { I(table[1][i]); } FOR(i, 2, 61) { FOR(j, 1, N + 1) { table[i][j] = table[i - 1][table[i - 1][j]]; } } ll now = 1; FOR(i, 1, 61) { if ((K >> (i - 1)) & 1) { now = table[i][now]; } } P(now); }
replace
40
41
40
41
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define d(x) cerr << #x ":" << x << endl; #define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl #define rep(i, n) for (int i = (int)(0); i < (int)(n); i++) #define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define all(v) v.begin(), v.end() #define dump(v) \ cerr << #v ":[ "; \ for (auto macro_vi : v) { \ cerr << macro_vi << " "; \ } \ cerr << "]" << endl; #define ddump(v) \ cerr << #v ":" << endl; \ for (auto macro_row : v) { \ cerr << "["; \ for (auto macro__vi : macro_row) { \ cerr << macro__vi << " "; \ } \ cerr << "]" << endl; \ } using lint = long long; const int INF = 1e9; const lint LINF = 1e18; const lint MOD = 1e9 + 7; const double EPS = 1e-10; int main() { lint N, K; cin >> N >> K; vector<lint> A(N, 0); rep(i, N) { cin >> A[i]; A[i]--; } vector<int> path(1, 0); set<int> check{0}; int cn = 0; int start = -1; while (true) { int nc = A[cn]; if (check.count(nc)) { start = nc; break; } else { path.push_back(nc); cn = nc; check.insert(nc); } } int x = -1; int p = path.size(); for (int i = 0; i < p; i++) { if (path[i] == start) { x = i; } } if (x == -1) { return 1; } K -= (lint)x; vector<lint> path2(p - x); rep(i, path2.size()) { path2[i] = path[i + x]; } lint mod = path2.size(); cout << path2[K % mod] + 1 << endl; // dump(path); // d(start); // dump(path2); // d(x); return 0; }
#include <bits/stdc++.h> using namespace std; #define d(x) cerr << #x ":" << x << endl; #define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl #define rep(i, n) for (int i = (int)(0); i < (int)(n); i++) #define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define all(v) v.begin(), v.end() #define dump(v) \ cerr << #v ":[ "; \ for (auto macro_vi : v) { \ cerr << macro_vi << " "; \ } \ cerr << "]" << endl; #define ddump(v) \ cerr << #v ":" << endl; \ for (auto macro_row : v) { \ cerr << "["; \ for (auto macro__vi : macro_row) { \ cerr << macro__vi << " "; \ } \ cerr << "]" << endl; \ } using lint = long long; const int INF = 1e9; const lint LINF = 1e18; const lint MOD = 1e9 + 7; const double EPS = 1e-10; int main() { lint N, K; cin >> N >> K; vector<lint> A(N, 0); rep(i, N) { cin >> A[i]; A[i]--; } vector<int> path(1, 0); set<int> check{0}; int cn = 0; int start = -1; while (true) { int nc = A[cn]; if (check.count(nc)) { start = nc; break; } else { path.push_back(nc); cn = nc; check.insert(nc); } } int x = -1; int p = path.size(); for (int i = 0; i < p; i++) { if (path[i] == start) { x = i; } } if (K < x) { cout << path[K] + 1 << endl; return 0; } K -= (lint)x; vector<lint> path2(p - x); rep(i, path2.size()) { path2[i] = path[i + x]; } lint mod = path2.size(); cout << path2[K % mod] + 1 << endl; // dump(path); // d(start); // dump(path2); // d(x); return 0; }
replace
59
61
59
62
0
p02684
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <unordered_map> #include <vector> #pragma warning(disable : 4996) #define rep(i, n) for (int i = 0; i < (n); i++) #define PI 3.141592653589793 using namespace std; using ll = long long; const int D = 60; const int MAX_N = 200005; int to[D][MAX_N]; int main() { ll n, k; cin >> n >> k; rep(i, n) { cin >> to[0][i]; to[0][i]--; } rep(i, n) { rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; } } int v = 0; for (int i = D - 1; i >= 0; i--) { ll l = 1ll << i; if (l <= k) { v = to[i][v]; k -= l; } } cout << v + 1 << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <unordered_map> #include <vector> #pragma warning(disable : 4996) #define rep(i, n) for (int i = 0; i < (n); i++) #define PI 3.141592653589793 using namespace std; using ll = long long; const int D = 60; const int MAX_N = 200005; int to[D][MAX_N]; int main() { ll n, k; cin >> n >> k; rep(i, n) { cin >> to[0][i]; to[0][i]--; } rep(i, D - 1) { rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; } } int v = 0; for (int i = D - 1; i >= 0; i--) { ll l = 1ll << i; if (l <= k) { v = to[i][v]; k -= l; } } cout << v + 1 << endl; return 0; }
replace
32
33
32
33
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using lint = long long; int main() { int n; lint k; scanf("%d%lld", &n, &k); vector<int> to(n); rep(u, n) scanf("%d", &to[u]), to[u]--; static int dbl[60][100000]; rep(u, n) dbl[0][u] = to[u]; rep(t, 59) rep(u, n) dbl[t + 1][u] = dbl[t][dbl[t][u]]; int pos = 0; rep(t, 60) if (k >> t & 1) pos = dbl[t][pos]; printf("%d\n", pos + 1); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using lint = long long; int main() { int n; lint k; scanf("%d%lld", &n, &k); vector<int> to(n); rep(u, n) scanf("%d", &to[u]), to[u]--; static int dbl[60][200000]; rep(u, n) dbl[0][u] = to[u]; rep(t, 59) rep(u, n) dbl[t + 1][u] = dbl[t][dbl[t][u]]; int pos = 0; rep(t, 60) if (k >> t & 1) pos = dbl[t][pos]; printf("%d\n", pos + 1); return 0; }
replace
14
15
14
15
0
p02684
C++
Runtime Error
#include <array> #include <iostream> #include <map> #include <string> using namespace std; int main(void) { int n; long int k; std::array<int, 200000> next_town; cin >> n >> k; for (int i = 0; i < n; i++) { int next; cin >> next; next_town[i] = next - 1; } int current_index = 0; std::map<int, int> arrive_time; bool dumped = false; for (long int i = 0; i < k; i++) { arrive_time[current_index] = i; current_index = next_town[current_index]; if (!dumped && arrive_time.find(current_index) != arrive_time.end()) { int roop_start = arrive_time[current_index]; int roop_length = i - roop_start; i = k - (k - 1 - roop_start) % roop_length - 1; dumped = true; } } cout << current_index + 1; }
#include <array> #include <iostream> #include <map> #include <string> using namespace std; int main(void) { int n; long int k; std::array<int, 200000> next_town; cin >> n >> k; for (int i = 0; i < n; i++) { int next; cin >> next; next_town[i] = next - 1; } int current_index = 0; std::map<int, int> arrive_time; bool dumped = false; for (long int i = 0; i < k; i++) { arrive_time[current_index] = i; current_index = next_town[current_index]; if (!dumped && arrive_time.find(current_index) != arrive_time.end()) { int roop_start = arrive_time[current_index]; int roop_length = i - roop_start + 1; i = k - (k - roop_start) % roop_length - 1; dumped = true; } } cout << current_index + 1; }
replace
29
31
29
31
0
p02684
C++
Runtime Error
/* Date:2020.5.10 Name:Aliemo */ #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <string> #define int long long #define rr register #define MAXN 1010 #define inf 1e18 using namespace std; inline int read(int &T) { int s = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) { f |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + (ch ^ 48); ch = getchar(); } return T = f ? -s : s; } void print(int x) { if (x < 0) x = -x, putchar('-'); if (x > 9) print(x / 10); putchar(x % 10 + '0'); } int n, k, now, last, sum, ans; int a[MAXN], vis[MAXN]; bool f; signed main() { n = read(n); k = read(k); for (rr int i = 1; i <= n; i++) a[i] = read(a[i]); now = 1; vis[now]++; while (k--) { now = a[now]; // cout << now; if (vis[now] == 1 && !f) { // ans++; vis[now]++; f = 1; int t = now; t = a[t]; ans++; while (vis[t] != 2) { t = a[t]; ans++; } // cout << ans << " "; k %= ans; } vis[now]++; } cout << now; }
/* Date:2020.5.10 Name:Aliemo */ #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <string> #define int long long #define rr register #define MAXN 200010 #define inf 1e18 using namespace std; inline int read(int &T) { int s = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) { f |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + (ch ^ 48); ch = getchar(); } return T = f ? -s : s; } void print(int x) { if (x < 0) x = -x, putchar('-'); if (x > 9) print(x / 10); putchar(x % 10 + '0'); } int n, k, now, last, sum, ans; int a[MAXN], vis[MAXN]; bool f; signed main() { n = read(n); k = read(k); for (rr int i = 1; i <= n; i++) a[i] = read(a[i]); now = 1; vis[now]++; while (k--) { now = a[now]; // cout << now; if (vis[now] == 1 && !f) { // ans++; vis[now]++; f = 1; int t = now; t = a[t]; ans++; while (vis[t] != 2) { t = a[t]; ans++; } // cout << ans << " "; k %= ans; } vis[now]++; } cout << now; }
replace
14
15
14
15
0
p02684
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> P; int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; const int MAXN = 100000; const int MAXE = 100000; const int MAXV = 10000; const int inf = 1e9; const ll INF = 2e18; const ll MOD = 1e9 + 7; struct UnionFind { vector<int> par, rank; UnionFind(int n) : par(n), rank(n) { for (int i = 0; i < n; ++i) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x), ry = root(y); if (rx == ry) return; if (rank[ry] > rank[rx]) par[rx] = ry; else par[ry] = rx; if (rank[rx] == rank[ry]) rank[rx]++; } bool same(int x, int y) { return root(x) == root(y); } }; vector<ll> fac, inv, finv; void initcom() { fac = inv = finv = vector<ll>(1e6); fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1; for (ll i = 2; i < 1e6; ++i) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll com(int n, int k) { if (n < 0 || k < 0) return 0; if (n < k) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll gcd(ll x, ll y) { if (y == 0) return x; return gcd(y, x % y); } ll mopow(ll a, ll x, ll mod) { ll res = 1; while (x > 0) { if (x & 1) res = res * a % mod; a = a * a % mod; x >>= 1; } return res; } int main() { ll n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i], --a[i]; vector<bool> used(n, false); int cur = 0; while (!used[cur]) { used[cur] = true; cur = a[cur]; } vector<int> cycle; cycle.push_back(cur); int s = a[cur]; while (s != cur) { cycle.push_back(s); s = a[s]; } int t = 0; while (t != cur) { k--; t = a[t]; } ll m = cycle.size(); cout << cycle[k % m] + 1 << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> P; int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; const int MAXN = 100000; const int MAXE = 100000; const int MAXV = 10000; const int inf = 1e9; const ll INF = 2e18; const ll MOD = 1e9 + 7; struct UnionFind { vector<int> par, rank; UnionFind(int n) : par(n), rank(n) { for (int i = 0; i < n; ++i) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x), ry = root(y); if (rx == ry) return; if (rank[ry] > rank[rx]) par[rx] = ry; else par[ry] = rx; if (rank[rx] == rank[ry]) rank[rx]++; } bool same(int x, int y) { return root(x) == root(y); } }; vector<ll> fac, inv, finv; void initcom() { fac = inv = finv = vector<ll>(1e6); fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1; for (ll i = 2; i < 1e6; ++i) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll com(int n, int k) { if (n < 0 || k < 0) return 0; if (n < k) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll gcd(ll x, ll y) { if (y == 0) return x; return gcd(y, x % y); } ll mopow(ll a, ll x, ll mod) { ll res = 1; while (x > 0) { if (x & 1) res = res * a % mod; a = a * a % mod; x >>= 1; } return res; } int main() { ll n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i], --a[i]; vector<bool> used(n, false); int cur = 0; while (!used[cur]) { used[cur] = true; cur = a[cur]; } vector<int> cycle; cycle.push_back(cur); int s = a[cur]; while (s != cur) { cycle.push_back(s); s = a[s]; } int t = 0; while (t != cur) { if (k == 0) { cout << t + 1 << endl; return 0; } k--; t = a[t]; } ll m = cycle.size(); cout << cycle[k % m] + 1 << endl; return 0; }
insert
110
110
110
114
0
p02684
C++
Runtime Error
/* AUTHOR: Aman Raj LANG: C++ */ using namespace std; typedef long long ll; #include <bits/stdc++.h> // setbase - cout << setbase (16); cout << 100 << endl; Prints 64 // setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints // xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx // cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define all(a) a.begin(), a.end() #define mod 1000000007 #define inf 1e9 // #define mod 3 #define flush fflush(stdout) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef cc_hash_table<int, ll, hash<ll>> ht; // find_by_order() // order_of_key typedef tree<int, null_type, less<long long int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; ll power1(ll a, ll b) // a raised to power b { if (b <= 0) return 1; if (b == 1) return a; ll c = power1(a, b / 2); c *= c; c = c % mod; if (b % 2 == 1) c *= a; c = c % mod; return c; } /* ll mi(ll a,ll m) { ll c= power1(a,m-2,mod); return c; } */ int main() { fast; ll n, k; cin >> n >> k; vector<ll> v(n); for (auto &x : v) cin >> x; vector<ll> visited(n, -1); ll t = 0, times = 0; visited[0] = 0; while (k > 0) { t = v[t] - 1; times++; k--; if (visited[t] >= 0) { break; } visited[t] = times; } k = max(k, 0ll); times = times - visited[t]; times = k % times; while (times--) { t = v[t] - 1; } cout << t + 1 << endl; return 0; }
/* AUTHOR: Aman Raj LANG: C++ */ using namespace std; typedef long long ll; #include <bits/stdc++.h> // setbase - cout << setbase (16); cout << 100 << endl; Prints 64 // setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints // xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx // cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define all(a) a.begin(), a.end() #define mod 1000000007 #define inf 1e9 // #define mod 3 #define flush fflush(stdout) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef cc_hash_table<int, ll, hash<ll>> ht; // find_by_order() // order_of_key typedef tree<int, null_type, less<long long int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; ll power1(ll a, ll b) // a raised to power b { if (b <= 0) return 1; if (b == 1) return a; ll c = power1(a, b / 2); c *= c; c = c % mod; if (b % 2 == 1) c *= a; c = c % mod; return c; } /* ll mi(ll a,ll m) { ll c= power1(a,m-2,mod); return c; } */ int main() { fast; ll n, k; cin >> n >> k; vector<ll> v(n); for (auto &x : v) cin >> x; vector<ll> visited(n, -1); ll t = 0, times = 0; visited[0] = 0; while (k > 0) { t = v[t] - 1; times++; k--; if (visited[t] >= 0) { break; } visited[t] = times; } if (k <= 0) { cout << t + 1 << endl; return 0; } times = times - visited[t]; times = k % times; while (times--) { t = v[t] - 1; } cout << t + 1 << endl; return 0; }
replace
75
76
75
79
0
p02684
C++
Runtime Error
#include <algorithm> #include <functional> #include <math.h> #include <numeric> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; int main() { int n; long long k; int x; scanf("%d %lld", &n, &k); vector<int> next(n); for (int i = 0; i < n; ++i) { scanf("%d", &x); next[i] = x - 1; } vector<bool> visited(n); vector<int> chain; visited[0] = true; chain.push_back(0); while (k > 0) { int nn = next[chain[chain.size() - 1]]; --k; chain.push_back(nn); if (visited[nn]) { break; } visited[nn] = true; } if (k == 0) { printf("%d\n", chain[chain.size() - 1] + 1); } // Find cycle int index = 0; while (chain[index++] != chain[chain.size() - 1]) ; vector<int> whole_cycle(chain.begin() + index, chain.end()); --k; k %= whole_cycle.size(); printf("%d", whole_cycle[k] + 1); return 0; }
#include <algorithm> #include <functional> #include <math.h> #include <numeric> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; int main() { int n; long long k; int x; scanf("%d %lld", &n, &k); vector<int> next(n); for (int i = 0; i < n; ++i) { scanf("%d", &x); next[i] = x - 1; } vector<bool> visited(n); vector<int> chain; visited[0] = true; chain.push_back(0); while (k > 0) { int nn = next[chain[chain.size() - 1]]; --k; chain.push_back(nn); if (visited[nn]) { break; } visited[nn] = true; } if (k == 0) { printf("%d\n", chain[chain.size() - 1] + 1); return 0; } // Find cycle int index = 0; while (chain[index++] != chain[chain.size() - 1]) ; vector<int> whole_cycle(chain.begin() + index, chain.end()); --k; k %= whole_cycle.size(); printf("%d", whole_cycle[k] + 1); return 0; }
insert
43
43
43
44
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define MOD_NUMBER 1000000007 // ベクトル要素の列挙 template <class T> void vector_print(vector<T> vec) { for (unsigned int i = 0; i < vec.size(); i++) { cout << vec.at(i); if (i < vec.size() - 1) { cout << " "; } else if (i == vec.size() - 1) { cout << endl; } } } // エラトステネスの篩による素数配列の出力 vector<int> Eratosthenes(int N) { vector<int> p = {2}; int sqrtN = sqrt(N); for (int i = 3; i <= N; i++) { p.push_back(i); for (int j = 0; p.at(j) <= sqrtN; j++) { if (i % p.at(j) == 0) { p.pop_back(); break; } } } return p; } int main() { ll n, k, x = 1, size = 0, spare; cin >> n >> k; vector<ll> a(n); vector<ll> b(n + 1, 0); for (int i = 0; i < n; i++) { cin >> a.at(i); } for (int i = 0; i < n; i++) { if (a.at(x - 1) <= 0) { break; } spare = x; x = a.at(x - 1); a.at(spare - 1) = -size; b.at(i) = x; size++; } if (k <= size) { cout << b.at(k - 1) << endl; return 0; } k += a.at(x - 1); k %= size - a.at(x - 1); k -= a.at(x - 1); if (k == 0) { k = size; } cout << b.at(k - 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define MOD_NUMBER 1000000007 // ベクトル要素の列挙 template <class T> void vector_print(vector<T> vec) { for (unsigned int i = 0; i < vec.size(); i++) { cout << vec.at(i); if (i < vec.size() - 1) { cout << " "; } else if (i == vec.size() - 1) { cout << endl; } } } // エラトステネスの篩による素数配列の出力 vector<int> Eratosthenes(int N) { vector<int> p = {2}; int sqrtN = sqrt(N); for (int i = 3; i <= N; i++) { p.push_back(i); for (int j = 0; p.at(j) <= sqrtN; j++) { if (i % p.at(j) == 0) { p.pop_back(); break; } } } return p; } int main() { ll n, k, x = 1, size = 0, spare; cin >> n >> k; vector<ll> a(n); vector<ll> b(n + 1, 0); for (int i = 0; i < n; i++) { cin >> a.at(i); } for (int i = 0; i < n; i++) { if (a.at(x - 1) <= 0) { break; } spare = x; x = a.at(x - 1); a.at(spare - 1) = -size; b.at(i) = x; size++; } if (k <= size) { cout << b.at(k - 1) << endl; return 0; } k += a.at(x - 1); k %= size + a.at(x - 1); k -= a.at(x - 1); if (k == 0) { k = size; } cout << b.at(k - 1) << endl; return 0; }
replace
59
60
59
60
0
p02684
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; const int N = 2e5 + 1; long long k; int n, a[N]; bool avail[N]; vector<int> V; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; V.push_back(0); int start = 1; bool run = false; while (avail[a[start]] == false) { start = a[start]; avail[start] = true; V.push_back(start); } // V.push_back(a[start]); // for (int i = 1; i < V.size(); i++) cout << V[i] << " "; cout << "\n"; int d, c = V.size() - 1; for (int i = 1; i < V.size() - 1; i++) if (a[start] == V[i]) d = i; // cout << d << " " << c << "\n"; if (k < d) cout << V[k]; else { k = k - d + 1; k = k % (c - d + 1); if (k == 0) cout << V[c]; else cout << V[d + k - 1]; } return 0; }
#include <iostream> #include <vector> using namespace std; const int N = 2e5 + 1; long long k; int n, a[N]; bool avail[N]; vector<int> V; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; V.push_back(0); int start = 1; bool run = false; while (avail[a[start]] == false) { start = a[start]; avail[start] = true; V.push_back(start); } // V.push_back(a[start]); // for (int i = 1; i < V.size(); i++) cout << V[i] << " "; cout << "\n"; int d, c = V.size() - 1; for (int i = 1; i < V.size(); i++) if (a[start] == V[i]) d = i; // cout << d << " " << c << "\n"; if (k < d) cout << V[k]; else { k = k - d + 1; k = k % (c - d + 1); if (k == 0) cout << V[c]; else cout << V[d + k - 1]; } return 0; }
replace
24
25
24
25
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #include <string> #define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define ll long long #define mod (ll)(1e9 + 7) #define nax (ll)(1e5) #define INFL LLONG_MAX #define INF INT_MAX #define disp(a, n, arr) \ for (int i = a; i < (ll)n; i++) \ cout << arr[i] << " "; \ cout << "\n"; #define check cout << "Working\n"; #define pi pair<int, int> #define pl pair<ll, ll> #define vi vector<int> #define vl vector<ll> #define vpl vector<pl> #define vpi vector<pi> #define vb vector<bool> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FIO; // Write your code here int t = 1; // cin >> t; while (t--) { ll n, k; cin >> n >> k; vl v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } vl rep(n + 10, -1); // rep[1] = 1; ll cur = 1; ll lim = 0; ll i = 1; while (k--) { if (rep[cur] != -1) { lim = i - rep[cur]; break; } else { rep[cur] = i; cur = v[cur - 1]; // cout << cur << " "; } i++; } k++; ll ans = k % lim; ll result = cur; while (ans--) { result = v[cur - 1]; cur = result; } cout << result << "\n"; } return 0; }
#include <bits/stdc++.h> #include <string> #define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define ll long long #define mod (ll)(1e9 + 7) #define nax (ll)(1e5) #define INFL LLONG_MAX #define INF INT_MAX #define disp(a, n, arr) \ for (int i = a; i < (ll)n; i++) \ cout << arr[i] << " "; \ cout << "\n"; #define check cout << "Working\n"; #define pi pair<int, int> #define pl pair<ll, ll> #define vi vector<int> #define vl vector<ll> #define vpl vector<pl> #define vpi vector<pi> #define vb vector<bool> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FIO; // Write your code here int t = 1; // cin >> t; while (t--) { ll n, k; cin >> n >> k; vl v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } vl rep(n + 10, -1); // rep[1] = 1; ll cur = 1; ll lim = 0; ll i = 1; while (k--) { if (rep[cur] != -1) { lim = i - rep[cur]; break; } else { rep[cur] = i; cur = v[cur - 1]; // cout << cur << " "; } i++; } k++; if (lim == 0) { cout << cur << "\n"; return 0; } ll ans = k % lim; ll result = cur; while (ans--) { result = v[cur - 1]; cur = result; } cout << result << "\n"; } return 0; }
insert
63
63
63
67
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02684
C++
Runtime Error
#include <bits/stdc++.h> #include <math.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) typedef long long ll; using Graph = vector<vector<int>>; typedef long long ll; typedef pair<int, int> P; const int MOD = 1000000007; const int INF_32 = 1LL << 30; const int64_t INF_64 = 1LL << 60; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) { cin >> a.at(i); a.at(i)--; } map<ll, ll> mp; ll count = 0; ll curr = 0; mp[curr] = 0; while (true) { curr = a[curr]; count++; if (count == k) { return curr + 1; } if (mp.count(curr)) { break; } mp[curr] = count; } ll loop_begin = curr; ll loop = count - mp[loop_begin]; k = k - mp[loop_begin]; k = k % loop; curr = loop_begin; for (int i = 0; i < k; i++) { curr = a[curr]; } cout << curr + 1 << endl; }
#include <bits/stdc++.h> #include <math.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) typedef long long ll; using Graph = vector<vector<int>>; typedef long long ll; typedef pair<int, int> P; const int MOD = 1000000007; const int INF_32 = 1LL << 30; const int64_t INF_64 = 1LL << 60; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) { cin >> a.at(i); a.at(i)--; } map<ll, ll> mp; ll count = 0; ll curr = 0; mp[curr] = 0; while (true) { curr = a[curr]; count++; if (count == k) { cout << curr + 1 << endl; return 0; } if (mp.count(curr)) { break; } mp[curr] = count; } ll loop_begin = curr; ll loop = count - mp[loop_begin]; k = k - mp[loop_begin]; k = k % loop; curr = loop_begin; for (int i = 0; i < k; i++) { curr = a[curr]; } cout << curr + 1 << endl; }
replace
34
35
34
36
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ff first #define ss second #define pb push_back #define mp make_pair #define ld long double #define forn(low, high, i) for (i = low; i < high; i++) #define forrev(high, low, i) for (i = high; i >= low; i--) #define all(v) v.begin(), v.end() #define decimal_digits cout << fixed << setprecision(15); string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto &x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); #define GOOGLE 0 #define MULTIPLE_TC 0 const int inf = 1e9; const long long INF = 1e18; const int mod = 1e9 + 7; const int bit32 = log2(inf) + 3; const int bit64 = log2(INF) + 3; inline int add(long long a, long long b) { a += b; if (a >= mod) a -= mod; return a; } inline int sub(long long a, long long b) { a -= b; if (a < 0) a += mod; return a; } inline int mul(long long a, long long b) { a %= mod; b %= mod; return (int)((long long)a * b % mod); } inline int modexpo(long long a, long long b) { int res = 1; while (b > 0) { if (b & 1) res = mul(res, a); a = mul(a, a); b /= 2; } return res; } inline int divide(long long a, long long b) { return mul(a, modexpo(b, mod - 2)); } inline int inv(long long a) { return modexpo(a, mod - 2); } clock_t time_p = clock(); void ktj() { time_p = clock() - time_p; cerr << "Time elapsed : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } void pre() { // Reset each global variable (esp. for graphs) } const int N = 2e5 + 10; int a[N]; void solve() { long long n, k; int i, j; cin >> n >> k; for (i = 1; i <= n; i++) { cin >> a[i]; } int u = 1, step = 1, sub = 0, p = 0, cyc = 0; map<int, int> m, par; par[u] = p; vector<int> res; while (1) { if (m.find(u) != m.end()) { while (p != u) { res.push_back(p); p = par[p]; } res.push_back(u); sub = m[u] - 1; break; } m[u] = step; par[u] = p; p = u; u = a[u]; step += 1; } reverse(all(res)); cyc = res.size(); k -= sub; k %= cyc; cout << res[k] << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); pre(); int t = 1, T; if (MULTIPLE_TC) cin >> t; for (T = 1; T <= t; T++) { if (GOOGLE) cout << "Case #" << T << ": "; solve(); } ktj(); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ff first #define ss second #define pb push_back #define mp make_pair #define ld long double #define forn(low, high, i) for (i = low; i < high; i++) #define forrev(high, low, i) for (i = high; i >= low; i--) #define all(v) v.begin(), v.end() #define decimal_digits cout << fixed << setprecision(15); string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto &x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); #define GOOGLE 0 #define MULTIPLE_TC 0 const int inf = 1e9; const long long INF = 1e18; const int mod = 1e9 + 7; const int bit32 = log2(inf) + 3; const int bit64 = log2(INF) + 3; inline int add(long long a, long long b) { a += b; if (a >= mod) a -= mod; return a; } inline int sub(long long a, long long b) { a -= b; if (a < 0) a += mod; return a; } inline int mul(long long a, long long b) { a %= mod; b %= mod; return (int)((long long)a * b % mod); } inline int modexpo(long long a, long long b) { int res = 1; while (b > 0) { if (b & 1) res = mul(res, a); a = mul(a, a); b /= 2; } return res; } inline int divide(long long a, long long b) { return mul(a, modexpo(b, mod - 2)); } inline int inv(long long a) { return modexpo(a, mod - 2); } clock_t time_p = clock(); void ktj() { time_p = clock() - time_p; cerr << "Time elapsed : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } void pre() { // Reset each global variable (esp. for graphs) } const int N = 2e5 + 10; int a[N]; void solve() { long long n, k; int i, j; cin >> n >> k; for (i = 1; i <= n; i++) { cin >> a[i]; } int u = 1, step = 1, sub = 0, p = 0, cyc = 0; map<int, int> m, par; par[u] = p; vector<int> res; while (1) { if (m.find(u) != m.end()) { while (p != u) { res.push_back(p); p = par[p]; } res.push_back(u); sub = m[u] - 1; break; } m[u] = step; par[u] = p; p = u; u = a[u]; step += 1; } reverse(all(res)); cyc = res.size(); if (k > sub) { k -= sub; k %= cyc; cout << res[k] << '\n'; } else { int u = 1; for (i = 1; i <= k; i++) { u = a[u]; } cout << u << '\n'; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); pre(); int t = 1, T; if (MULTIPLE_TC) cin >> t; for (T = 1; T <= t; T++) { if (GOOGLE) cout << "Case #" << T << ": "; solve(); } ktj(); }
replace
125
128
125
136
0
Time elapsed : 0.000126
p02684
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define ll long long int #define rep(i, a, n) for (ll i = a; i < n; i++) #define repm(i, a, n) for (ll i = a; i >= n; i--) #define INF 1e9 #define LINF 1e17 #define MOD (int)(1e9 + 7) #define pi 3.141592653589 #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vd vector<double> #define vb vector<bool> #define vs vector<string> #define vii vector<vector<int>> #define vll vector<ll> #define vllll vector<vector<ll>> #define ALL(a) (a).begin(), (a).end() #define sort_v(a) sort(a.begin(), a.end()) #define reverse_v(a) reverse(a.begin(), a.end()) void print(bool c) { (c) ? (cout << "Yes" << endl) : (cout << "No" << endl); } void Print(bool c) { (c) ? (cout << "YES" << endl) : (cout << "NO" << endl); } // 最小公倍数 template <typename T> T gcd(T a, T b) { while (1) { if (a < b) swap(a, b); if (b == 0) break; a %= b; } return a; } // 最大公約数 template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); } // セカンドキー->ファーストキーの順ソート bool cmp(const pii &a, const pii &b) { if (a.second != b.second) return a.second < b.second; return a.first < b.first; } // 右下左上 int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; int eve(int x) { if (x % 400 == 0) return 1; if (x % 100 == 0) return 0; if (x % 4 == 0) return 1; return 0; } int main(void) { ll n, k; cin >> n >> k; vll a(n); rep(i, 0, n) { cin >> a[i]; a[i]--; } vll pass(n); ll start = 0, end = 0; rep(i, 0, k) { pass[start] = 1; if (!pass[a[start]]) start = a[start]; else { end = start, start = a[start]; break; } if (i == k - 1) return cout << start + 1 << endl, 0; } ll cnt = 1, sp = 0; for (int i = 0;; i++) { if (sp == start) { k -= i; cnt = 1; } else if (sp == end) { sp = a[sp]; k %= cnt; break; } sp = a[sp]; cnt++; } rep(j, 0, k) sp = a[sp]; return cout << sp + 1 << endl, 0; }
#include <algorithm> #include <bitset> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define ll long long int #define rep(i, a, n) for (ll i = a; i < n; i++) #define repm(i, a, n) for (ll i = a; i >= n; i--) #define INF 1e9 #define LINF 1e17 #define MOD (int)(1e9 + 7) #define pi 3.141592653589 #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vd vector<double> #define vb vector<bool> #define vs vector<string> #define vii vector<vector<int>> #define vll vector<ll> #define vllll vector<vector<ll>> #define ALL(a) (a).begin(), (a).end() #define sort_v(a) sort(a.begin(), a.end()) #define reverse_v(a) reverse(a.begin(), a.end()) void print(bool c) { (c) ? (cout << "Yes" << endl) : (cout << "No" << endl); } void Print(bool c) { (c) ? (cout << "YES" << endl) : (cout << "NO" << endl); } // 最小公倍数 template <typename T> T gcd(T a, T b) { while (1) { if (a < b) swap(a, b); if (b == 0) break; a %= b; } return a; } // 最大公約数 template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); } // セカンドキー->ファーストキーの順ソート bool cmp(const pii &a, const pii &b) { if (a.second != b.second) return a.second < b.second; return a.first < b.first; } // 右下左上 int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; int eve(int x) { if (x % 400 == 0) return 1; if (x % 100 == 0) return 0; if (x % 4 == 0) return 1; return 0; } int main(void) { ll n, k; cin >> n >> k; vll a(n); rep(i, 0, n) { cin >> a[i]; a[i]--; } vll pass(n); ll start = 0, end = 0; rep(i, 0, k) { pass[start] = 1; if (!pass[a[start]]) start = a[start]; else { end = start, start = a[start]; break; } if (i == k - 1) return cout << start + 1 << endl, 0; } // cout << start << " " << end << endl; if (end - start == 0) return cout << start + 1 << endl, 0; ll cnt = 1, sp = 0; for (int i = 0;; i++) { if (sp == start) { k -= i; cnt = 1; } else if (sp == end) { sp = a[sp]; k %= cnt; break; } sp = a[sp]; cnt++; } rep(j, 0, k) sp = a[sp]; return cout << sp + 1 << endl, 0; }
replace
94
95
94
97
TLE
p02684
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long int #define ull unsigned long long int #define I_read() freopen("input.tex", "r", stdin); #define O_write() freopen("output.txt", "w", stdout); int main() { // ios_base::sync_with_stdio(false); // cin.tie(0); // I_read(); // O_write(); ll a; ull b, temp, rem, numt = 0; cin >> a >> b; ll town[a], trv[a], ptow = 0; bool pass[a]; for (ll i = 0; i < a; i++) { cin >> town[i]; pass[i] = 0; // cout<<pass[i]<<" "; } cout << endl; while (pass[ptow] != 1 && numt < b) { trv[ptow] = numt; pass[ptow] = 1; // cout<<ptow+1<<"->"; ptow = town[ptow] - 1; numt++; } // cout<<"numt="<<numt<<"pass[ptow]="<<pass[ptow]<<endl; if (numt != b) { // cout<<"not-compleated-"; temp = numt - trv[ptow]; rem = (b - numt) % temp; // cout<<"temp="<<temp<<"rem="<<rem<<"->"; } while (rem > 0) { // trv[ptow] =numt; // pass[ptow] = 1; ptow = town[ptow] - 1; rem--; } cout << ptow + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long int #define ull unsigned long long int #define I_read() freopen("input.tex", "r", stdin); #define O_write() freopen("output.txt", "w", stdout); int main() { // ios_base::sync_with_stdio(false); // cin.tie(0); // I_read(); // O_write(); ll a; ull b, temp, rem = 0, numt = 0; cin >> a >> b; ll town[a], trv[a], ptow = 0; bool pass[a]; for (ll i = 0; i < a; i++) { cin >> town[i]; pass[i] = 0; // cout<<pass[i]<<" "; } cout << endl; while (pass[ptow] != 1 && numt < b) { trv[ptow] = numt; pass[ptow] = 1; // cout<<ptow+1<<"->"; ptow = town[ptow] - 1; numt++; } // cout<<"numt="<<numt<<"pass[ptow]="<<pass[ptow]<<endl; if (numt != b) { // cout<<"not-compleated-"; temp = numt - trv[ptow]; rem = (b - numt) % temp; // cout<<"temp="<<temp<<"rem="<<rem<<"->"; } while (rem > 0) { // trv[ptow] =numt; // pass[ptow] = 1; ptow = town[ptow] - 1; rem--; } cout << ptow + 1 << endl; return 0; }
replace
20
21
20
21
TLE
p02684
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define ll long long #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; static const int MAX = 200005; int N; ll K; int A[MAX], B[MAX], C[MAX]; int main() { cin >> N >> K; rep(i, N) { cin >> A[i]; A[i]--; } vector<int> V; int x = A[0]; B[0] = 1; C[0] = 0; for (int i = 1; B[x] == 0; i++) { B[x] = 1; C[x] = i; V.push_back(x); x = A[x]; } int n = V.size() + 1; int m = n - C[x]; ll a; if (n > K) a = K; else { a = ((K - C[x]) % m) + C[x]; } if (a == -1) cout << 1 << endl; else cout << V[a - 1] + 1 << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define ll long long #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; static const int MAX = 200005; int N; ll K; int A[MAX], B[MAX], C[MAX]; int main() { cin >> N >> K; rep(i, N) { cin >> A[i]; A[i]--; } vector<int> V; int x = A[0]; B[0] = 1; C[0] = 0; for (int i = 1; B[x] == 0; i++) { B[x] = 1; C[x] = i; V.push_back(x); x = A[x]; } int n = V.size() + 1; int m = n - C[x]; ll a; if (n > K) a = K; else { a = ((K - C[x]) % m) + C[x]; } if (a == 0) cout << 1 << endl; else cout << V[a - 1] + 1 << endl; return 0; }
replace
43
44
43
44
0
p02684
C++
Runtime Error
#include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; int a[200005]; vector<int> path; long long ind[200005] = {}; bool check_cycle(int cur_index) { if (ind[cur_index]) return true; return false; } int main() { long long n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; } long long temp_k = 0, cur_index = 1; while (!ind[cur_index] && temp_k < k) { ind[cur_index] = temp_k++; path.push_back(cur_index); cur_index = a[cur_index]; } if (temp_k == k) { cout << cur_index; } else { cout << path[((k - ind[cur_index]) % (path.size() - cur_index)) + cur_index]; } return 0; }
#include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; int a[200005]; vector<int> path; long long ind[200005] = {}; bool check_cycle(int cur_index) { if (ind[cur_index]) return true; return false; } int main() { long long n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; } long long temp_k = 0, cur_index = 1; while (!ind[cur_index] && temp_k < k) { ind[cur_index] = temp_k++; path.push_back(cur_index); cur_index = a[cur_index]; } if (temp_k == k) { cout << cur_index; } else { cout << path[((k - ind[cur_index]) % (path.size() - ind[cur_index])) + ind[cur_index]]; } return 0; }
replace
30
32
30
32
0
p02684
C++
Runtime Error
#include <cstdio> #define rep(i, n) for (int i = 0; i < (n); i++) typedef long long ll; int next[70][200008]; int main() { int N; ll K; scanf("%d%lld", &N, &K); rep(i, N) { scanf("%d", &next[0][i]); next[0][i]--; } rep(j, 70) { rep(i, N) { next[j + 1][i] = next[j][next[j][i]]; } } int cur = 0; rep(j, 70) { if (K % 2) cur = next[j][cur]; K /= 2; } printf("%d\n", cur + 1); return 0; }
#include <cstdio> #define rep(i, n) for (int i = 0; i < (n); i++) typedef long long ll; int next[70][200008]; int main() { int N; ll K; scanf("%d%lld", &N, &K); rep(i, N) { scanf("%d", &next[0][i]); next[0][i]--; } rep(j, 69) { rep(i, N) { next[j + 1][i] = next[j][next[j][i]]; } } int cur = 0; rep(j, 70) { if (K % 2) cur = next[j][cur]; K /= 2; } printf("%d\n", cur + 1); return 0; }
replace
14
15
14
15
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back using namespace std; typedef long long LL; int T[200001]; bool vis[200001]; int ans, mulai, selesai; vector<int> d; LL K; void dfs(int nod) { vis[nod] = 1; d.pb(nod); if (K == 0) { ans = nod; return; } if (!vis[T[nod]]) K--, dfs(T[nod]); else { mulai = T[nod]; selesai = nod; } } int main() { int N; scanf("%d%lld", &N, &K); for (int i = 1; i <= N; i++) { scanf("%d", &T[i]); } dfs(1); if (K > 0) { int cycle; K--; int l1 = d.size(); for (int i = 0; i < l1; i++) { if (d[i] == mulai) { for (int j = i + 1; j < l1; j++) { if (d[j] == selesai) { cycle = j - i + 1; mulai = i; selesai = j; i = l1; break; } } } } K %= cycle; ans = d[mulai + K]; } printf("%d\n", ans); }
#include <bits/stdc++.h> #define pb push_back using namespace std; typedef long long LL; int T[200001]; bool vis[200001]; int ans, mulai, selesai; vector<int> d; LL K; void dfs(int nod) { vis[nod] = 1; d.pb(nod); if (K == 0) { ans = nod; return; } if (!vis[T[nod]]) K--, dfs(T[nod]); else { mulai = T[nod]; selesai = nod; } } int main() { int N; scanf("%d%lld", &N, &K); for (int i = 1; i <= N; i++) { scanf("%d", &T[i]); } dfs(1); if (K > 0) { int cycle; K--; int l1 = d.size(); for (int i = 0; i < l1; i++) { if (d[i] == mulai) { for (int j = i; j < l1; j++) { if (d[j] == selesai) { cycle = j - i + 1; mulai = i; selesai = j; i = l1; break; } } } } K %= cycle; ans = d[mulai + K]; } printf("%d\n", ans); }
replace
39
40
39
40
0
p02684
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <sstream> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> Vec; typedef vector<pair<ll, ll>> Pair; #define rep(i, n) for (ll i = 0; i < n; i++) #define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0])) // static const ll MOD = 1000000007; // static const ll INF = 100000000; // #define PI 3.14159265359 int main() { // input ll N, K; cin >> N >> K; Vec A(N); rep(i, N) { cin >> A[i]; --A[i]; } // process Vec is_visited(N, 0); ll now = 0, loop = 0, remaining_K = 0; rep(i, K) { if (is_visited[now] != 0) { loop = i - is_visited[now]; remaining_K = K - i; break; } is_visited[now] = i; now = A[now]; } remaining_K %= loop; rep(i, remaining_K) { now = A[now]; } cout << now + 1 << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <sstream> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> Vec; typedef vector<pair<ll, ll>> Pair; #define rep(i, n) for (ll i = 0; i < n; i++) #define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0])) // static const ll MOD = 1000000007; // static const ll INF = 100000000; // #define PI 3.14159265359 int main() { // input ll N, K; cin >> N >> K; Vec A(N); rep(i, N) { cin >> A[i]; --A[i]; } // process Vec is_visited(N, 0); ll now = 0, loop = 0, remaining_K = 0; rep(i, K) { if (is_visited[now] != 0) { loop = i - is_visited[now]; remaining_K = K - i; break; } is_visited[now] = i; now = A[now]; } if (remaining_K != 0) { remaining_K %= loop; rep(i, remaining_K) { now = A[now]; } } cout << now + 1 << endl; return 0; }
replace
48
50
48
52
0
p02684
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define INF 2000000007 #define LINF 100000000000000007 #define MOD 1000000007 #define int long long #define rep(i, n) for (int i = 0; i < n; i++) #define repb(i, n) for (int i = n - 1; i >= 0; i--) #define MODE 0 #ifdef MODE #define DEB(X) cout << #X << ": " << X << " "; #define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " "; #define END cout << endl; #else #define DEB(X) \ {} #define ARDEB(i, X) \ {} #define END \ {} #endif typedef long long ll; typedef pair<int, int> P; struct edge { int to, cost; }; int ceil2(int a, int b) { if (a % b) { return a / b + 1; } else { return a / b; } } using namespace std; int k; int n, m, x, ans = INF; int a[211111]; int f[211111]; vector<int> v, v2; signed main() { cin >> n >> k; rep(i, n) { cin >> a[i]; a[i]--; } int now = 0; f[0] = 1; int fini; v.push_back(0); while (true) { now = a[now]; if (f[now]) { fini = now; break; } f[now] = 1; v.push_back(now); } int finif = 0; rep(i, v.size()) { if (v[i] == fini) finif = 1; if (finif) v2.push_back(v[i]); } int fcnt = 0, scnt = 0, mode = 0; rep(i, v.size()) { if (mode == 0) { fcnt++; if (i == v.size() - 1) continue; if (v[i + 1] == fini) { mode = 1; } } else { scnt++; } } // cout<<fcnt<<" "<<scnt<<endl; // cout<<v.size()<<endl; // rep(i,v2.size())cout<<v2[i]+1<<" ";cout<<endl; k -= fcnt; k++; if (scnt == 0) { k %= fcnt; if (k == 0) cout << v2[fcnt - 1] + 1 << endl; else cout << v2[k - 1] + 1 << endl; return 0; } k %= scnt; // cout<<endl; // cout<<k<<endl; if (k == 0) cout << v2[scnt - 1] + 1 << endl; else cout << v2[k - 1] + 1 << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define INF 2000000007 #define LINF 100000000000000007 #define MOD 1000000007 #define int long long #define rep(i, n) for (int i = 0; i < n; i++) #define repb(i, n) for (int i = n - 1; i >= 0; i--) #define MODE 0 #ifdef MODE #define DEB(X) cout << #X << ": " << X << " "; #define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " "; #define END cout << endl; #else #define DEB(X) \ {} #define ARDEB(i, X) \ {} #define END \ {} #endif typedef long long ll; typedef pair<int, int> P; struct edge { int to, cost; }; int ceil2(int a, int b) { if (a % b) { return a / b + 1; } else { return a / b; } } using namespace std; int k; int n, m, x, ans = INF; int a[211111]; int f[211111]; vector<int> v, v2; signed main() { cin >> n >> k; rep(i, n) { cin >> a[i]; a[i]--; } int now = 0; f[0] = 1; int fini; v.push_back(0); while (true) { now = a[now]; if (f[now]) { fini = now; break; } f[now] = 1; v.push_back(now); } int finif = 0; rep(i, v.size()) { if (v[i] == fini) finif = 1; if (finif) v2.push_back(v[i]); } int fcnt = 0, scnt = 0, mode = 0; rep(i, v.size()) { if (mode == 0) { fcnt++; if (i == v.size() - 1) continue; if (v[i + 1] == fini) { mode = 1; } } else { scnt++; } } // cout<<fcnt<<" "<<scnt<<endl; // cout<<v.size()<<endl; // rep(i,v.size())cout<<v[i]+1<<" ";cout<<endl; if (fcnt > k) { cout << v[k] + 1 << endl; return 0; } k -= fcnt; k++; if (scnt == 0) { k %= fcnt; if (k == 0) cout << v2[fcnt - 1] + 1 << endl; else cout << v2[k - 1] + 1 << endl; return 0; } k %= scnt; // cout<<endl; // cout<<k<<endl; if (k == 0) cout << v2[scnt - 1] + 1 << endl; else cout << v2[k - 1] + 1 << endl; }
replace
94
95
94
100
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; signed main(void) { int n; long long k, cnt, cnt_loop; cin >> n >> k; int a[n], label[n]; int pre, tmp; vector<bool> visit(n, false); vector<int> ans; rep(i, n) { cin >> a[i]; a[i]--; } queue<int> que; que.push(a[0]); pre = -1; cnt = 1; label[0] = cnt; visit[0] = true; while (true) { int now = que.front(); que.pop(); if (visit[now]) { cnt_loop = label[pre] - label[now] + 1; tmp = now; break; } cnt++; label[now] = cnt; visit[now] = true; que.push(a[now]); pre = now; } ans.push_back(tmp + 1); rep(i, cnt_loop - 1) { int now = a[tmp]; tmp = now; ans.push_back(tmp + 1); } k -= (cnt - cnt_loop); k %= cnt_loop; cout << ans[k] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; signed main(void) { int n; long long k, cnt, cnt_loop; cin >> n >> k; int a[n], label[n]; int pre, tmp; vector<bool> visit(n, false); vector<int> ans; rep(i, n) { cin >> a[i]; a[i]--; } queue<int> que; que.push(a[0]); pre = -1; cnt = 1; label[0] = cnt; visit[0] = true; while (true) { int now = que.front(); que.pop(); if (visit[now]) { cnt_loop = label[pre] - label[now] + 1; tmp = now; break; } if (cnt >= k) { cout << now + 1 << endl; return 0; } cnt++; label[now] = cnt; visit[now] = true; que.push(a[now]); pre = now; } ans.push_back(tmp + 1); rep(i, cnt_loop - 1) { int now = a[tmp]; tmp = now; ans.push_back(tmp + 1); } k -= (cnt - cnt_loop); k %= cnt_loop; cout << ans[k] << endl; return 0; }
insert
31
31
31
35
0
p02684
C++
Runtime Error
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define PI 3.14159265359 using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pai; typedef pair<ll, ll> pal; const int MOD = 1e9 + 7; const int MODD = 998244353; const ll LLINF = 1e18; ll dub[60][100001]; int main() { ll n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } int log_k = log2(k); for (int i = 0; i < n; i++) { dub[0][i] = a[i]; } for (int i = 1; i <= log_k; i++) { for (int j = 0; j < n; j++) { dub[i][j] = dub[i - 1][dub[i - 1][j]]; } } ll ans = 0; for (int i = 0; i <= log_k; i++) { if (k & (1LL << i)) { ans = dub[i][ans]; } } cout << ans + 1 << endl; return 0; }
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define PI 3.14159265359 using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pai; typedef pair<ll, ll> pal; const int MOD = 1e9 + 7; const int MODD = 998244353; const ll LLINF = 1e18; ll dub[62][200001]; int main() { ll n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } int log_k = log2(k); for (int i = 0; i < n; i++) { dub[0][i] = a[i]; } for (int i = 1; i <= log_k; i++) { for (int j = 0; j < n; j++) { dub[i][j] = dub[i - 1][dub[i - 1][j]]; } } ll ans = 0; for (int i = 0; i <= log_k; i++) { if (k & (1LL << i)) { ans = dub[i][ans]; } } cout << ans + 1 << endl; return 0; }
replace
21
22
21
22
0
p02684
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdint> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define All(v) (v).begin(), (v).end() #define pb push_back #define MP(a, b) make_pair((a), (b)) using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; int main() { ll N, K; cin >> N >> K; vector<ll> A(N); rep(i, N) { cin >> A[i]; A[i]--; } ll now = 0; vector<ll> hist; set<ll> se; while (1) { hist.pb(now); se.insert(now); if (se.count(A[now])) break; now = A[now]; } ll save = 0; rep(i, hist.size()) { if (hist[i] == A[now]) { save = i; break; } } vector<ll> new_hist; for (int i = save; i < hist.size(); i++) { new_hist.pb(hist[i]); } K -= save; ll len = new_hist.size(); cout << new_hist[K % len] + 1 << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdint> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define All(v) (v).begin(), (v).end() #define pb push_back #define MP(a, b) make_pair((a), (b)) using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; int main() { ll N, K; cin >> N >> K; vector<ll> A(N); rep(i, N) { cin >> A[i]; A[i]--; } ll now = 0; vector<ll> hist; set<ll> se; while (1) { hist.pb(now); se.insert(now); if (se.count(A[now])) break; now = A[now]; } ll save = 0; rep(i, hist.size()) { if (hist[i] == A[now]) { save = i; break; } } vector<ll> new_hist; for (int i = save; i < hist.size(); i++) { new_hist.pb(hist[i]); } if (K < save) { cout << hist[K] + 1 << endl; return 0; } K -= save; ll len = new_hist.size(); cout << new_hist[K % len] + 1 << endl; return 0; }
insert
54
54
54
58
0
p02684
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define in(n) cin >> (n) #define out(ans) cout << (ans) << endl; #define keta(n) cout << fixed << setprecision((n)); using ll = int64_t; using v = vector<int>; int main() { ll N, K; cin >> N >> K; vector<ll> tolist(N); for (ll i = 0; i < N; i++) { ll A; cin >> A; A--; tolist.at(i) = A; } bool roopjud = false; vector<bool> judlist(N, false); judlist.at(0) = true; ll gpoint = 0, npoint, rs = -1, mcount = 0, rcount = 0, ans; while (rs == -1 && mcount <= N) { npoint = tolist.at(gpoint); if (judlist.at(npoint)) { rs = gpoint; roopjud = true; } else { gpoint = npoint; mcount++; } } if (roopjud) { gpoint = rs; npoint = -1; while (npoint != rs) { npoint = tolist.at(gpoint); rcount++; gpoint = npoint; } if (K - mcount >= 0) { gpoint = rs; if ((K - mcount) % rcount == 0) { ans == rs; } else { for (ll i = 0; i < (K - mcount) % rcount; i++) { ans = tolist.at(gpoint); gpoint = ans; } } } else { gpoint = 0; for (ll i = 0; i < K; i++) { ans = tolist.at(gpoint); gpoint = ans; } } } else { gpoint = 0; for (ll i = 0; i < K; i++) { ans = tolist.at(gpoint); gpoint = ans; } } ans++; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define in(n) cin >> (n) #define out(ans) cout << (ans) << endl; #define keta(n) cout << fixed << setprecision((n)); using ll = int64_t; using v = vector<int>; int main() { ll N, K; cin >> N >> K; vector<ll> tolist(N); for (ll i = 0; i < N; i++) { ll A; cin >> A; A--; tolist.at(i) = A; } bool roopjud = false; vector<bool> judlist(N, false); judlist.at(0) = true; ll gpoint = 0, npoint, rs = -1, mcount = 0, rcount = 0, ans; while (rs == -1 && mcount <= N) { npoint = tolist.at(gpoint); if (judlist.at(npoint)) { rs = gpoint; roopjud = true; } else { judlist.at(gpoint) = true; gpoint = npoint; mcount++; } } if (roopjud) { gpoint = rs; npoint = -1; while (npoint != rs) { npoint = tolist.at(gpoint); rcount++; gpoint = npoint; } if (K - mcount >= 0) { gpoint = rs; if ((K - mcount) % rcount == 0) { ans == rs; } else { for (ll i = 0; i < (K - mcount) % rcount; i++) { ans = tolist.at(gpoint); gpoint = ans; } } } else { gpoint = 0; for (ll i = 0; i < K; i++) { ans = tolist.at(gpoint); gpoint = ans; } } } else { gpoint = 0; for (ll i = 0; i < K; i++) { ans = tolist.at(gpoint); gpoint = ans; } } ans++; cout << ans << endl; }
insert
32
32
32
33
TLE
p02684
C++
Runtime Error
// JAI SHREE RAM #pragma GCC optimize("O3") typedef long long int ll; typedef unsigned long long int ull; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define FOR(i, n) for (int i = 0; i < n; i++) #define MOD 1000000007 #define sz(a) int((a).size()) #define forn(i, n) for (int i = 0; i < (n); ++i) #define fore(i, l, r) for (int i = int(l); i < int(r); ++i) #define loop for (int i = 0; i < n; i++) #define max3(a, b, c) max(a, max(b, c)) #define max4(a, b, c, d) max3(a, max(b, c), d) #define max5(a, b, c, d, e) max4(a, max(b, c), d, e) #define min3(a, b, c) min(a, min(b, c)) #define min4(a, b, c, d) min3(a, min(b, c), d) #define min5(a, b, c, d, e) min4(a, min(b, c), d, e) #define sec second #define fir first #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define repe(i, m, n) for (ll(i) = (m); (i) < (n); (i)++) // #define all(a) (a).begin(),(a).end() #define vec vector<int> #define M_PI 3.14159265358979323846 /* pi */ #define pb push_back #define pii pair<int, int> #include <bits/stdc++.h> #include <cmath> using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast ll n, k; cin >> n >> k; std::vector<ll> v(n); rep(i, n) { cin >> v[i]; v[i]--; } if (k == 1) { cout << v[0] << endl; return 0; } ll arr[10005] = {0}; arr[0] = 1; ll i = 0, index = 0, size; while (true) { k--; if (arr[v[i]] == 1) { i = v[i]; index = i; break; } arr[v[i]] = 1; if (k == 0) { cout << v[i] + 1 << endl; return 0; } i = v[i]; } if (k == 0) { cout << i + 1 << endl; return 0; } ll c = 0, x = 0; memset(arr, 0, sizeof(arr)); arr[i] = 1; while (1) { c++; i = v[i]; if (arr[i] == 1) break; arr[i] = 1; } // cout<<c<<"->"<<k<<endl; // cout<<k%c<<endl; ll rem = k % c; // cout<<rem<<endl; ll ans = index; while (rem--) { ans = v[index]; index = v[index]; } cout << ans + 1 << endl; return 0; }
// JAI SHREE RAM #pragma GCC optimize("O3") typedef long long int ll; typedef unsigned long long int ull; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define FOR(i, n) for (int i = 0; i < n; i++) #define MOD 1000000007 #define sz(a) int((a).size()) #define forn(i, n) for (int i = 0; i < (n); ++i) #define fore(i, l, r) for (int i = int(l); i < int(r); ++i) #define loop for (int i = 0; i < n; i++) #define max3(a, b, c) max(a, max(b, c)) #define max4(a, b, c, d) max3(a, max(b, c), d) #define max5(a, b, c, d, e) max4(a, max(b, c), d, e) #define min3(a, b, c) min(a, min(b, c)) #define min4(a, b, c, d) min3(a, min(b, c), d) #define min5(a, b, c, d, e) min4(a, min(b, c), d, e) #define sec second #define fir first #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define repe(i, m, n) for (ll(i) = (m); (i) < (n); (i)++) // #define all(a) (a).begin(),(a).end() #define vec vector<int> #define M_PI 3.14159265358979323846 /* pi */ #define pb push_back #define pii pair<int, int> #include <bits/stdc++.h> #include <cmath> using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast ll n, k; cin >> n >> k; std::vector<ll> v(n); rep(i, n) { cin >> v[i]; v[i]--; } if (k == 1) { cout << v[0] << endl; return 0; } ll arr[200005] = {0}; arr[0] = 1; ll i = 0, index = 0, size; while (true) { k--; if (arr[v[i]] == 1) { i = v[i]; index = i; break; } arr[v[i]] = 1; if (k == 0) { cout << v[i] + 1 << endl; return 0; } i = v[i]; } if (k == 0) { cout << i + 1 << endl; return 0; } ll c = 0, x = 0; memset(arr, 0, sizeof(arr)); arr[i] = 1; while (1) { c++; i = v[i]; if (arr[i] == 1) break; arr[i] = 1; } // cout<<c<<"->"<<k<<endl; // cout<<k%c<<endl; ll rem = k % c; // cout<<rem<<endl; ll ans = index; while (rem--) { ans = v[index]; index = v[index]; } cout << ans + 1 << endl; return 0; }
replace
48
49
48
49
-11
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> ii; typedef pair<ll, ll> pll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, k, pos = 0; cin >> n >> k; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } map<ll, ll> voyage; while (voyage[pos] < 1 && k > 0) { voyage[pos] = k; pos = a[pos]; k--; } k %= voyage[pos] - k; while (k > 0) { pos = a[pos]; k--; } cout << pos + 1 << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> ii; typedef pair<ll, ll> pll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, k, pos = 0; cin >> n >> k; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } map<ll, ll> voyage; while (voyage[pos] < 1 && k > 0) { voyage[pos] = k; pos = a[pos]; k--; } if (voyage[pos] > 0) k %= voyage[pos] - k; while (k > 0) { pos = a[pos]; k--; } cout << pos + 1 << "\n"; return 0; }
replace
28
29
28
30
0
p02684
C++
Runtime Error
#define _GLIBCXX_DEBUG #include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define rep2(i, s, n) for (int i = s; i < (int)n; i++) #define ll long long #define all(v) v.begin(), v.end() using namespace std; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; map<ll, ll> mp; mp[0]++; ll key = 0; ll time = 0; ll loop = 0; vector<ll> vec; vector<ll> vec0; vec0.push_back(0); rep(i, 2 * n + 10) { mp[a[key] - 1]++; if (mp[a[key] - 1] == 1) { time++; vec0.push_back(a[key]); } if (mp[a[key] - 1] == 2) { loop++; vec.push_back(a[key]); } key = a[key] - 1; } if (k > time && (k - time) % loop != 0) cout << vec[(k - time) % loop - 1] << endl; if (k > time && (k - time) % loop == 0) cout << vec[(k - time) % loop - 1 + loop] << endl; else cout << vec0[k] << endl; }
#define _GLIBCXX_DEBUG #include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define rep2(i, s, n) for (int i = s; i < (int)n; i++) #define ll long long #define all(v) v.begin(), v.end() using namespace std; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; map<ll, ll> mp; mp[0]++; ll key = 0; ll time = 0; ll loop = 0; vector<ll> vec; vector<ll> vec0; vec0.push_back(0); rep(i, 2 * n + 10) { mp[a[key] - 1]++; if (mp[a[key] - 1] == 1) { time++; vec0.push_back(a[key]); } if (mp[a[key] - 1] == 2) { loop++; vec.push_back(a[key]); } key = a[key] - 1; } if (k > time && (k - time) % loop != 0) cout << vec[(k - time) % loop - 1] << endl; else if (k > time && (k - time) % loop == 0) cout << vec[loop - 1] << endl; else cout << vec0[k] << endl; }
replace
46
48
46
48
0
p02684
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <forward_list> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <tuple> #include <utility> #include <vector> #define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i)) using namespace std; using ll = long long; using P = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = (1ll << 60); int main(void) { ll n, k; cin >> n >> k; vector<ll> a(n), p; map<ll, ll> m; for (int i = 1; i <= n; i++) { cin >> a[i]; m[i] = INF; } ll time = 0; ll city = 1; ll start, end; while (true) { if (m[city] == INF) { m[city] = time; p.push_back(city); city = a[city]; } else { start = m[city]; end = time; break; } time++; } if (start > k) { cout << p[k] << endl; return 0; } cout << p[(k - start) % (start - end) + start] << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <forward_list> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <tuple> #include <utility> #include <vector> #define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i)) using namespace std; using ll = long long; using P = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = (1ll << 60); int main(void) { ll n, k; cin >> n >> k; vector<ll> a(n + 1), p; map<ll, ll> m; for (int i = 1; i <= n; i++) { cin >> a[i]; m[i] = INF; } ll time = 0; ll city = 1; ll start, end; while (true) { if (m[city] == INF) { m[city] = time; p.push_back(city); city = a[city]; } else { start = m[city]; end = time; break; } time++; } if (start > k) { cout << p[k] << endl; return 0; } cout << p[(k - start) % (start - end) + start] << endl; }
replace
28
29
28
29
0
p02684
C++
Time Limit Exceeded
// i_m_arin #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<long long> vll; typedef vector<string> vs; typedef priority_queue<ll> pqll; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef map<ll, ll> mpll; typedef set<ll> stll; #define f(i, a, n) for (ll i = a; i < (ll)n; i++) #define f2(i, a, b) for (ll i = a; i <= (ll)b; i++) #define pb push_back #define ff first #define ss second #define pf push_front #define mp make_pair #define all(a) (a).begin(), (a).end() #define decimal(x) fixed << setprecision(x) #define fsort(a) sort(all(a)) #define rsort(a) sort(all(a), greater<>()) #define fast_io \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define mem0(a) memset(a, 0, sizeof(a)) #define mem1(a) memset(a, -1, sizeof(a)) #define lcm(a, b) (a * b) / __gcd(a, b) #define ub(v, x) upper_bound(all(v), x) - v.begin() #define lb(v, x) lower_bound(all(v), x) - v.begin() const ld PI = acos(-1); const ll MOD = 1e9 + 7; const ll INF = LLONG_MAX; const ll SIZE = 100000; vll ar[1000000]; bool vis[1000000]; ll level[1000000]; ll pos = -1, pos2; void dfs1(ll node, ll par) { if (par == 0) level[node] = 0; else level[node] = level[par] + 1; vis[node] = true; for (auto x : ar[node]) { if (!vis[x]) dfs1(x, node); } } ll dfs(ll node, ll par) { vis[node] = true; for (auto x : ar[node]) { if (!vis[x]) { ll val = dfs(x, node); if (val != 0) return val; } else { if (x != par) { pos = x; pos2 = node; return abs(level[node] - level[x] + 1); } } } return 0; } // vll vect(1000000); // void dfs2(ll cur,ll req){ // vis[cur]=true; // vect.pb(cur); // if(cur==req) return; // for(auto x:ar[cur]){ // if(!vis[x]) dfs2(x,req); // } // } void solve() { ll n, k; cin >> n >> k; ll a[n + 5]; f(i, 0, 1000000) vis[i] = false; f(i, 1, n + 1) { ll x; cin >> x; a[i] = x; ar[i].pb(x); } dfs1(1, 0); f(i, 0, 1000000) vis[i] = false; ll ret = dfs(1, 0); if (pos == -1) { ll ans = 0, i = 1; while (k--) { ans = a[i]; i = a[i]; } cout << ans << endl; } else { ll ans = 0; ll fir = level[pos] - level[1]; if (k <= fir + ret) { ll i = 1; while (k--) { ans = a[i]; i = a[i]; } cout << ans << endl; } else { k = k - (fir + ret); if (k > ret) k = k % ret; if (k == 0) k = ret; // f(i,0,1000000) vis[i]=false; // dfs2(pos,pos2); // vll vet(n+5); // f(i,0,vect.size()){ // if(vect[i]>0){ // vet[vect[i]]=a[vect[i]]; // } // } ll i = pos; while (k--) { ans = a[i]; i = a[i]; } cout << ans << endl; } } } int main() { fast_io; int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
// i_m_arin #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<long long> vll; typedef vector<string> vs; typedef priority_queue<ll> pqll; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef map<ll, ll> mpll; typedef set<ll> stll; #define f(i, a, n) for (ll i = a; i < (ll)n; i++) #define f2(i, a, b) for (ll i = a; i <= (ll)b; i++) #define pb push_back #define ff first #define ss second #define pf push_front #define mp make_pair #define all(a) (a).begin(), (a).end() #define decimal(x) fixed << setprecision(x) #define fsort(a) sort(all(a)) #define rsort(a) sort(all(a), greater<>()) #define fast_io \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define mem0(a) memset(a, 0, sizeof(a)) #define mem1(a) memset(a, -1, sizeof(a)) #define lcm(a, b) (a * b) / __gcd(a, b) #define ub(v, x) upper_bound(all(v), x) - v.begin() #define lb(v, x) lower_bound(all(v), x) - v.begin() const ld PI = acos(-1); const ll MOD = 1e9 + 7; const ll INF = LLONG_MAX; const ll SIZE = 100000; vll ar[1000000]; bool vis[1000000]; ll level[1000000]; ll pos = -1, pos2; void dfs1(ll node, ll par) { if (par == 0) level[node] = 0; else level[node] = level[par] + 1; vis[node] = true; for (auto x : ar[node]) { if (!vis[x]) dfs1(x, node); } } ll dfs(ll node, ll par) { vis[node] = true; for (auto x : ar[node]) { if (!vis[x]) { ll val = dfs(x, node); if (val != 0) return val; } else { if (x != par) { pos = x; pos2 = node; return abs(level[node] - level[x] + 1); } } } return 0; } // vll vect(1000000); // void dfs2(ll cur,ll req){ // vis[cur]=true; // vect.pb(cur); // if(cur==req) return; // for(auto x:ar[cur]){ // if(!vis[x]) dfs2(x,req); // } // } void solve() { ll n, k; cin >> n >> k; ll a[n + 5]; f(i, 0, 1000000) vis[i] = false; f(i, 1, n + 1) { ll x; cin >> x; a[i] = x; ar[i].pb(x); } dfs1(1, 0); f(i, 0, 1000000) vis[i] = false; ll ret = dfs(1, 0); if (pos == -1) { ll ans = 0, i = 1; while (n--) { ans = a[i]; i = a[i]; } cout << ans << endl; } else { ll ans = 0; ll fir = level[pos] - level[1]; if (k <= fir + ret) { ll i = 1; while (k--) { ans = a[i]; i = a[i]; } cout << ans << endl; } else { k = k - (fir + ret); if (k > ret) k = k % ret; if (k == 0) k = ret; // f(i,0,1000000) vis[i]=false; // dfs2(pos,pos2); // vll vet(n+5); // f(i,0,vect.size()){ // if(vect[i]>0){ // vet[vect[i]]=a[vect[i]]; // } // } ll i = pos; while (k--) { ans = a[i]; i = a[i]; } cout << ans << endl; } } } int main() { fast_io; int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
105
106
105
106
TLE
p02684
C++
Runtime Error
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") #pragma GCC optimize("unroll-loops") // #pragma warning(disable : 4996) #ifdef _MSC_VER #include <intrin.h> #define __builtin_popcount __popcnt #endif #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits.h> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define FORR(i, m, n) for (int i = m - 1; i >= n; --i) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define REVERSE(v, n) reverse(v, v + n); #define VREVERSE(v) reverse(v.begin(), v.end()) #define ll long long #define print(x) cout << (x) << '\n' #define pe(x) cout << (x) << " " #define DEBUG(x) cout << #x << ": " << x << endl #define lb(v, n) lower_bound(v.begin(), v.end(), (n)) #define ub(v, n) upper_bound(v.begin(), v.end(), (n)) #define int long long // #define double long double #define all(x) (x).begin(), (x).end() #define print_space(v) \ REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ") template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef array<int, 3> arr3; std::random_device rd; std::mt19937 mt(rd()); constexpr ll MOD = 1e9 + 7; constexpr int MAX = 100050; const double pi = acos(-1); constexpr double EPS = 1e-12; constexpr ll INF = 1e16; void yes(bool c) { if (c) print("Yes"); else print("No"); }; int dp[100010][70]; int nxt[100010]; void solve() { int N, K; cin >> N >> K; REP(i, N) { cin >> nxt[i]; nxt[i]--; } REP(i, N) { dp[i][0] = nxt[i]; } REP(k, 61) { REP(i, N) { dp[i][k + 1] = dp[dp[i][k]][k]; } } int now = 0; REPR(i, 60) { if (K & (1LL << i)) { now = dp[now][i]; } } print(now + 1); } signed main() { cin.tie(0); ios::sync_with_stdio(false); // int q; cin >> q; // while (q--) solve(); }
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") #pragma GCC optimize("unroll-loops") // #pragma warning(disable : 4996) #ifdef _MSC_VER #include <intrin.h> #define __builtin_popcount __popcnt #endif #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits.h> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define FORR(i, m, n) for (int i = m - 1; i >= n; --i) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define REVERSE(v, n) reverse(v, v + n); #define VREVERSE(v) reverse(v.begin(), v.end()) #define ll long long #define print(x) cout << (x) << '\n' #define pe(x) cout << (x) << " " #define DEBUG(x) cout << #x << ": " << x << endl #define lb(v, n) lower_bound(v.begin(), v.end(), (n)) #define ub(v, n) upper_bound(v.begin(), v.end(), (n)) #define int long long // #define double long double #define all(x) (x).begin(), (x).end() #define print_space(v) \ REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ") template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef array<int, 3> arr3; std::random_device rd; std::mt19937 mt(rd()); constexpr ll MOD = 1e9 + 7; constexpr int MAX = 100050; const double pi = acos(-1); constexpr double EPS = 1e-12; constexpr ll INF = 1e16; void yes(bool c) { if (c) print("Yes"); else print("No"); }; int dp[200010][70]; int nxt[200010]; void solve() { int N, K; cin >> N >> K; REP(i, N) { cin >> nxt[i]; nxt[i]--; } REP(i, N) { dp[i][0] = nxt[i]; } REP(k, 61) { REP(i, N) { dp[i][k + 1] = dp[dp[i][k]][k]; } } int now = 0; REPR(i, 60) { if (K & (1LL << i)) { now = dp[now][i]; } } print(now + 1); } signed main() { cin.tie(0); ios::sync_with_stdio(false); // int q; cin >> q; // while (q--) solve(); }
replace
81
83
81
83
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll N, K; cin >> N >> K; vector<ll> A(N + 1); for (int i = 1; i <= N; i++) cin >> A[i]; vector<int> tp(N + 1); tp[0] = 1; vector<int> visited(N + 1, -1); visited[1] = 0; ll cycle = 0; ll first = 0; for (ll i = 1; i <= N; i++) { tp[i] = A[tp[i - 1]]; if (visited[tp[i]] != -1) { first = visited[tp[i]]; cycle = i - visited[tp[i]]; break; } else { visited[tp[i]] = i; } } ll n; if (K > first) n = ((K - first) % cycle) + first; else n = K; cout << tp[K] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll N, K; cin >> N >> K; vector<ll> A(N + 1); for (int i = 1; i <= N; i++) cin >> A[i]; vector<int> tp(N + 1); tp[0] = 1; vector<int> visited(N + 1, -1); visited[1] = 0; ll cycle = 0; ll first = 0; for (ll i = 1; i <= N; i++) { tp[i] = A[tp[i - 1]]; if (visited[tp[i]] != -1) { first = visited[tp[i]]; cycle = i - visited[tp[i]]; break; } else { visited[tp[i]] = i; } } ll n; if (K > first) n = ((K - first) % cycle) + first; else n = K; cout << tp[n] << endl; }
replace
34
35
34
35
0
p02684
C++
Runtime Error
// #include<bits/stdc++.h> #include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <set> #include <vector> /* #include <boost/multiprecision/cpp_dec_float.hpp> #include<boost/multiprecision/cpp_int.hpp> #include <boost/rational.hpp> namespace mp = boost::multiprecision; using Real = mp::number<mp::cpp_dec_float<1024>>; using Bint = mp::cpp_int; */ using namespace std; #define int long long #define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++) #define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--) #define FOR(i, n) for ((i) = (0); (i) < (n); (i)++) #define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--) #define MOD 1000000007 signed main() { int N, K; int A[20000] = {0}; int An[20000] = {0}; cin >> N >> K; for (int i = 1; i <= N; i++) { cin >> A[i]; } int i = 1; int n = 1; K--; while (K >= 0) { if (An[n] != 0) { int diff = i - An[n]; K %= diff; } An[n] = i; i++; K--; n = A[n]; } cout << n << endl; }
// #include<bits/stdc++.h> #include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <set> #include <vector> /* #include <boost/multiprecision/cpp_dec_float.hpp> #include<boost/multiprecision/cpp_int.hpp> #include <boost/rational.hpp> namespace mp = boost::multiprecision; using Real = mp::number<mp::cpp_dec_float<1024>>; using Bint = mp::cpp_int; */ using namespace std; #define int long long #define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++) #define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--) #define FOR(i, n) for ((i) = (0); (i) < (n); (i)++) #define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--) #define MOD 1000000007 signed main() { int N, K; int A[200010] = {0}; int An[200010] = {0}; cin >> N >> K; for (int i = 1; i <= N; i++) { cin >> A[i]; } int i = 1; int n = 1; K--; while (K >= 0) { if (An[n] != 0) { int diff = i - An[n]; K %= diff; } An[n] = i; i++; K--; n = A[n]; } cout << n << endl; }
replace
28
30
28
30
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int N; ll K; vector<bool> visited; vector<int> dic; vector<int> A; int dfs(int cur) { K--; if (visited[cur]) return cur; if (K == 0) return cur; visited[cur] = true; dic.push_back(cur); dfs(A[cur]); } int main() { cin >> N >> K; A.resize(N); visited.resize(N); fill(visited.begin(), visited.end(), false); for (int i = 0; i < N; i++) { cin >> A[i]; A[i]--; } visited[0] = true; dic.push_back(0); int cur = dfs(A[0]); ll idx = 0; vector<int> last; int i; for (i = 0; i < (int)dic.size(); i++) { if (dic[i] == cur) { idx = i; last.push_back(dic[i]); break; } } for (int j = i + 1; j < (int)dic.size(); j++) { last.push_back(dic[j]); } idx = (ll)dic.size() - idx; if (K == 0) cout << (cur + 1) << endl; else cout << (last[K % idx] + 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int N; ll K; vector<bool> visited; vector<int> dic; vector<int> A; int dfs(int cur) { K--; if (visited[cur]) return cur; if (K == 0) return cur; visited[cur] = true; dic.push_back(cur); return dfs(A[cur]); } int main() { cin >> N >> K; A.resize(N); visited.resize(N); fill(visited.begin(), visited.end(), false); for (int i = 0; i < N; i++) { cin >> A[i]; A[i]--; } visited[0] = true; dic.push_back(0); int cur = dfs(A[0]); ll idx = 0; vector<int> last; int i; for (i = 0; i < (int)dic.size(); i++) { if (dic[i] == cur) { idx = i; last.push_back(dic[i]); break; } } for (int j = i + 1; j < (int)dic.size(); j++) { last.push_back(dic[j]); } idx = (ll)dic.size() - idx; if (K == 0) cout << (cur + 1) << endl; else cout << (last[K % idx] + 1) << endl; return 0; }
replace
19
20
19
20
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int nm = 1e5; int n, a, b[nm], s[nm], t, c, r; string k; int main() { cin >> n >> k; a = 0; for (int i = 0; i < n; i++) { cin >> b[i]; b[i]--; } if (k.size() <= 6) { int kInt = stoi(k); for (int i = 0; i < kInt; i++) a = b[a]; cout << a + 1 << endl; return 0; } fill(s, s + n, -1); c = a; for (int i = 0;; i++, c = b[c]) if (s[c] < 0) s[c] = i; else { r = -s[c]; t = i - s[c]; break; } for (int i = 0, b = 1; i < (int)k.size(); i++, b = b * 10 % t) r += (k[k.size() - 1 - i] - '0') * b; r %= t; if (r < 0) r += t; for (int i = 0; i < r; i++) c = b[c]; cout << c + 1 << endl; }
#include <bits/stdc++.h> using namespace std; const int nm = 3e5; int n, a, b[nm], s[nm], t, c, r; string k; int main() { cin >> n >> k; a = 0; for (int i = 0; i < n; i++) { cin >> b[i]; b[i]--; } if (k.size() <= 6) { int kInt = stoi(k); for (int i = 0; i < kInt; i++) a = b[a]; cout << a + 1 << endl; return 0; } fill(s, s + n, -1); c = a; for (int i = 0;; i++, c = b[c]) if (s[c] < 0) s[c] = i; else { r = -s[c]; t = i - s[c]; break; } for (int i = 0, b = 1; i < (int)k.size(); i++, b = b * 10 % t) r += (k[k.size() - 1 - i] - '0') * b; r %= t; if (r < 0) r += t; for (int i = 0; i < r; i++) c = b[c]; cout << c + 1 << endl; }
replace
3
4
3
4
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // long long using ll = long long; // pair<int, int> using PII = pair<int, int>; // 最大値、mod const int MOD = 1000000007; const int mod = 1000000007; const int INF = 1000000000; const long long LINF = 1e18; const int MAX = 510000; // 出力系 #define print(x) cout << x << endl #define prints(x) cout << fixed << setprecision(10) << x << endl #define printc(x) cout << setw(2) << setfill('0') << x << endl; #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl // begin() end() #define all(x) (x).begin(), (x).end() // for #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) // 最大公約数 unsigned gcd(unsigned a, unsigned b) { if (a < b) return gcd(b, a); unsigned r; while ((r = a % b)) { a = b; b = r; } return b; } // 最小公倍数 unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; } // a = max(a, b), a = min(a, b) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } // 階乗(MODをとる) ll pow_mod(ll num, ll pow, ll mod) { ll prod = 1; num %= mod; while (pow > 0) { if (pow & 1) prod = prod * num % mod; num = num * num % mod; pow >>= 1; } return prod; } // 二項係数(MODとる、1 ≦ k ≦ n ≦ 10^7 程度) // COMinit() // COM(x, y) // とコンビで使う // テーブルを作る前処理 long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } // int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}; struct UnionFind { vector<int> par; vector<int> rank; vector<ll> Size; UnionFind(int n = 1) { init(n); } void init(int n = 1) { par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1); for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; Size[x] += Size[y]; return true; } ll size(int x) { return Size[root(x)]; } }; #define rrep(i, a, b) for (int i = (a); i > (b); i--) #define rep(i, a, b) for (int i = (a); i < (b); i++) struct Mint { int val; Mint inv() const { int tmp, a = val, b = mod, x = 1, y = 0; while (b) tmp = a / b, a -= tmp * b, swap(a, b), x -= tmp * y, swap(x, y); return Mint(x); } public: Mint() : val(0) {} Mint(ll x) { if ((val = x % mod) < 0) val += mod; } Mint pow(ll t) { Mint res = 1, b = *this; while (t) { if (t & 1) res *= b; b *= b; t >>= 1; } return res; } Mint &operator+=(const Mint &x) { if ((val += x.val) >= mod) val -= mod; return *this; } Mint &operator-=(const Mint &x) { if ((val += mod - x.val) >= mod) val -= mod; return *this; } Mint &operator*=(const Mint &x) { val = (ll)val * x.val % mod; return *this; } Mint &operator/=(const Mint &x) { return *this *= x.inv(); } bool operator==(const Mint &x) const { return val == x.val; } bool operator!=(const Mint &x) const { return val != x.val; } bool operator<(const Mint &x) const { return val < x.val; } bool operator<=(const Mint &x) const { return val <= x.val; } bool operator>(const Mint &x) const { return val > x.val; } bool operator>=(const Mint &x) const { return val >= x.val; } Mint operator+(const Mint &x) const { return Mint(*this) += x; } Mint operator-(const Mint &x) const { return Mint(*this) -= x; } Mint operator*(const Mint &x) const { return Mint(*this) *= x; } Mint operator/(const Mint &x) const { return Mint(*this) /= x; } }; struct factorial { vector<Mint> Fact, Finv; public: factorial(int maxx) { Fact.resize(maxx + 1), Finv.resize(maxx + 1); Fact[0] = Mint(1); rep(i, 0, maxx) Fact[i + 1] = Fact[i] * (i + 1); Finv[maxx] = Mint(1) / Fact[maxx]; rrep(i, maxx, 0) Finv[i - 1] = Finv[i] * i; } Mint fact(int n, bool inv = 0) { if (inv) return Finv[n]; else return Fact[n]; } Mint nPr(int n, int r) { if (n < 0 || n < r || r < 0) return Mint(0); else return Fact[n] * Finv[n - r]; } Mint nCr(int n, int r) { if (n < 0 || n < r || r < 0) return Mint(0); else return Fact[n] * Finv[r] * Finv[n - r]; } }; ll modfact(ll n) { if (n <= 1) return 1; return (n * modfact(n - 1)) % MOD; } int main() { int n; cin >> n; ll k; cin >> k; int a[60][20010]; REP(i, n) { cin >> a[0][i]; a[0][i]--; } REP(i, 59) REP(j, n) { a[i + 1][j] = a[i][a[i][j]]; } int res = 0; for (int i = 59; i >= 0; i--) { ll l = 1ll << i; if (l <= k) { res = a[i][res]; k -= l; } } cout << res + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; // long long using ll = long long; // pair<int, int> using PII = pair<int, int>; // 最大値、mod const int MOD = 1000000007; const int mod = 1000000007; const int INF = 1000000000; const long long LINF = 1e18; const int MAX = 510000; // 出力系 #define print(x) cout << x << endl #define prints(x) cout << fixed << setprecision(10) << x << endl #define printc(x) cout << setw(2) << setfill('0') << x << endl; #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl // begin() end() #define all(x) (x).begin(), (x).end() // for #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) // 最大公約数 unsigned gcd(unsigned a, unsigned b) { if (a < b) return gcd(b, a); unsigned r; while ((r = a % b)) { a = b; b = r; } return b; } // 最小公倍数 unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; } // a = max(a, b), a = min(a, b) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } // 階乗(MODをとる) ll pow_mod(ll num, ll pow, ll mod) { ll prod = 1; num %= mod; while (pow > 0) { if (pow & 1) prod = prod * num % mod; num = num * num % mod; pow >>= 1; } return prod; } // 二項係数(MODとる、1 ≦ k ≦ n ≦ 10^7 程度) // COMinit() // COM(x, y) // とコンビで使う // テーブルを作る前処理 long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } // int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}; struct UnionFind { vector<int> par; vector<int> rank; vector<ll> Size; UnionFind(int n = 1) { init(n); } void init(int n = 1) { par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1); for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; Size[x] += Size[y]; return true; } ll size(int x) { return Size[root(x)]; } }; #define rrep(i, a, b) for (int i = (a); i > (b); i--) #define rep(i, a, b) for (int i = (a); i < (b); i++) struct Mint { int val; Mint inv() const { int tmp, a = val, b = mod, x = 1, y = 0; while (b) tmp = a / b, a -= tmp * b, swap(a, b), x -= tmp * y, swap(x, y); return Mint(x); } public: Mint() : val(0) {} Mint(ll x) { if ((val = x % mod) < 0) val += mod; } Mint pow(ll t) { Mint res = 1, b = *this; while (t) { if (t & 1) res *= b; b *= b; t >>= 1; } return res; } Mint &operator+=(const Mint &x) { if ((val += x.val) >= mod) val -= mod; return *this; } Mint &operator-=(const Mint &x) { if ((val += mod - x.val) >= mod) val -= mod; return *this; } Mint &operator*=(const Mint &x) { val = (ll)val * x.val % mod; return *this; } Mint &operator/=(const Mint &x) { return *this *= x.inv(); } bool operator==(const Mint &x) const { return val == x.val; } bool operator!=(const Mint &x) const { return val != x.val; } bool operator<(const Mint &x) const { return val < x.val; } bool operator<=(const Mint &x) const { return val <= x.val; } bool operator>(const Mint &x) const { return val > x.val; } bool operator>=(const Mint &x) const { return val >= x.val; } Mint operator+(const Mint &x) const { return Mint(*this) += x; } Mint operator-(const Mint &x) const { return Mint(*this) -= x; } Mint operator*(const Mint &x) const { return Mint(*this) *= x; } Mint operator/(const Mint &x) const { return Mint(*this) /= x; } }; struct factorial { vector<Mint> Fact, Finv; public: factorial(int maxx) { Fact.resize(maxx + 1), Finv.resize(maxx + 1); Fact[0] = Mint(1); rep(i, 0, maxx) Fact[i + 1] = Fact[i] * (i + 1); Finv[maxx] = Mint(1) / Fact[maxx]; rrep(i, maxx, 0) Finv[i - 1] = Finv[i] * i; } Mint fact(int n, bool inv = 0) { if (inv) return Finv[n]; else return Fact[n]; } Mint nPr(int n, int r) { if (n < 0 || n < r || r < 0) return Mint(0); else return Fact[n] * Finv[n - r]; } Mint nCr(int n, int r) { if (n < 0 || n < r || r < 0) return Mint(0); else return Fact[n] * Finv[r] * Finv[n - r]; } }; ll modfact(ll n) { if (n <= 1) return 1; return (n * modfact(n - 1)) % MOD; } int main() { int n; cin >> n; ll k; cin >> k; int a[61][200010]; REP(i, n) { cin >> a[0][i]; a[0][i]--; } REP(i, 59) REP(j, n) { a[i + 1][j] = a[i][a[i][j]]; } int res = 0; for (int i = 59; i >= 0; i--) { ll l = 1ll << i; if (l <= k) { res = a[i][res]; k -= l; } } cout << res + 1 << endl; return 0; }
replace
241
242
241
242
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; void solve() { ll n, k; cin >> n >> k; vector<int> v(n + 5), vis(n + 5, -1); for (int i = 1; i <= n; i++) cin >> v[i]; int i, ans = 1, cl = 0; vis[1] = 0; for (i = 1; i <= k; i++) { ans = v[ans]; if (vis[ans] != -1) { cl = i - vis[ans]; break; } else { vis[ans] = i; } } // cout << ans << " " << i << " " << cl << '\n'; // starting at ans move k - i k = (k - i) % cl; while (k--) ans = v[ans]; cout << ans << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; void solve() { ll n, k; cin >> n >> k; vector<int> v(n + 5), vis(n + 5, -1); for (int i = 1; i <= n; i++) cin >> v[i]; int i, ans = 1, cl = 0; vis[1] = 0; for (i = 1; i <= k; i++) { ans = v[ans]; if (vis[ans] != -1) { cl = i - vis[ans]; break; } else { vis[ans] = i; } } // cout << ans << " " << i << " " << cl << '\n'; // starting at ans move k - i if (cl) { k = (k - i) % cl; while (k--) ans = v[ans]; } cout << ans << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
replace
24
27
24
29
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int D = 110; const int N = 2000000; int to[D][N]; int main() { int n; ll k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> to[0][i]; } rep(i, D) { for (int j = 1; j <= n; j++) { to[i + 1][j] = to[i][to[i][j]]; } } int now_town = 1; int tmp = -1; do { tmp++; if (k % 2 == 1) { now_town = to[tmp][now_town]; } k /= 2; } while (k != 0); cout << now_town << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int D = 110; const int N = 2000000; int to[D][N]; int main() { int n; ll k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> to[0][i]; } rep(i, D - 1) { for (int j = 1; j <= n; j++) { to[i + 1][j] = to[i][to[i][j]]; } } int now_town = 1; int tmp = -1; do { tmp++; if (k % 2 == 1) { now_town = to[tmp][now_town]; } k /= 2; } while (k != 0); cout << now_town << endl; }
replace
15
16
15
16
-11
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define MOD 1000000007 #define MOD2 998244353 #define INF ((1 << 30) - 1) #define LINF (1LL << 60) #define EPS (1e-10) typedef long long Int; typedef pair<Int, Int> P; int to[110000]; bool reached[110000]; int cnt[110000]; int main() { int n; Int k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> to[i]; } int now = 1; reached[1] = true; Int sumcnt = 0; int period = INF; while (true) { int nex = to[now]; sumcnt++; if (sumcnt == k) { cout << nex << endl; return 0; } if (!reached[nex]) { reached[nex] = true; cnt[nex] = sumcnt; } else { period = sumcnt - cnt[nex]; now = nex; break; } now = nex; } k -= sumcnt; k %= period; rep(i, k) { int nex = to[now]; now = nex; } cout << now << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define MOD 1000000007 #define MOD2 998244353 #define INF ((1 << 30) - 1) #define LINF (1LL << 60) #define EPS (1e-10) typedef long long Int; typedef pair<Int, Int> P; int to[220000]; bool reached[220000]; int cnt[220000]; int main() { int n; Int k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> to[i]; } int now = 1; reached[1] = true; Int sumcnt = 0; int period = INF; while (true) { int nex = to[now]; sumcnt++; if (sumcnt == k) { cout << nex << endl; return 0; } if (!reached[nex]) { reached[nex] = true; cnt[nex] = sumcnt; } else { period = sumcnt - cnt[nex]; now = nex; break; } now = nex; } k -= sumcnt; k %= period; rep(i, k) { int nex = to[now]; now = nex; } cout << now << endl; }
replace
11
14
11
14
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pi; typedef vector<int> vi; typedef vector<vi> vvi; const int mod = 1e9 + 7; const int inf = 2e9 + 5; const int N = 1e5 + 5; ll n, k, a[N]; bool visited[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; int ptr = 1, c = 0, period = -1, ptr1; vi cycle(n + 1); while (true) { if (visited[ptr]) { period = c - cycle[ptr]; ptr1 = ptr; break; } cycle[ptr] = c; visited[ptr] = true; ptr = a[ptr]; c += 1; } if (k < c) { for (int i = 1; i <= n; i++) { if (cycle[i] == k) { cout << i << "\n"; return 0; } } } k -= c; k %= period; ptr = ptr1; c = 0; while (true) { if (c == k) { cout << ptr << "\n"; return 0; } ptr = a[ptr]; c += 1; } return 0; }
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pi; typedef vector<int> vi; typedef vector<vi> vvi; const int mod = 1e9 + 7; const int inf = 2e9 + 5; const int N = 2e5 + 5; ll n, k, a[N]; bool visited[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; int ptr = 1, c = 0, period = -1, ptr1; vi cycle(n + 1); while (true) { if (visited[ptr]) { period = c - cycle[ptr]; ptr1 = ptr; break; } cycle[ptr] = c; visited[ptr] = true; ptr = a[ptr]; c += 1; } if (k < c) { for (int i = 1; i <= n; i++) { if (cycle[i] == k) { cout << i << "\n"; return 0; } } } k -= c; k %= period; ptr = ptr1; c = 0; while (true) { if (c == k) { cout << ptr << "\n"; return 0; } ptr = a[ptr]; c += 1; } return 0; }
replace
12
13
12
13
0
p02684
C++
Runtime Error
#include <iostream> #include <string> #include <vector> using namespace std; int main() { ios_base ::sync_with_stdio(false); int n = 0; long long k = 0; cin >> n >> k; vector<int> p(n + 2, 0); for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; } vector<int> used(n + 2, -1); used[0] = 0; int cp = 0; int ct = 0; while (k > 0) { k--; ct++; cp = p[cp]; if (used[cp] != -1) { break; } used[cp] = ct; } k %= (ct - used[cp]); while (k > 0) { k--; cp = p[cp]; } cout << cp + 1; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int main() { ios_base ::sync_with_stdio(false); int n = 0; long long k = 0; cin >> n >> k; vector<int> p(n + 2, 0); for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; } vector<int> used(n + 2, -1); used[0] = 0; int cp = 0; int ct = 0; while (k > 0) { k--; ct++; cp = p[cp]; if (used[cp] != -1) { break; } used[cp] = ct; } if (k != 0) k %= (ct - used[cp]); while (k > 0) { k--; cp = p[cp]; } cout << cp + 1; return 0; }
replace
29
30
29
31
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[60][200005]; int main() { long long n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> dp[0][i]; dp[0][i]--; } for (int i = 0; i < 60; i++) { for (int j = 0; j < n; j++) { dp[i + 1][j] = dp[i][dp[i][j]]; } } long long ans = 0; for (int i = 59; i >= 0; i--) { long long l = 1ll << i; if (l <= k) { k -= l; ans = dp[i][ans]; } } cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long dp[60][200005]; int main() { long long n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> dp[0][i]; dp[0][i]--; } for (int i = 0; i < 59; i++) { for (int j = 0; j < n; j++) { dp[i + 1][j] = dp[i][dp[i][j]]; } } long long ans = 0; for (int i = 59; i >= 0; i--) { long long l = 1ll << i; if (l <= k) { k -= l; ans = dp[i][ans]; } } cout << ans + 1 << endl; return 0; }
replace
10
11
10
11
0
p02684
Python
Runtime Error
n, k = list(map(int, input().split())) a_list = list(map(int, input().split())) START = 1 # 訪れたタイミング town_with_i = {START: 0} # 現在地 current = START # 既に訪れたことある街(巡回の判定に使う) known_towns = set([START]) i = 0 for _ in range(k): i += 1 # 現在地 current = a_list[current - 1] # 既に来たことがあるということは巡回している if current in known_towns: break # メモ town_with_i[current] = i known_towns.add(current) # 巡回までのステップ数 first_step = town_with_i[current] # 1周するのに必要なステップ数 r = i - town_with_i[current] # print(town_with_i) # print(current) # print(i) # print(r) loop_num = (k - first_step) % r for i in range(loop_num): current = a_list[current - 1] print(current)
n, k = list(map(int, input().split())) a_list = list(map(int, input().split())) START = 1 # 訪れたタイミング town_with_i = {START: 0} # 現在地 current = START # 既に訪れたことある街(巡回の判定に使う) known_towns = set([START]) i = 0 for _ in range(k): i += 1 # 現在地 current = a_list[current - 1] # 既に来たことがあるということは巡回している if current in known_towns: break # メモ town_with_i[current] = i known_towns.add(current) # 巡回までのステップ数 first_step = town_with_i[current] # 1周するのに必要なステップ数 r = i - town_with_i[current] # print(town_with_i) # print(current) # print(i) # print(r) loop_num = (k - first_step) % r if r > 0 else (k - first_step) for i in range(loop_num): current = a_list[current - 1] print(current)
replace
34
35
34
35
0
p02684
Python
Runtime Error
n, k = map(int, input().split()) a = list(map(int, input().split())) rec = [0] i = 0 flag = [0 for _ in range(n + 1)] while True: if flag[rec[i]] == 0: flag[rec[i]] = 1 rec.append(a[rec[i]] - 1) else: start = rec.index(a[rec[i]] - 1) loop = rec[start:] pre = rec[:start] break i += 1 count = (k - len(pre)) % len(loop) if k < len(pre): print(loop[k]) else: print(loop[count] + 1)
n, k = map(int, input().split()) a = list(map(int, input().split())) rec = [0] i = 0 flag = [0 for _ in range(n + 1)] while True: if flag[rec[i]] == 0: flag[rec[i]] = 1 rec.append(a[rec[i]] - 1) else: start = rec.index(a[rec[i]] - 1) loop = rec[start:] pre = rec[:start] break i += 1 count = (k - len(pre)) % len(loop) if k <= len(pre): print(pre[k] + 1) else: print(loop[count] + 1)
replace
16
18
16
18
0
p02684
Python
Time Limit Exceeded
#!/usr/bin/env python3 import numpy as np # def input(): # return sys.stdin.readline().rstrip() def main(): n, k = map(int, input().split()) warps = list(map(int, input().split())) warps = [0] + warps warps = np.array(warps, dtype=int) dp = np.zeros((k.bit_length() + 1, n + 1), dtype=int) dp[0, :] = warps for h in range(1, len(dp)): for j in range(len(dp[0] + 1)): dp[h][j] = dp[h - 1][dp[h - 1][j]] node = 1 # for i in reversed(range(k.bit_length())): for i in range(k.bit_length(), -1, -1): if k >> i & 1: node = dp[i][node] print(node) main()
#!/usr/bin/env python3 import numpy as np # def input(): # return sys.stdin.readline().rstrip() def main(): n, k = map(int, input().split()) warps = list(map(int, input().split())) warps = [0] + warps warps = np.array(warps, dtype=int) dp = np.zeros((k.bit_length() + 1, n + 1), dtype=int) dp[0, :] = warps for h in range(1, len(dp)): # dp[h] = dp[h - 1][dp[h - 1]] dp[h] = np.take(dp[h - 1], dp[h - 1]) node = 1 # for i in reversed(range(k.bit_length())): for i in range(k.bit_length(), -1, -1): if k >> i & 1: node = dp[i][node] print(node) main()
replace
21
23
21
23
TLE
p02684
Python
Runtime Error
import sys sys.setrecursionlimit(10**7) f_inf = float("inf") mod = 10**9 + 7 def resolve(): n, k = map(int, input().split()) A = list(map(int, input().split())) tree = [[] for _ in range(n)] for idx, a in enumerate(A): tree[idx].append(a - 1) start = [] depth = [0 for _ in range(n)] def dfs(v, p, d, cnt): depth[v] = d for u in tree[v]: if u == p: continue if visited[u]: start.append([u + 1, depth[u], cnt + 1]) return else: visited[u] = True dfs(u, v, d + 1, cnt + 1) return visited = [False for _ in range(n)] visited[0] = True dfs(0, -1, 0, 0) if k <= start[0][1]: print(depth.index(k) + 1) else: cnt = (k - start[0][1]) % (start[0][2] - start[0][1]) res = start[0][1] + cnt print(depth.index(res) + 1) if __name__ == "__main__": resolve()
import sys sys.setrecursionlimit(10**7) f_inf = float("inf") mod = 10**9 + 7 def resolve(): n, k = map(int, input().split()) A = list(map(int, input().split())) tree = [[] for _ in range(n)] for idx, a in enumerate(A): tree[idx].append(a - 1) start = [] depth = [0 for _ in range(n)] def dfs(v, p, d, cnt): depth[v] = d for u in tree[v]: if visited[u]: start.append([u + 1, depth[u], cnt + 1]) return else: visited[u] = True dfs(u, v, d + 1, cnt + 1) return visited = [False for _ in range(n)] visited[0] = True dfs(0, -1, 0, 0) if k <= start[0][1]: print(depth.index(k) + 1) else: cnt = (k - start[0][1]) % (start[0][2] - start[0][1]) res = start[0][1] + cnt print(depth.index(res) + 1) if __name__ == "__main__": resolve()
delete
21
24
21
21
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define mod 1000000009 #define ll long long using namespace std; long long int n, num, a[20005], vis[20005]; int main() { scanf("%lld%lld", &n, &num); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), vis[i] = 0; long long int pos1 = 1, pos2; while (vis[pos1] != 1) { vis[pos1] = 1; pos1 = a[pos1]; } pos2 = pos1; for (int j = 1; j <= n; j++) vis[j] = 0; int t = 0; while (vis[pos2] != 1) { vis[pos2] = 1; t++; pos2 = a[pos2]; } int b = 0; pos2 = 1; while (1) { if (pos1 == pos2) break; pos2 = a[pos2], b++; } int c = 0; if (num <= b) { pos2 = 1; while (1) { if (num == c) break; pos2 = a[pos2], c++; } printf("%d\n", pos2); return 0; } num -= b; num %= t; pos2 = pos1; c = 0; while (1) { if (num == c) break; pos2 = a[pos2], c++; } printf("%d\n", pos2); return 0; }
#include <bits/stdc++.h> #define mod 1000000009 #define ll long long using namespace std; long long int n, num, a[200005], vis[200005]; int main() { scanf("%lld%lld", &n, &num); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), vis[i] = 0; long long int pos1 = 1, pos2; while (vis[pos1] != 1) { vis[pos1] = 1; pos1 = a[pos1]; } pos2 = pos1; for (int j = 1; j <= n; j++) vis[j] = 0; int t = 0; while (vis[pos2] != 1) { vis[pos2] = 1; t++; pos2 = a[pos2]; } int b = 0; pos2 = 1; while (1) { if (pos1 == pos2) break; pos2 = a[pos2], b++; } int c = 0; if (num <= b) { pos2 = 1; while (1) { if (num == c) break; pos2 = a[pos2], c++; } printf("%d\n", pos2); return 0; } num -= b; num %= t; pos2 = pos1; c = 0; while (1) { if (num == c) break; pos2 = a[pos2], c++; } printf("%d\n", pos2); return 0; }
replace
4
5
4
5
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; #define chmax(x, y) x = max(x, y); const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, -1, 0, 1}; const int INF = 1001001001; int main() { int n; ll k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; vector<int> s; vector<int> ord(n + 1, -1); int c = 0, l = 0; int v = 1; while (ord[v] != -1) { ord[v] = s.size(); s.push_back(v); v = a[v - 1]; } c = s.size() - ord[v]; l = ord[v]; if (k < l) cout << s[k] << endl; else { k -= l; k %= c; cout << s[l + k] << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; #define chmax(x, y) x = max(x, y); const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, -1, 0, 1}; const int INF = 1001001001; int main() { int n; ll k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; vector<int> s; vector<int> ord(n + 1, -1); int c = 0, l = 0; int v = 1; while (ord[v] == -1) { ord[v] = s.size(); s.push_back(v); v = a[v - 1]; } c = s.size() - ord[v]; l = ord[v]; if (k < l) cout << s[k] << endl; else { k -= l; k %= c; cout << s[l + k] << endl; } return 0; }
replace
22
23
22
23
-11
p02684
C++
Runtime Error
/******************************** *** ******** ********** ********* *** * ****** ********* * ******** *** ** ***** ******** *** ******* *** *** **** ******* ***** ****** *** **** *** ****** ******* ***** *** ***** ** ***** +++++++++ **** *** ****** * **** *********** *** *** ******* *** ************* ** *** ******** ** *************** * **********_********************** * | / / \ \ / /\ * * | / | | \ / / \ * * | / | | \ / / \ * * |+\ | | | /______\ * * | \ | | | | | * * | \ \_/ | | | * ********************************/ #include <bits/stdc++.h> #define lli long long int #define MOD 1000000007 #define KING 1000000002 #define queen 1000002 #define fi first #define se second #define se second #define lsb(i) (i & -i) #define nl cout << '\n' #define TEST cout << "TEST", nl #define pr pair<lli, lli> #define pb push_back #define eb emplace_back #define mp make_pair #define vi vector<lli> #define vpi vector<pr> #define P pair<lli, pr> #define fastIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define NA(i, s, n) for (lli i = s; i < n; i++) using namespace std; void solve() { lli n, k, i = 1; cin >> n >> k; vi a(n + 1), vis(n + 1, 0); NA(i, 1, n + 1) cin >> a[i]; while (k) { if (!vis[a[i]]) vis[a[i]] = 1; else break; i = a[i]; --k; } lli j = 0, p = i; unordered_map<lli, lli> mpp; while (k) { mpp[j] = i; i = a[i]; ++j; if (i == p) break; } cout << mpp[k % j]; return; } int main() { fastIO; lli t = 1; // cin>>t; NA(i, 0, t) solve(); return 0; }
/******************************** *** ******** ********** ********* *** * ****** ********* * ******** *** ** ***** ******** *** ******* *** *** **** ******* ***** ****** *** **** *** ****** ******* ***** *** ***** ** ***** +++++++++ **** *** ****** * **** *********** *** *** ******* *** ************* ** *** ******** ** *************** * **********_********************** * | / / \ \ / /\ * * | / | | \ / / \ * * | / | | \ / / \ * * |+\ | | | /______\ * * | \ | | | | | * * | \ \_/ | | | * ********************************/ #include <bits/stdc++.h> #define lli long long int #define MOD 1000000007 #define KING 1000000002 #define queen 1000002 #define fi first #define se second #define se second #define lsb(i) (i & -i) #define nl cout << '\n' #define TEST cout << "TEST", nl #define pr pair<lli, lli> #define pb push_back #define eb emplace_back #define mp make_pair #define vi vector<lli> #define vpi vector<pr> #define P pair<lli, pr> #define fastIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define NA(i, s, n) for (lli i = s; i < n; i++) using namespace std; void solve() { lli n, k, i = 1; cin >> n >> k; vi a(n + 1), vis(n + 1, 0); NA(i, 1, n + 1) cin >> a[i]; while (k) { if (!vis[a[i]]) vis[a[i]] = 1; else break; i = a[i]; --k; } if (!k) { cout << i; return; } lli j = 0, p = i; unordered_map<lli, lli> mpp; while (k) { mpp[j] = i; i = a[i]; ++j; if (i == p) break; } cout << mpp[k % j]; return; } int main() { fastIO; lli t = 1; // cin>>t; NA(i, 0, t) solve(); return 0; }
insert
55
55
55
59
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int cnt = 1, tmp, aa; vector<int> check(n, 0); check[0] = 1; for (long long i = 1; i <= k; i++) { cnt = a[cnt - 1]; if (check[cnt - 1] == 0) check[cnt - 1] = i + 1; else { tmp = i + 1 - check[cnt - 1]; aa = i - tmp; // cout<<tmp<<' '<<cnt<<' '<<aa<<endl; break; } } tmp = (k - aa) % tmp; for (int i = 0; i < tmp; i++) { cnt = a[cnt - 1]; } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int cnt = 1, tmp = k, aa = 0; vector<int> check(n, 0); check[0] = 1; for (long long i = 1; i <= k; i++) { cnt = a[cnt - 1]; if (check[cnt - 1] == 0) check[cnt - 1] = i + 1; else { tmp = i + 1 - check[cnt - 1]; aa = i - tmp; // cout<<tmp<<' '<<cnt<<' '<<aa<<endl; break; } } tmp = (k - aa) % tmp; for (int i = 0; i < tmp; i++) { cnt = a[cnt - 1]; } cout << cnt << endl; }
replace
10
11
10
11
0
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define my_max(a, b) ((a) > (b) ? (a) : (b)) #define my_min(a, b) ((a) < (b) ? (a) : (b)) #define fi first #define se second #define pb push_back #define eb emplace_back #define rep(i, s, t) for (int i = (int)(s); i <= (int)(t); i++) #define rev(i, t, s) for (int i = (int)(t); i >= (int)(s); i--) #define lson rt << 1 #define rson rt << 1 | 1 #define sz(x) (int)(x).size() typedef long long ll; typedef long double lb; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> VI; typedef std::vector<ll> VL; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree; ll gcd(ll x, ll y) { return y % x == 0 ? x : gcd(y % x, x); } template <class T> T my_abs(T a) { if (a < 0) a = -a; return a; } inline ll read() { ll ret = 0, sign = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') sign = -1; c = getchar(); } while (c >= '0' && c <= '9') { ret = ret * 10 + c - '0'; c = getchar(); } return ret * sign; } inline void write(ll x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } // void expand_stack_space(){ // int size = 256 << 20; // 256MB // char *p = (char*)malloc(size) + size; // __asm__("movl %0, %%esp\n" :: "r"(p)); // } const int maxn = 2e5 + 7; int a[maxn], n, vis[maxn], st[maxn], top; ll k; int dfs(int u) { if (k == 0) return u; if (vis[u]) { int cycsz = 0, j; for (j = top; j > 0; --j) if (st[j] == u) break; cycsz = top - j + 1; k %= cycsz; return st[j + k]; } st[++top] = u; vis[u] = 1; --k; dfs(a[u]); } int main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif // ONLINE_JUDGE scanf("%d%lld", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } printf("%d\n", dfs(1)); return 0; }
#include <bits/stdc++.h> #define my_max(a, b) ((a) > (b) ? (a) : (b)) #define my_min(a, b) ((a) < (b) ? (a) : (b)) #define fi first #define se second #define pb push_back #define eb emplace_back #define rep(i, s, t) for (int i = (int)(s); i <= (int)(t); i++) #define rev(i, t, s) for (int i = (int)(t); i >= (int)(s); i--) #define lson rt << 1 #define rson rt << 1 | 1 #define sz(x) (int)(x).size() typedef long long ll; typedef long double lb; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> VI; typedef std::vector<ll> VL; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree; ll gcd(ll x, ll y) { return y % x == 0 ? x : gcd(y % x, x); } template <class T> T my_abs(T a) { if (a < 0) a = -a; return a; } inline ll read() { ll ret = 0, sign = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') sign = -1; c = getchar(); } while (c >= '0' && c <= '9') { ret = ret * 10 + c - '0'; c = getchar(); } return ret * sign; } inline void write(ll x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } // void expand_stack_space(){ // int size = 256 << 20; // 256MB // char *p = (char*)malloc(size) + size; // __asm__("movl %0, %%esp\n" :: "r"(p)); // } const int maxn = 2e5 + 7; int a[maxn], n, vis[maxn], st[maxn], top; ll k; int dfs(int u) { if (k == 0) return u; if (vis[u]) { int cycsz = 0, j; for (j = top; j > 0; --j) if (st[j] == u) break; cycsz = top - j + 1; k %= cycsz; return st[j + k]; } st[++top] = u; vis[u] = 1; --k; return dfs(a[u]); } int main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif // ONLINE_JUDGE scanf("%d%lld", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } printf("%d\n", dfs(1)); return 0; }
replace
80
81
80
81
0
p02684
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n; long long int k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } int b[n + 1]; vector<int> c; int d = 0; for (int i = 0; i < n + 3; i++) { b[d]++; c.push_back(d); if (b[d] == 2) break; d = a[d]; } int p; p = c.size(); int q = 0; for (int i = 0; i < p; i++) { if (c[i] == c[p - 1]) { q = i; break; } } if (k < p) cout << c[k] + 1 << endl; else cout << c[q + (k - p + 1) % (p - q - 1)] + 1 << endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n; long long int k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } int b[n + 1] = {}; vector<int> c; int d = 0; for (int i = 0; i < n + 3; i++) { b[d]++; c.push_back(d); if (b[d] == 2) break; d = a[d]; } int p; p = c.size(); int q = 0; for (int i = 0; i < p; i++) { if (c[i] == c[p - 1]) { q = i; break; } } if (k < p) cout << c[k] + 1 << endl; else cout << c[q + (k - p + 1) % (p - q - 1)] + 1 << endl; }
replace
19
20
19
20
0
p02684
C++
Time Limit Exceeded
/* * ~ AtCoder Botay ~ * code by kazalika */ #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; long long k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; a[i]--; } if (a[0] == 0) { cout << 1; return 0; } vector<long long> timer(n); long long nn = 0, t = 0; while (timer[nn] < k || !timer[nn]) { timer[nn] = t; if (timer[nn] == k || a[nn] == nn) { cout << nn + 1; return 0; } nn = a[nn]; t++; } k -= timer[nn]; assert(k >= 0); if (!k || a[nn] == nn) { cout << nn + 1; return 0; } long long st = nn, cn = 1; nn = a[nn]; vector<long long> v = {st}; while (st != nn) { v.push_back(nn); nn = a[nn]; cn++; } assert(v.size() == cn); cout << v[k % cn] + 1; }
/* * ~ AtCoder Botay ~ * code by kazalika */ #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; long long k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; a[i]--; } if (a[0] == 0) { cout << 1; return 0; } vector<long long> timer(n); long long nn = 0, t = 0; while (!timer[nn]) { timer[nn] = t; if (timer[nn] == k || a[nn] == nn) { cout << nn + 1; return 0; } nn = a[nn]; t++; } k -= timer[nn]; assert(k >= 0); if (!k || a[nn] == nn) { cout << nn + 1; return 0; } long long st = nn, cn = 1; nn = a[nn]; vector<long long> v = {st}; while (st != nn) { v.push_back(nn); nn = a[nn]; cn++; } assert(v.size() == cn); cout << v[k % cn] + 1; }
replace
27
28
27
28
TLE
p02684
C++
Runtime Error
#include <bits/stdc++.h> #define ll unsigned long long using namespace std; const int maxn = 1e5 + 10; ll arr[maxn], vis[maxn]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ll n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> arr[i]; int acout = 0, cur = 1; vector<int> v; while (1) { if (vis[cur]) break; else { acout++, vis[cur] = 1; v.push_back(cur); cur = arr[cur]; } } // for(int i=0;i<v.size();i++) cout<<v[i]<<" "; // cout<<"debug "<<cur<<endl; int cal = 0, i = 0; if (v.size() > k) { cout << v[k] << endl; return 0; } while (1) { if (v[i] != cur) cal++, acout--, v.erase(v.begin()); else break; } if (cal > 0) k -= cal; // for(int i=0;i<v.size();i++) cout<<v[i]<<" "; // cout<<endl; if (acout == 0) { cout << v[v.size() - 1] << endl; return 0; } cout << v[k % acout] << endl; return 0; }
#include <bits/stdc++.h> #define ll unsigned long long using namespace std; const int maxn = 2e5 + 10; ll arr[maxn], vis[maxn]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ll n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> arr[i]; int acout = 0, cur = 1; vector<int> v; while (1) { if (vis[cur]) break; else { acout++, vis[cur] = 1; v.push_back(cur); cur = arr[cur]; } } // for(int i=0;i<v.size();i++) cout<<v[i]<<" "; // cout<<"debug "<<cur<<endl; int cal = 0, i = 0; if (v.size() > k) { cout << v[k] << endl; return 0; } while (1) { if (v[i] != cur) cal++, acout--, v.erase(v.begin()); else break; } if (cal > 0) k -= cal; // for(int i=0;i<v.size();i++) cout<<v[i]<<" "; // cout<<endl; if (acout == 0) { cout << v[v.size() - 1] << endl; return 0; } cout << v[k % acout] << endl; return 0; }
replace
3
4
3
4
0