#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define MOD 1000000007
typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
const ll INF=63;
const int mxn=505;
bool DEBUG=0;
int n,a[mxn][mxn],vis[mxn][mxn];
ll k;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
cin>>n>>k;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cin>>a[i][j];
}
}
if(k==1){
cout<<1<<"\n";
return 0;
}
if(k==2){
cout<<2<<"\n";
return 0;
}
int ptr=3,now=2,prev=1;
vi list;
while(!vis[now][prev]){
//cerr<<ptr<<" "<<now<<" "<<prev<<"\n";
vis[now][prev]=ptr;
int pr = now;
now=a[now][prev];
prev = pr;
if(ptr==k){
cout<<now<<"\n";
return 0;
}
ptr++;
list.eb(now);
}
k-=vis[now][prev];
ll siz = 1LL*(ptr-vis[now][prev]);
k=k%siz;
cout<<list[k+vis[now][prev]-1-2];
return 0;
}
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
1408 KB |
Output is correct |
3 |
Correct |
14 ms |
2176 KB |
Output is correct |
4 |
Correct |
16 ms |
2304 KB |
Output is correct |
5 |
Correct |
20 ms |
2560 KB |
Output is correct |
6 |
Correct |
19 ms |
2304 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
896 KB |
Output is correct |
9 |
Correct |
5 ms |
1408 KB |
Output is correct |
10 |
Correct |
17 ms |
2176 KB |
Output is correct |
11 |
Correct |
20 ms |
2552 KB |
Output is correct |
12 |
Correct |
20 ms |
2432 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
896 KB |
Output is correct |