Cod sursa(job #2230780)

Utilizator giotoPopescu Ioan gioto Data 11 august 2018 14:24:07
Problema Kperm Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>
using namespace std;

int n, k;
const int MOD = 666013;
int main()
{
    freopen("kperm.in", "r", stdin);
    freopen("kperm.out", "w", stdout);

    scanf("%d%d", &n, &k);
    if(k % 2 == 0) {printf("0"); return 0;}

    int c = n % k;
    if(c == 0) c = k;

    int x = 1, y = 1;
    for(int i = 1; i <= c ; ++i) x = (1LL * x * i) % MOD;
    for(int i = 1; i <= k - c ; ++i) y = (1LL * y * i) % MOD;

    int Sol = (1LL * x * y) % MOD;
    for(int i = 1, t = 1; i <= n ; i += k, ++t){
        for(int j = i; j <= i + c - 1 && j <= n ; ++j)
            Sol = (1LL * Sol * t) % MOD;
        for(int j = i + c; j <= i + k - 1 && j <= n ; ++j)
            Sol = (1LL * Sol * t) % MOD;
    }
    printf("%d", Sol);


    return 0;
}