#include <bits/stdc++.h>
#include <time.h>
//#include <random>
//#ifndef _LOCAL
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#endif
#define sz(x) int(x.size())
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define N 3 * 100500
#define oo ll(1e16)
#define pii pair <int, int>
#define pll pair <ll, ll>
#define ft first
#define sd second
#define pb push_back
#define ppb pop_back
#define el '\n'
#define elf endl
#define base ll(1e9 + 7)
#define re return
#define nins 4294967295
using namespace std;
typedef long long ll;
typedef long double ld;
//mt19937 rnd(0);
ll n, g[1000][1000], was[1000][1000], rs[N];
ll k;
void solve() {
cin >> n >> k;
for (ll i = 0; i < n; i++)
for (ll j = 0; j < n; j++)
cin >> g[i][j], g[i][j]--;
for (ll i = 0; i < n; i++)
for (ll j = 0; j < n; j++)
was[i][j] = -1;
// for (ll i = 0; i <= k; i++)
// rs[i] = 0;
ll lst = 0, cr = 1;
rs[1] = 0;
rs[2] = 1;
was[lst][cr] = 2;
for (ll i = 3; i <= k; i++) {
ll nxt = g[cr][lst];
if (was[cr][nxt] == -1) {
was[cr][nxt] = i;
rs[i] = nxt;
lst = cr;
cr = nxt;
continue;
}
ll len = i - was[cr][nxt];
k -= was[cr][nxt];
ll ps = was[cr][nxt] + (k % len);
cout << rs[ps] + 1;
return;
}
cout << rs[k] + 1;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef _LOCAL
in("input.txt");
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
cout << "Test #" << i << elf;
clock_t start_time = clock();
solve();
cerr.precision(4); cerr << fixed;
cerr << elf;
cerr << "Time :: " << ld(clock() - start_time) / CLOCKS_PER_SEC << elf;
cout << elf;
}
#else
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << el;
}
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
9 ms |
2816 KB |
Output is correct |
3 |
Correct |
20 ms |
6008 KB |
Output is correct |
4 |
Correct |
22 ms |
7168 KB |
Output is correct |
5 |
Correct |
28 ms |
8192 KB |
Output is correct |
6 |
Correct |
24 ms |
8192 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
1664 KB |
Output is correct |
9 |
Correct |
9 ms |
3072 KB |
Output is correct |
10 |
Correct |
22 ms |
7168 KB |
Output is correct |
11 |
Correct |
25 ms |
8192 KB |
Output is correct |
12 |
Correct |
25 ms |
8184 KB |
Output is correct |
13 |
Correct |
5 ms |
768 KB |
Output is correct |
14 |
Correct |
8 ms |
1920 KB |
Output is correct |