CSES - Datatähti 2025 alku - Results
Submission details
Task:Kortit II
Sender:Kemm1706
Submission time:2024-11-02 20:33:32 +0200
Language:C++ (C++11)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED3
#2ACCEPTED5
#3ACCEPTED26
#4ACCEPTED28
#5ACCEPTED38
Test results
testverdicttimegroup
#1ACCEPTED0.04 s1, 2, 3, 4, 5details
#2ACCEPTED0.04 s2, 3, 4, 5details
#3ACCEPTED0.04 s3, 4, 5details
#4ACCEPTED0.04 s4, 5details
#5ACCEPTED0.04 s5details
#6ACCEPTED0.04 s5details

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector <ll> vl;
typedef vector <vl> vvl;

const ll inf = 1e18;
const ll modd = 1e9 + 7;

ll pow(ll a, ll n)
{
    ll ans = 1, i = 0;
    while((1ll << i) <= n)
    {
        if((1ll << i) & n)
        {
            ans *= a;
            ans %= modd;
            //cerr << i << ": " << ans << "\n";
        }
        a *= a;
        a %= modd;
        i++;
    }
    return ans;
}

ll cal(ll a, ll n, const vl &mul, const vl &inv)
{
    ll ans = mul[n] * inv[n - a]  % modd * inv[a] % modd;
    //ll ans = mul[n] / mul[n - a] / mul[a];
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll t, n, a, b, i, k;
    cin >> t;
    vvl ans(2005, vl(2005, 0));
    vl mul(2005), inv(2005);
    mul[0] = mul[1] = inv[0] = inv[1] = 1;
    for(i = 2; i <= 2000; i++)
    {
        mul[i]  = mul[i - 1] * i % modd;
        inv[i] = pow(mul[i], modd - 2);
    }
    //cerr << "!!!!!";
    ans[2][1] = 1;
    for(k = 3; k <= 2000; k++)
        for(i = 1; i < k; i++)
        {
            ans[k][i] = (k - 1) * ans[k - 2][i - 1] % modd + i * ans[k - 1][i] % modd + ans[k - 1][i - 1] * (k - i) % modd;
            ans[k][i] %= modd;
        }
    //cerr << "??????";
    while(t--)
    {
        cin >> n >> a >> b;
        if((a == 0) && (b == 0))
        {
            cout << mul[n] << "\n";
            continue;
        }
        if(a + b > n || a * b == 0)
            {
                cout << "0\n";
                continue;
            }
        cout << cal(n - a - b, n, mul, inv) * ans[a + b][a] % modd * mul[n] % modd << "\n";
        //cerr << cal(a, n, mul, inv) << " ";
        //cerr << mul[n] * inv[n] % modd << "\n";
        //cerr << pow(n, a) << "\n";
    }
// * mul[n] / mul[n - a - b] / mul[n - a - b + 1] * mul[a + b]
    return 0;
}

Test details

Test 1

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

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: ACCEPTED

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: ACCEPTED

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: ACCEPTED

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
...