Submission #744307

#TimeUsernameProblemLanguageResultExecution timeMemory
744307MohamedAhmed04Magneti (COCI21_magneti)C++14
110 / 110
171 ms55892 KiB
#include <bits/stdc++.h> using namespace std ; const int mod = 1e9 + 7 ; int Add(int x , int y) { int z = x + y ; if(z >= mod) z -= mod ; return z ; } int Sub(int x , int y) { int z = x - y ; if(z < 0) z += mod ; return z ; } int Mul(int x , int y) { return (x * 1ll * y) % mod ; } int powlog(int base , int power) { if(power == 0) return 1 ; int x = powlog(base , power / 2) ; x = Mul(x , x) ; if(power & 1) x = Mul(x , base) ; return x ; } int modinv(int x) { return powlog(x , mod-2) ; } int Div(int x , int y) { return Mul(x , modinv(y)) ; } struct combination { vector<int>fact , inv ; combination(int sz) : fact(sz + 1) , inv(sz + 1) { fact[0] = 1 ; inv[0] = 1 ; for(int i = 1 ; i <= sz ; ++i) fact[i] = Mul(fact[i-1] , i) ; inv[sz] = modinv(fact[sz]) ; for(int i = sz-1 ; i >= 1 ; --i) inv[i] = Mul(inv[i+1] , i+1) ; } int choose(int n , int k) const { if(k < 0 || n < k) return 0 ; return Mul(Mul(fact[n] , inv[k]) , inv[n - k]) ; } }; const int MAX = 2e4 + 10 ; const int MAXN = 50 + 10 ; combination comb(MAX) ; int arr[MAXN] ; int n , m ; int dp[MAXN][MAXN][MAX] ; int stars_and_bars(int n , int k) { return comb.choose(n+k-1 , k-1) ; } int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n>>m ; for(int i = 1 ; i <= n ; ++i) cin>>arr[i] ; sort(arr+1 , arr+n+1) ; dp[0][0][0] = 1 ; for(int i = 1 ; i <= n ; ++i) { for(int j = 1 ; j <= i ; ++j) { for(int k = 0 ; k <= m ; ++k) { dp[i][j][k] = Mul(dp[i-1][j-1][k] , j) ; if(k >= arr[i]-1) dp[i][j][k] = Add(dp[i][j][k] , Mul(2 , Mul(dp[i-1][j][k-(arr[i]-1)] , j))) ; if(k >= 2*(arr[i]-1)) dp[i][j][k] = Add(dp[i][j][k] , Mul(dp[i-1][j+1][k-2*(arr[i]-1)] , j)) ; } } } int ans = 0 ; for(int k = 0 ; k+n <= m ; ++k) ans = Add(ans , Mul(dp[n][1][k] , stars_and_bars(m-k-n , n+1))) ; return cout<<ans<<"\n" , 0 ; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...