Advertisement
Fshl0

Balls

Jun 11th, 2022
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define pb push_back
  5. //#define mp make_pair
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9. typedef pair <ll, int> pli;
  10. typedef pair <ll,  ll>  pll;
  11. typedef pair <int, int> pii;
  12.  
  13. const int INF = 1e7;
  14. const ll llINF = 1e18;
  15. const int maxN = 1e3 + 9;
  16. const int maxM = 5e3 + 9;
  17. const int maxK = 1e3 + 9;
  18. const int MOD = 1000000007;
  19. const double pi = acos (-1);
  20.  
  21. int N, K, freq[maxN];
  22. long long memo[maxN][maxK], nCr[2 * maxN][2 * maxK];
  23.  
  24. long long dp (int currBall, int openedBoxes) {
  25.     if (openedBoxes > K)
  26.         return 0;
  27.    
  28.     if (currBall > N)
  29.         return openedBoxes == K;
  30.        
  31.     if (freq[currBall] == 0)
  32.         return dp (currBall + 1, openedBoxes);
  33.        
  34.     long long ret = 0;
  35.     for (int cnt = 0; cnt <= freq[currBall] && (cnt + openedBoxes <= K); cnt++) {
  36.         if (cnt == 0 && openedBoxes == 0)
  37.             continue;
  38.        
  39.         int n = freq[currBall] - cnt;
  40.         int k = cnt + openedBoxes;
  41.        
  42.         ret += nCr[K - openedBoxes][cnt] * nCr[n + k - 1][n] % MOD
  43.                 * dp (currBall + 1, k) % MOD;
  44.     }
  45.    
  46.     return memo[currBall][openedBoxes] = ret;
  47. }
  48.  
  49. void solve () {
  50.     cin >> N >> K;
  51.    
  52.     for (int i = 1; i <= N; i++)
  53.         freq[i] = 0;
  54.        
  55.     for (int i = 1; i <= N; i++) {
  56.         int x;
  57.         cin >> x;
  58.        
  59.         freq[x]++;
  60.     }
  61.    
  62.     for (int i = 0; i <= N; i++)
  63.         for (int j = 0; j <= K; j++)
  64.             memo[i][j] = -1;
  65.    
  66.     cout << dp (1, 0) << "\n";
  67.    
  68.     return;
  69. }
  70.  
  71. int main () {
  72.     int testCases = 1;
  73.     cin >> testCases;
  74.    
  75.     nCr[0][0] = 1;
  76.     for (int i = 1; i < 2 * maxN; i++) {
  77.         nCr[i][0] = nCr[i][i] = 1;
  78.          
  79.         for (int j = 1; j < i; j++) {
  80.             nCr[i][j] = (nCr[i - 1][j - 1] + nCr[i - 1][j]) % MOD;
  81.         }
  82.     }
  83.    
  84.     //ios_base::sync_with_stdio (0);
  85.     //cin.tie (0);
  86.    
  87.     //setIO ("billboard");
  88.     //preCalc ();
  89.     while (testCases--) {
  90.         //initialize common variv1les
  91.        
  92.        
  93.         //go solve
  94.         solve ();
  95.     }
  96.    
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement