Cod sursa(job #109879)

Utilizator sims_glAlexandru Simion sims_gl Data 25 noiembrie 2007 12:52:30
Problema NKPerm Scor 10
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasele 11-12 Marime 0.94 kb
#include <stdio.h>

int n, k, T, f[32], a[32], pos, used[32];

int main()
{
	freopen("nkperm.in", "r", stdin);
	freopen("nkperm.out", "w", stdout);

	scanf("%d%d%d", &n, &k, &T);

	f[0] = f[1] = 1;

	for (int i = 2; i <= n; ++i)
		f[i] = f[i - 1] * i;

	for (int t = 1; t <= T; ++t) {
		char ch;

		scanf(" %c ", &ch);

		if (ch == 'A') {
			for (int i = 1; i <= n; ++i)
				scanf("%d", &a[i]);

			int pos = 1;

			for (int i = 1; i <= n; ++i) {
				int aux = a[i];

				for (int j = 1; j < i; ++j)
					if (a[j] < a[i])
						--aux;

				pos += (aux - 1) * f[n - i];
			}

			printf("%d\n", pos);
		} else {
			scanf("%d", &pos);

			for (int i = 1; i <= n; ++i)
				used[i] = 0;

			for (int i = 1; i <= n; ++i) {
				int aux = (pos - 1) / f[n - i] + 1;

				pos -= (aux - 1) * f[n - i];

				int crt = 1;

				for (; aux; ++crt)
					if (!used[crt])
						--aux;

				printf("%d ", crt - 1);

				used[crt - 1] = 1;
			}

			printf("\n");
		}
	}

	return 0;
}