Cod sursa(job #1912301)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 8 martie 2017 00:44:44
Problema Kperm Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>
#include <cmath>
#define MaxN 5005
#define MOD 666013
using namespace std;
  
FILE*IN,*OUT;

long long N,K,C,R,Answer=1,f[MaxN];
long long LogPow(int base,int exp)
{
	int act=1,pow=base;
	for(int pos=0;pos<=19;pos++)
	{
		if(exp&(1<<pos))
			act=(act*pow)%MOD;
		pow=(pow*pow)%MOD;
	}
	return act;
}
int main()
{
    IN=fopen("kperm.in","r");
    OUT=fopen("kperm.out","w");

	fscanf(IN,"%d%d",&N,&K);

	f[0]=1;
	for(int i=1;i<=N;i++)
		f[i]=(f[i-1]*i)%MOD;

	C=N/K,R=N%K;
	Answer=(f[R]*f[K-R])%MOD;
	Answer=(Answer*LogPow(f[C+1],R))%MOD;
	Answer=(Answer*LogPow(f[C],K-R))%MOD;
	fprintf(OUT,"%d",Answer);
	
	return 0;
}