제출 #848292

#제출 시각아이디문제언어결과실행 시간메모리
848292fcmalkcinPolitičari (COCI20_politicari)C++17
70 / 70
13 ms3160 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb  push_back
#define eb  emplace_back
#define ll  long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"

const int maxn = 5e2 + 10;  // Giữ nguyên định nghĩa maxn
const ll mod = 1e9 + 7;
const ll base = 1e18;

int n;
int a[maxn][maxn], f[maxn][maxn], dp[maxn * maxn];

ll k;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
            --a[i][j];
        }
    }

    int prev = 0, curr = 1;
    dp[1] = 0;
    dp[2] = 1;
    f[prev][curr] = 2;
    for (int i = 3; (ll)i <= k; ++i) {
        int nxt = a[curr][prev];
        prev = curr;
        curr = nxt;
        if (f[prev][curr] == 0) {
            f[prev][curr] = i;
            dp[i] = curr;
            continue;
        }
        int j = f[prev][curr];
        int cycle = i - j;
        int ret = j + ((k - j) % cycle);
        cout << dp[ret] + 1 << endl;
        return 0;
    }

    cout << dp[k] + 1 << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...