Pagini recente » Cod sursa (job #1413422) | Cod sursa (job #237141) | Cod sursa (job #2906451) | Cod sursa (job #2768254) | Cod sursa (job #2260275)
#include <cstdio>
const int MOD = 2000003;
int FactFreq[6000], NrFactori;
int CombFact(int, int);
void InvMod(int, int, int&, int&);
int main()
{
freopen("sandokan.in", "r", stdin);
freopen("sandokan.out", "w", stdout);
int N, K;
scanf("%i %i", &N, &K);
K = (N % (K - 1) == 0 ? K - 1 : N % (K - 1));
printf("%i\n", CombFact(N - 1, K - 1));
return 0;
}
void InvMod(int a, int b, int &x, int &y)
{
if(!b)
{
x = 1;
y = 0;
}
else
{
InvMod(b, a % b, x, y);
int aux;
aux = x;
x = y;
y = aux - y * (a / b);
}
}
int CombFact(int N, int K)
{
/// C(N, K) = N! * (N-K)!^(-1) * K!^(-1)
int Result = 1, Fact = 1;
for(int i = N - K + 1; i <= N; ++i)
Result = 1LL * Result * i % MOD;
for(int i = 2; i <= K; ++i)
Fact = 1LL * Fact * i % MOD;
int x, y;
InvMod(Fact, MOD, x, y);
if(x < 0)
x = MOD + x % MOD;
return 1LL * Result * x % MOD;
}