Pagini recente » Cod sursa (job #2918117) | Cod sursa (job #2880019) | Cod sursa (job #3189739) | Cod sursa (job #1430182) | Cod sursa (job #1630790)
#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]*(long long)i)%LIMIT;
}
}
void find_the_solution()
{
solution=(factorial[nr_of_elements%K]*(long long)factorial[K-nr_of_elements%K])%LIMIT;
for (int i=0; i<K; i++)
solution=(solution*(long long)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;
}