#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define int long long
using namespace std;
typedef tuple<int, int, int> iii;
typedef long long ll;
const int mod =998244353;
int n, k;
int a[505][505], vis[505][505];
vector<int> v, vv;
int mOd(int x, int y){
if(x%y)return x%y;
else return y;
}
int32_t main(){
fast;
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<<endl;
return 0;
}
else k--;
int i=2, j=1, ind=0, cnt;
while(true){
if(vis[i][j]){
//~ cout<<i<<" :: "<<j<<endl;
for(int k=vis[i][j];k<=(int)v.size()-1;k++){
vv.pb(v[k]);
}
cnt=vis[i][j];
break;
}
vis[i][j]=ind;
v.pb(i);
ind++;
int t=i;
i=a[i][j];
j=t;
}
//~ cout<<v.size()<<" :: "<<vv.size()<<" :: "<<cnt<<endl;
//~ for(auto it: v){
//~ cout<<it<<" v ";
//~ }
//~ cout<<endl;
//~ for(auto it: vv){
//~ cout<<it<<" vv ";
//~ }
//~ cout<<endl;
if(k<=cnt){
cout<<v[k-1]<<endl;
}
else{
k-=cnt;
cout<<vv[mOd(k, (int)vv.size())-1]<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
3 ms |
3416 KB |
Output is correct |
3 |
Correct |
8 ms |
4444 KB |
Output is correct |
4 |
Correct |
11 ms |
4728 KB |
Output is correct |
5 |
Correct |
13 ms |
5368 KB |
Output is correct |
6 |
Correct |
16 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2780 KB |
Output is correct |
9 |
Correct |
3 ms |
3420 KB |
Output is correct |
10 |
Correct |
10 ms |
4700 KB |
Output is correct |
11 |
Correct |
13 ms |
5212 KB |
Output is correct |
12 |
Correct |
13 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |