답안 #1102329

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102329 2024-10-18T02:33:55 Z MinhKien Zapina (COCI20_zapina) C++17
110 / 110
225 ms 2408 KB
#include <iostream>

using namespace std;

#define AT "CHIAKEO"
#define ll long long

const ll MOD = 1e9 + 7;
const int N = 360;

int n;
ll gt[N], inv[N];
ll binpow(ll x, ll p) {
    ll res = 1;
    while (p > 0) {
        if (p & 1) {
            res = (res * x) % MOD;
        }

        p >>= 1;
        x = (x * x) % MOD;
    }

    return res;
}

ll nCk(ll x, ll k) {

    ll res = gt[x] * inv[x - k] % MOD * inv[k] % MOD;
    return res;
}

namespace subtask2 {
    void main() {
        ll RealAns = 0;
        for (int i = 1; i < (1 << n); ++i) {
            ll way = 1;
            ll sum = n;
            bool ck = true;

            for (int j = 0; (1 << j) <= i; ++j) {
                if (i >> j & 1) {
                    if (sum < (ll)(j + 1)) {
                        ck = false;
                        break;
                    }

                    way = (way * nCk(sum, j + 1)) % MOD;
                    sum -= (ll)(j + 1);
                }
            }

            int bit = __builtin_popcount(i);
            ll k = n - bit;
            way = (way * binpow(k, sum)) % MOD;

            if (ck) {
                if (bit % 2) {
                    RealAns = (RealAns + way) % MOD;
                } else {
                    RealAns = (RealAns - way + MOD * MOD) % MOD;
                }
            }
        }

        cout << RealAns;
    }
};

ll dp[N][N][2];

int main() {
    if (fopen(AT".inp", "r")) {
        freopen(AT".INP", "r", stdin);
        freopen(AT".OUT", "w", stdout);
    }

    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);

    cin >> n;
    gt[0] = 1;
    inv[0] = 1;
    for (int i = 1; i <= n; ++i) {
        gt[i] = (gt[i - 1] * (ll)i) % MOD;
        inv[i] = binpow(gt[i], MOD - 2);
    }

    dp[0][0][0] = 1;
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            for (int k = 0; k <= j; ++k) {

                if (i == k) {
                    dp[i][j][1] = (dp[i][j][1] + dp[i - 1][j - k][1] * nCk(j, k) % MOD + dp[i - 1][j - k][0] * nCk(j, k) % MOD) % MOD;
                } else {
                    dp[i][j][0] = (dp[i - 1][j - k][0] * nCk(j, k) % MOD + dp[i][j][0]) % MOD;
                    dp[i][j][1] = (dp[i - 1][j - k][1] * nCk(j, k) % MOD + dp[i][j][1]) % MOD;
                }

            }
        }
    }

    cout << dp[n][n][1] << endl;

    return 0;
}

Compilation message

zapina.cpp: In function 'int main()':
zapina.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen(AT".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
zapina.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen(AT".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 508 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 508 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 508 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 106 ms 1864 KB Output is correct
18 Correct 2 ms 592 KB Output is correct
19 Correct 24 ms 1400 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 141 ms 2004 KB Output is correct
22 Correct 17 ms 1104 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 32 ms 1360 KB Output is correct
25 Correct 23 ms 1204 KB Output is correct
26 Correct 43 ms 1416 KB Output is correct
27 Correct 204 ms 2320 KB Output is correct
28 Correct 205 ms 2376 KB Output is correct
29 Correct 207 ms 2388 KB Output is correct
30 Correct 206 ms 2376 KB Output is correct
31 Correct 215 ms 2376 KB Output is correct
32 Correct 213 ms 2408 KB Output is correct
33 Correct 212 ms 2376 KB Output is correct
34 Correct 215 ms 2364 KB Output is correct
35 Correct 215 ms 2384 KB Output is correct
36 Correct 220 ms 2376 KB Output is correct
37 Correct 225 ms 2328 KB Output is correct