#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 505;
int dir[mxn][mxn];
int idx[mxn][mxn];
ll n,k;
int C;
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
if(k == 1){
cout<<1;
return 0;
}
else if(k == 2){
cout<<2;
return 0;
}
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++)cin>>dir[i][j];
}
pii now = {2,1};
k -= 2;
do{
k--;
idx[now.fs][now.sc] = ++C;
int nxt = dir[now.fs][now.sc];
now = {nxt,now.fs};
if(!k){
cout<<now.fs<<'\n';
return 0;
}
}while(!idx[now.fs][now.sc]);
ll sz = C+1-idx[now.fs][now.sc];
k %= sz;
memset(idx,0,sizeof(idx));
while(k){
k--;
int nxt = dir[now.fs][now.sc];
now = {nxt,now.fs};
}
cout<<now.fs;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1884 KB |
Output is correct |
3 |
Correct |
9 ms |
2660 KB |
Output is correct |
4 |
Correct |
11 ms |
2908 KB |
Output is correct |
5 |
Correct |
13 ms |
3164 KB |
Output is correct |
6 |
Correct |
13 ms |
3252 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
1784 KB |
Output is correct |
9 |
Correct |
4 ms |
1908 KB |
Output is correct |
10 |
Correct |
11 ms |
2904 KB |
Output is correct |
11 |
Correct |
15 ms |
3164 KB |
Output is correct |
12 |
Correct |
14 ms |
3128 KB |
Output is correct |
13 |
Correct |
1 ms |
1368 KB |
Output is correct |
14 |
Correct |
2 ms |
1880 KB |
Output is correct |