Submission #1029644

# Submission time Handle Problem Language Result Execution time Memory
1029644 2024-07-21T07:00:22 Z atom Magneti (COCI21_magneti) C++17
30 / 110
5 ms 2908 KB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

const int MOD = 1e9 + 7;
const int N = 2e4 + 5;

int bPow(int a, int b) {
    ll ans = 1;
    while (b) {
        if (b & 1) ans = (1LL * ans * a)  % MOD;
        a = (1LL * a * a) % MOD;
        b >>= 1;
    }
    return ans;
}

int fac[N], inv[N];

void prepare() {
    fac[0] = 1;
    for (int i = 1; i < N; ++i)
        fac[i] = (1LL * fac[i - 1] * i) % MOD;

    inv[N - 1] = bPow(fac[N - 1], MOD - 2);
    for (int i = N - 2; i >= 0; --i)
        inv[i] = (1LL * inv[i + 1] * (i + 1)) % MOD;
}

int C(int k, int n) {
    if (k > n) return 0;
    return ((1LL * fac[n] * inv[n - k]) % MOD * (inv[k])) % MOD;
}

int dp[51][51][10001];

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    
    #ifdef JASPER
        freopen("in1", "r", stdin);
    #endif

    prepare();

    int n, l;
    cin >> n >> l;
    vector <int> a(n + 1);
    for (int i = 1; i <= n; ++i) cin >> a[i];

    sort(a.begin(), a.end());
    if (a[1] == a[n]) {
        // sub1
        int p = (n - 1) * a[1] + 1; // min length for arrangement (empty slots taken)
        ll ans = (1LL * fac[n] * C(n, n + l - p)) % MOD;      
        cout << ans << "\n";  
    }
    else {
        // dp(i, j, p): number of ways to place first i magnets into j groups, length of arrangement = p; 
        dp[0][0][0] = 1;

        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= i; ++j) {
                for (int p = 1; p <= l; ++p) {
                    // create new group;
                    dp[i][j][p] = (dp[i][j][p] + dp[i - 1][j - 1][p - 1]) % MOD;
                    // place to the end of one in j groups
                    if (p >= a[i]) {
                        int ways = 2 * j; // j groups, each groups can be placed at the begin or at the end
                        dp[i][j][p] = (dp[i][j][p] + (dp[i - 1][j][p - a[i]] * ways) % MOD) % MOD;
                    }
                    // place between two groups
                    if (p >= 2 * a[i] - 1) {
                        int ways = j * (j + 1); // from (j + 1) groups, choose two to merge into one, each pair (x, y) has two ways to place: (x__a(i)__y) or (y__a(i)__x)
                        dp[i][j][p] = (dp[i][j][p] + (dp[i - 1][j + 1][p - 2 * a[i] + 1] * ways) % MOD) % MOD;
                    }       
                }
            }
        }

        ll ans = 0;
        for (int p = 1; p <= l; ++p) {
            debug(dp[n][1][p]);
            ans += (1LL * C(n, n + l - p) * dp[n][1][p]) % MOD;
            ans %= MOD;
        }
        cout << ans << "\n";
    }
    
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
Main.cpp:90:13: note: in expansion of macro 'debug'
   90 |             debug(dp[n][1][p]);
      |             ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2648 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 5 ms 2652 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 5 ms 2652 KB Output is correct
6 Correct 1 ms 872 KB Output is correct
7 Correct 5 ms 2652 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 5 ms 2648 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 5 ms 2652 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 5 ms 2652 KB Output is correct
16 Correct 1 ms 872 KB Output is correct
17 Correct 5 ms 2652 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Incorrect 3 ms 2908 KB Output isn't correct
22 Halted 0 ms 0 KB -