Pagini recente » Cod sursa (job #541690) | Cod sursa (job #1279353) | Cod sursa (job #2036006) | Cod sursa (job #1030003) | Cod sursa (job #1630769)
#include<fstream>
#define MNOE 5002 // maximum number of elements
#define LIMIT 666013
using namespace std;
FILE*in;
ofstream out("kperm.out");
int nr_of_elements;
int K;
long int factorial[MNOE];
int rest[MNOE];
long int solution;
void read()
{
in=fopen("kperm.in", "r");
fscanf(in, "%d%d", &nr_of_elements, &K);
}
void build_factorial_and_rest()
{
factorial[0]=1;
for (int i=1; i<=nr_of_elements; i++)
{
rest[i%K]++;
factorial[i]=(factorial[i-1]*i)%LIMIT;
}
}
void find_the_solution()
{
solution=(factorial[nr_of_elements%K]*factorial[K-nr_of_elements%K])%LIMIT;
for (int i=0; i<K; i++)
solution=(solution*factorial[rest[i]])%LIMIT;
}
void solve()
{
build_factorial_and_rest();
find_the_solution();
}
void show()
{
if (K % 2 == 0)
solution=0;
out<<solution;
}
int main()
{
read();
solve();
show();
return 0;
}