#include <bits/stdc++.h>
#define ll long long
#define REP(i,n) for(int i=0; i<n; i++)
#define ld long double
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
using namespace std;
long long N, K;
int Mat[1000][1000];
map< pii, int> MAP;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> K;
REP(i, N)
REP(j, N)
cin >> Mat[i][j];
if(K <= 2){
cout << K;
return 0;
}
K-= 2;
pii par = mp(1, 2);
//cout << "PAR: " << par.fi << " " << par.se << "\n";
long long Time = 0;
while(K > 0 && MAP.find(par)==MAP.end()){
MAP[par] = Time++;
int prev = par.se;
int curr = Mat[par.se-1][par.fi-1];
par = mp(prev, curr);
//cout << "PAR: " << par.fi << " " << par.se << "\n";
K--;
}
//cout << "FISH " << K << "\n";
//return 0;
if(K == 0){
cout << par.se << "\n";
return 0;
}
//cout << "KK: " << K << "\n";
long long l = Time - MAP[par];
//cout << "L = " << l << "\n";
K -= (K/l)*l;
//cout << "K = " << K << "\n";
while(K > 0){
int prev = par.se;
int curr = Mat[par.se-1][par.fi-1];
par = mp(prev, curr);
//cout << "PAR: " << par.fi << " " << par.se << "\n";
K--;
}
cout << par.se << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
1276 KB |
Output is correct |
3 |
Correct |
16 ms |
1916 KB |
Output is correct |
4 |
Correct |
18 ms |
2168 KB |
Output is correct |
5 |
Correct |
22 ms |
2296 KB |
Output is correct |
6 |
Correct |
24 ms |
2424 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
6 ms |
888 KB |
Output is correct |
9 |
Correct |
9 ms |
1272 KB |
Output is correct |
10 |
Correct |
20 ms |
2040 KB |
Output is correct |
11 |
Correct |
22 ms |
2296 KB |
Output is correct |
12 |
Correct |
24 ms |
2296 KB |
Output is correct |
13 |
Correct |
5 ms |
504 KB |
Output is correct |
14 |
Correct |
6 ms |
888 KB |
Output is correct |