/*
ID: antwand1
TASK: pprime
LANG: C++
*/
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define du long double
#define F first
#define S second
#define FOR(a,b) for(int a=1;a<=b;a++)
#define FORl(a,b) for(a=1;a<=b;a++)
#define FOR0(a,b) for(int a=1;a<b;a++)
#define FORl0(a,b) for(a=0;a<b;a++)
using namespace std;
const int N=501;
int a[N][N];
int vis[N][N];
vector<int> vec;
vector<int> v;
main()
{
//freopen("pprime.in","r",stdin);freopen("pprime.out","w",stdout);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
vec.push_back(1);
vec.push_back(2);
int x=2,y=1;
int cnt=2,cur=0;
while(1){
// cout<<x<<" "<<y<<endl;
if(vis[x][y]){
cur=vis[x][y];
break;
}
vis[x][y]=cnt++;
vec.push_back(a[x][y]);
int Y=x;
int X=a[x][y];
x=X;
y=Y;
}
// cout<<"\n\n";
//cout<<cur<<"\n\n";
for(int i=0;i<cur;i++){
k--;
if(!k){
cout<<vec[i];
return 0;
}
}
for(int i=cur;i<vec.size();i++){
v.push_back(vec[i]);
}
int sz=v.size();
// cout<<"\n\n\n";
//for(int i=0;i<vec.size();i++)cout<<vec[i]<<" ";
// cout<<"\n\n\n";
// for(int i=0;i<v.size();i++)cout<<v[i]<<" ";
//cout<<"\n\n\n\n";
k--;
k%=sz;
cout<<v[k]<<endl;
}
Compilation message
politicari.cpp:22:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
22 | main()
| ^
politicari.cpp: In function 'int main()':
politicari.cpp:58:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=cur;i<vec.size();i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
11 ms |
2156 KB |
Output is correct |
3 |
Correct |
36 ms |
3308 KB |
Output is correct |
4 |
Correct |
45 ms |
3692 KB |
Output is correct |
5 |
Correct |
56 ms |
4076 KB |
Output is correct |
6 |
Correct |
56 ms |
3948 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
5 ms |
1260 KB |
Output is correct |
9 |
Correct |
16 ms |
2284 KB |
Output is correct |
10 |
Correct |
46 ms |
3692 KB |
Output is correct |
11 |
Correct |
58 ms |
4332 KB |
Output is correct |
12 |
Correct |
58 ms |
4332 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
1260 KB |
Output is correct |