Submission #218886

# Submission time Handle Problem Language Result Execution time Memory
218886 2020-04-03T02:42:12 Z eggag32 Zapina (COCI20_zapina) C++17
110 / 110
342 ms 2552 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end() 
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int MOD = 1e9 + 7;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }
 
ll dp[355][355][2]; //[index][tot][already have one happy]

ll fact[355];
ll invFact[355];

ll C(ll n, ll r){
	if(r > n) return 0;
	ll ret = fact[n];
	ret = ((ret * invFact[n - r]) + MOD) % MOD;
	ret = ((ret * invFact[r]) + MOD) % MOD;
	return ret;
}

ll mypow(ll a, ll p){
	if(p == 0) return 1;
	if(p == 1) return (a % MOD);
	if(p & 1) return ((a % MOD) * (mypow(a, p - 1) % MOD)) % MOD;
	ll x = mypow(a, p / 2) % MOD;
	return (x * x) % MOD;
}

ll inv(ll base){
	return mypow(base, MOD - 2);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n;
	cin >> n;
	rep(i, n + 2) rep(j, n + 2) rep(k, 2) dp[i][j][k] = 0LL;
	fact[0] = 1LL;
	invFact[0] = 1LL;
	for(ll i = 1LL; i < 355; i++){
		fact[i] = (1LL * fact[i - 1] * i) % MOD;
		invFact[i] = inv(fact[i]);
	}
	dp[1][1][1] = n;
	rep(i, n + 1) if(i != 1) dp[1][i][0] = C(n, i);
	repn(i, 2, n + 1){
		rep(j, n + 1){
			rep(k, n + 1) if((j + k) <= n){
				if(k == i){
					dp[i][j + k][1] = (dp[i][j + k][1] + (((dp[i - 1][j][1] + dp[i - 1][j][0]) % MOD) * C(n - j, k)) % MOD) % MOD;
				}
				else{
					dp[i][j + k][1] = (dp[i][j + k][1] + ((dp[i - 1][j][1] * C(n - j, k)) % MOD)) % MOD;
					dp[i][j + k][0] = (dp[i][j + k][0] + ((dp[i - 1][j][0] * C(n - j, k)) % MOD)) % MOD;
				}
			}
		}
	}
	cout <<  dp[n][n][1] % MOD << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 167 ms 1924 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
19 Correct 40 ms 1280 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 220 ms 2048 KB Output is correct
22 Correct 28 ms 1152 KB Output is correct
23 Correct 7 ms 640 KB Output is correct
24 Correct 51 ms 1280 KB Output is correct
25 Correct 37 ms 1280 KB Output is correct
26 Correct 72 ms 1528 KB Output is correct
27 Correct 309 ms 2304 KB Output is correct
28 Correct 306 ms 2304 KB Output is correct
29 Correct 316 ms 2424 KB Output is correct
30 Correct 314 ms 2304 KB Output is correct
31 Correct 327 ms 2424 KB Output is correct
32 Correct 318 ms 2316 KB Output is correct
33 Correct 334 ms 2424 KB Output is correct
34 Correct 329 ms 2304 KB Output is correct
35 Correct 328 ms 2304 KB Output is correct
36 Correct 339 ms 2552 KB Output is correct
37 Correct 342 ms 2304 KB Output is correct