Cod sursa(job #1630769)

Utilizator PraetorGrigorosoaia Florin Praetor Data 5 martie 2016 11:13:14
Problema Kperm Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#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;
}