Submission #344892

# Submission time Handle Problem Language Result Execution time Memory
344892 2021-01-06T17:18:22 Z limabeans Političari (COCI20_politicari) C++17
70 / 70
22 ms 2284 KB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;


const int maxn = 505;

int n; ll k;
int g[maxn][maxn];
int dp[maxn][maxn];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);
    
    cin>>n>>k;
    for (int i=1; i<=n; i++) {
	for (int j=1; j<=n; j++) {
	    cin>>g[i][j];
	}
    }

    if (k==1) out(1);

    
    assert(k>=2);
    memset(dp, -1, sizeof(dp));
    

    vector<int> stk = {-1, 1, 2};
    int idx = 2;
    while (idx<k) {
	int p = stk[idx-1];
	int at = stk[idx];
	assert(p!=at);
	
	if (dp[p][at] == -1) {
	    dp[p][at] = idx;
	    stk.push_back(g[at][p]);
	    idx++;
	    continue;
	}

	
	//cout<<"HERE"<<endl;
	assert(k>idx);
	ll rem = k-idx-1;
	ll len = idx-dp[p][at];
	
	rem %= len;
	ll x = dp[p][at]+1+rem;
	out(stk[x]);
    }

    cout<<stk.back()<<endl;    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 5 ms 1772 KB Output is correct
3 Correct 13 ms 2156 KB Output is correct
4 Correct 17 ms 2284 KB Output is correct
5 Correct 20 ms 2284 KB Output is correct
6 Correct 21 ms 2284 KB Output is correct
7 Correct 1 ms 1388 KB Output is correct
8 Correct 2 ms 1644 KB Output is correct
9 Correct 5 ms 1772 KB Output is correct
10 Correct 16 ms 2284 KB Output is correct
11 Correct 20 ms 2284 KB Output is correct
12 Correct 22 ms 2284 KB Output is correct
13 Correct 1 ms 1388 KB Output is correct
14 Correct 2 ms 1644 KB Output is correct