Cod sursa(job #1331681)

Utilizator andreioneaAndrei Onea andreionea Data 31 ianuarie 2015 23:08:46
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <fstream>
#include <vector>

int main()
{
	std::vector<std::vector<int>> stramosi_exp;
	std::vector<int> v;
	int n, m;
	std::ifstream f("stramosi.in");
	std::ofstream g("stramosi.out");
	f >> n >> m;
	v.push_back(0);
	for (int i = 0; i < n; ++i) {
		int x;
		f >> x;
		v.push_back(x);
	}
	stramosi_exp.push_back(v);
	for (int i = 1; (1 << i) <= n; ++i) {
		v.clear();
		v.push_back(0);
		for (int j = 1; j <= n; ++j) {
			int x = stramosi_exp[i - 1][stramosi_exp[i - 1][j]];
			v.push_back(x);
		}
		stramosi_exp.push_back(v);
	}
	while (m--) {
		int p, q;
		f >> q >> p;
		int rez = q;
		int i = 0;
		if (p > n)
			rez = 0;
		else
			while (p) {
				if (p & 1)
					rez = stramosi_exp[i][rez];
				++i;
				p /= 2;
				if (!rez)
					break;
			}
		g << rez << "\n";
	}
	f.close();
	g.close();
	return 0;
}