Cod sursa(job #482242)

Utilizator ProtomanAndrei Purice Protoman Data 2 septembrie 2010 20:50:24
Problema Permutari2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <algorithm>
#include <stdio.h>

#define MAX 310
#define restRez 10007

using namespace std;

int n, k;
int perm[MAX];
int a[MAX][MAX];

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

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

	perm[0] = 1;
	for (int i = 1; i <= n; i++)
		perm[i] = (perm[i - 1] * i) % restRez;

	for (int i = 1; i <= n; i++)
	{
		int pt = 0;
		for (int j = 1; j <= i; j++)
		{
			for (int h = 1; h < i; h++)
			{
				a[i][j] = a[i][j] + (a[i - h][j - 1] * a[h][1]) % restRez;
				a[i][j] -= (a[i][j] >= restRez)? restRez : 0;
			}

			pt = pt + a[i][j];
			pt -= (pt >= restRez)? restRez : 0;
		}

		a[i][1] = (perm[i] + restRez - pt) % restRez;
	}

	printf("%d\n", a[n][k]);

	fclose(stdin);
	fclose(stdout);
	return 0;
}