#include <bits/stdc++.h>
using namespace std;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
#define pii pair<int,int>
void solve() {
int n,k;
cin >> n >> k;
vector<vi> a(n+1,vi(n+1));
F(i,n) F(j,n) cin >> a[i][j];
if (k == 1) {
cout << 1 << endl;
return;
}
int up[n*n+1][62];
memset(up,0,sizeof up);
F(i,n) {
F(j,n) {
if (i == j) continue;
int idx = (i-1)*n+j;
up[idx][0] = (a[i][j]-1)*n+i;
}
}
for (int i=1;i<62;i++) {
for (int j=1;j<=n*n;j++) {
up[j][i] = up[up[j][i-1]][i-1];
}
}
int ans = n+1;
for (int i=0;i<62;i++) {
if ((k-2)&(1LL<<i)) ans = up[ans][i];
}
cout << (ans+n-1)/n << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int t = 1;
//cin >> t;
F(i,t) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
27 ms |
23824 KB |
Output is correct |
3 |
Correct |
112 ms |
78744 KB |
Output is correct |
4 |
Correct |
152 ms |
100944 KB |
Output is correct |
5 |
Correct |
208 ms |
124660 KB |
Output is correct |
6 |
Correct |
209 ms |
124652 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
9312 KB |
Output is correct |
9 |
Correct |
28 ms |
26712 KB |
Output is correct |
10 |
Correct |
167 ms |
100432 KB |
Output is correct |
11 |
Correct |
211 ms |
123732 KB |
Output is correct |
12 |
Correct |
239 ms |
123832 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
8 ms |
9308 KB |
Output is correct |