Cod sursa(job #2514270)

Utilizator ArdeleanOficialAlexandru ArdeleanOficial Data 25 decembrie 2019 00:09:38
Problema Permutari2 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("permutari2.in");
ofstream fout("permutari2.out");

const int N = 309, MOD = 10007;

int n;

int fact[N];
int pt1[N];

int* solve(int k) {
    if (k == 1)
        return pt1;
    int eu[N];
    for (int i = 1; i <= n; ++i)
        eu[i] = 0;
    if (k&1) {
        int *ans = solve(k - 1);
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j + i <= n; ++j)
                eu[i + j] = (eu[i + j] + ans[i] * pt1[j]) % MOD;
    }
    else {
        int *ans = solve(k / 2);
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j + i <= n; ++j)
                eu[i + j] = (eu[i + j] + ans[i] * ans[j]) % MOD;
    }
    return eu;
}


int main()
{
    int k;
    int *ans;
    fin >> n >> k;
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
        fact[i] = fact[i - 1] * i % MOD;
    for (int i = 1; i <= n; ++i) {
        pt1[i] = fact[i];
        for (int j = 1; j < i; ++j)
            pt1[i] = (pt1[i] + MOD * MOD - pt1[j] * fact[i - j]) % MOD;
    }
    ans = solve(k);
    fout << ans[n];
    return 0;
}