제출 #1102329

#제출 시각아이디문제언어결과실행 시간메모리
1102329MinhKienZapina (COCI20_zapina)C++17
110 / 110
225 ms2408 KiB
#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;
}

컴파일 시 표준 에러 (stderr) 메시지

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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...