Cod sursa(job #987807)

Utilizator darrenRares Buhai darren Data 21 august 2013 15:35:45
Problema Sortari2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <algorithm>

using namespace std;

const int MOD = 999017;

int N, D[1002];
int fact[1002], pow2[1002];

int main()
{
    ifstream fin("sortari2.in");
    ofstream fout("sortari2.out");

    fact[0] = 1;
    for (int i = 1; i <= 1000; ++i)
        fact[i] = 1LL * fact[i - 1] * i % MOD;
    pow2[0] = 1;
    for (int i = 1; i <= 1000; ++i)
        pow2[i] = 2LL * pow2[i - 1] % MOD;

    fin >> N;

    D[0] = 1;
    for (int i = 1; i <= N; ++i)
        for (int j = 1; j <= i; ++j)
        {
            if (j <= 2)
            {
                D[i] += D[i - j];
                if (D[i] >= MOD) D[i] -= MOD;

                continue;
            }
            D[i] = (D[i] + 1LL * D[i - j] * pow2[j - 2]) % MOD;
        }

    int result = fact[N] - D[N];
    if (result < 0) result += MOD;

    fout << result << '\n';

    fin.close();
    fout.close();
}