CSES - Datatähti 2025 alku - Results
Submission details
Task:Kortit II
Sender:Chenran
Submission time:2024-11-10 11:56:15 +0200
Language:C++ (C++11)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#10.00 s1, 2, 3, 4, 5details
#20.00 s2, 3, 4, 5details
#30.01 s3, 4, 5details
#40.01 s4, 5details
#5ACCEPTED0.09 s5details
#6ACCEPTED0.13 s5details

Code

#include <iostream>

using namespace std;

const int MAX_N = 2000;
const long long MOD = 1000000007;

long long F[MAX_N + 1]{};
long long A[MAX_N + 1][MAX_N + 1]{};
long long C[MAX_N + 1][MAX_N + 1]{};
long long D[MAX_N + 1][MAX_N + 1]{};

long long compute_factorial(int n)
{
    if (n == 0)
        return 1;

    if (F[n] != 0)
        return F[n];

    F[n] = n * compute_factorial(n - 1) % MOD;

    return F[n];
}

long long compute_eulerian_number(int n, int k)
{
    if (n == k)
        return 0;

    if (A[n][k] != 0)
        return A[n][k];

    if (k >= n - k)
        k = n - k - 1;

    if (k == 0)
        return 1;

    A[n][k] = ((n - k) * compute_eulerian_number(n - 1, k - 1) % MOD +
               (k + 1) * compute_eulerian_number(n - 1, k) % MOD) %
              MOD;
    A[n][n - k - 1] = A[n][k];

    return A[n][k];
}

long long compute_binomial_coefficient(int n, int k)
{
    if (k == 0 || k == n)
        return 1;

    if (C[n][k] != 0)
        return C[n][k];

    if (k > n - k)
        k = n - k;

    C[n][k] = (compute_binomial_coefficient(n - 1, k - 1) +
               compute_binomial_coefficient(n - 1, k)) %
              MOD;
    C[n][n - k] = C[n][k];

    return C[n][k];
}

long long compute_derangement(int n, int k)
{
    if (D[n][k] != 0)
        return D[n][k];

    if (k > n - k)
        k = n - k;

    long long sum = 0;
    for (int j = 0; j < n - k; j++)
    {
        long long binomial = compute_binomial_coefficient(n, j);
        long long eulerian = compute_eulerian_number(n - j, k);
        long long item = binomial * eulerian % MOD;
        item = j % 2 == 0 ? item : MOD - item;
        sum += item;
        sum %= MOD;
    }
    D[n][k] = D[n][n - k] = sum;

    return sum;
}

int main()
{
    int t;
    cin >> t;

    while (t--)
    {
        int n, a, b;
        cin >> n >> a >> b;

        if ((a != 0 && b == 0) || (a == 0 && b != 0) || (a + b > n))
        {
            cout << 0 << endl;
            continue;
        }

        int tie = n - a - b;

        long long n_derangement = compute_derangement(n - tie, b);
        long long n_factorial = compute_factorial(n);
        long long n_tie_binomial = compute_binomial_coefficient(n, tie);
        long long result = n_derangement *
                           n_factorial % MOD *
                           n_tie_binomial % MOD;

        cout << result << endl;
    }

    return 0;
}

Test details

Test 1

Group: 1, 2, 3, 4, 5

Verdict:

input
54
4 4 0
3 1 3
3 2 2
4 0 4
...

correct output
0
0
0
0
0
...

user output
0
0
0
0
0
...

Test 2

Group: 2, 3, 4, 5

Verdict:

input
284
6 1 0
5 0 2
7 1 5
7 7 5
...

correct output
0
0
35280
0
36720
...

user output
0
0
35280
0
36720
...

Test 3

Group: 3, 4, 5

Verdict:

input
841
19 3 12
19 19 13
19 7 13
20 11 15
...

correct output
40291066
0
0
0
0
...

user output
40291066
0
0
0
0
...

Test 4

Group: 4, 5

Verdict:

input
1000
15 12 6
7 1 6
44 4 26
6 6 5
...

correct output
0
5040
494558320
0
340694548
...

user output
0
5040
494558320
0
340694548
...

Test 5

Group: 5

Verdict: ACCEPTED

input
1000
892 638 599
966 429 655
1353 576 1140
1403 381 910
...

correct output
0
0
0
249098285
0
...

user output
0
0
0
249098285
0
...

Test 6

Group: 5

Verdict: ACCEPTED

input
1000
2000 1107 508
2000 1372 249
2000 588 65
2000 1739 78
...

correct output
750840601
678722180
744501884
159164549
868115056
...

user output
750840601
678722180
744501884
159164549
868115056
...