CSES - Datatähti 2025 alku - Results
Submission details
Task:Kortit II
Sender:NoelMatero
Submission time:2024-11-07 20:48:40 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp:33:41: error: use of deleted function 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map() [with _Key = std::tuple<int, int, int>; _Tp = long long int; _Hash = std::hash<std::tuple<int, int, int> >; _Pred = std::equal_to<std::tuple<int, int, int> >; _Alloc = std::allocator<std::pair<const std::tuple<int, int, int>, long long int> >]'
   33 | unordered_map<tuple<int, int, int>, ll> memo;
      |                                         ^~~~
In file included from /usr/include/c++/11/unordered_map:47,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:117,
                 from input/code.cpp:1:
/usr/include/c++/11/bits/unordered_map.h:141:7: note: 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map() [with _Key = std::tuple<int, int, int>; _Tp = long long int; _Hash = std::hash<std::tuple<int, int, int> >; _Pred = std::equal_to<std::tuple<int, int, int> >; _Alloc = std::allocator<std::pair<const std::tuple<int, int, int...

Code

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;
const int MAX_N = 2000;

int power(int base, int exp) {
    int result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) {
            result = (1LL * result * base) % MOD;
        }
        base = (1LL * base * base) % MOD;
        exp /= 2;
    }
    return result;
}

void precompute_factorials(vector<int>& factorial, vector<int>& inv_factorial, int n) {
    factorial[0] = 1;
    for (int i = 1; i <= n; ++i) {
        factorial[i] = (1LL * factorial[i - 1] * i) % MOD;
    }
    
    inv_factorial[n] = power(factorial[n], MOD - 2);
    for (int i = n - 1; i >= 0; --i) {
        inv_factorial[i] = (1LL * inv_factorial[i + 1] * (i + 1)) % MOD;
    }
}

unordered_map<tuple<int, int, int>, ll> memo;

ll dp(int mask, int pos, int a_remaining, int b_remaining, const vector<int>& p1) {
    if (pos == p1.size()) {
        return (a_remaining == 0 && b_remaining == 0) ? 1 : 0;
    }

    tuple<int, int, int> state = make_tuple(mask, a_remaining, b_remaining);
    
    if (memo.find(state) != memo.end())
        return memo[state];
    
    ll res = 0;
    for (int num = 1; num <= p1.size(); num++) {
        int bit = num - 1;
        if (!(mask & (1 << bit))) { 
            if (num < p1[pos]) {
                if (a_remaining > 0)
                    res = (res + dp(mask | (1 << bit), pos + 1, a_remaining - 1, b_remaining, p1)) % MOD;
            }
            else if (num > p1[pos]) {
                if (b_remaining > 0)
                    res = (res + dp(mask | (1 << bit), pos + 1, a_remaining, b_remaining - 1, p1)) % MOD;
            }
            else { 
                res = (res + dp(mask | (1 << bit), pos + 1, a_remaining, b_remaining, p1)) % MOD;
            }
        }
    }
    
    return memo[state] = res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int t;
    cin >> t;
    
    while (t--) {
        int n, a, b;
        cin >> n >> a >> b;

        vector<int> factorial(n + 1), inv_factorial(n + 1);
        precompute_factorials(factorial, inv_factorial, n);
        
        vector<int> p1(n);
        for (int i = 0; i < n; i++) {
            p1[i] = i + 1;
        }
                
        memo.clear();
                
        ll answer = dp(0, 0, a, b, p1);
        
        cout << (answer * factorial[n]) % MOD << endl;
    }
    
    return 0;
}