Official

D - Binary Alchemy Editorial by en_translator


Simulate the process of successively synthesizing elements \(1, 2, \ldots, N\) into element \(1\) while managing the current index of the element.

Sample code (beware of \(0\)-based indices)

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n));
	for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) {
		cin >> a[i][j];
		a[i][j]--;
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		if (ans >= i) ans = a[ans][i];
		else ans = a[i][ans];
	}
	ans++;
	cout << ans << '\n';
}

posted:
last update: