Cod sursa(job #1388009)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 14 martie 2015 23:58:34
Problema Sandokan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <algorithm>

#define DIM 5005
#define MOD 2000003
#define infile "sandokan.in"
#define outfile "sandokan.out"

using namespace std;

ifstream fin(infile);
ofstream fout(outfile);

int fact[DIM];

void euclid(int a, int b, int &x, int &y) {

	if (!b) {

		x = 1;
		y = 0;
		return;

	}

	int xa, ya;

	euclid(b, a % b, xa, ya);

	x = ya;
	y = xa - (a / b)*ya;

}

int main() {

	int n, k;

	fin >> n >> k;

	fact[0] = 1;

	for (int i = 1; i <= n; ++i)
		fact[i] = 1LL * fact[i - 1] * i % MOD;

	//solutia este Comb( n - 1, (n - 1) % (k - 1) )

	int sol = fact[n - 1];

	int temp = 1LL * fact[n - 1 - (n - 1) % (k - 1)] * fact[(n - 1) % (k - 1)] % MOD;

	int x, y;

	euclid(MOD, temp, x, y);

	while (y < 0)
		y += MOD;

	y %= MOD;

	sol = 1LL * sol * y % MOD;

	fout << sol;

	return 0;
}

//Trust me, I'm the Doctor!
//Miriam e tare!