# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1102329 | MinhKien | Zapina (COCI20_zapina) | C++17 | 225 ms | 2408 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |