Advertisement
Guest User

Untitled

a guest
Aug 1st, 2014
1,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <cassert>
  9. #include <ctime>
  10. #include <cmath>
  11. #include <string>
  12. #include <cstring>
  13. #include <queue>
  14. using namespace std;
  15.  
  16. #define f first
  17. #define s second
  18. #define mp make_pair
  19. #define pb push_back
  20. #define pii pair<int, int>
  21. #define vi vector<int>
  22. #define all(v) (v).begin(), (v).end()
  23. #define forit(it,v) for (__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
  24. #define f0(a) memset(a, 0, sizeof(a))
  25. #define ll long long
  26.  
  27. const int mod = 1000000007;
  28. int a[55], exceed[55];
  29. int C[55][55];
  30. int n, m, N, scores;
  31.  
  32. int calced[42][42][42*41], calcedn;
  33. int dp[42][42][42*41];
  34.  
  35. int Calc(int k, int last, int sum) {
  36.  
  37.     if (k == m) return scores + sum == N * (N - 1) / 2;
  38.  
  39.     if (last >= N) return 0;
  40.  
  41.     int &ans = dp[k][last][sum];
  42.  
  43.     if (calced[k][last][sum] == calcedn) return ans;
  44.     calced[k][last][sum] = calcedn;
  45.    
  46.     ans = Calc(k, last + 1, sum);
  47.     for (int i = 1; k + i <= m; ++i) {
  48.         sum += last;
  49.         if (sum + exceed[k + i] < (k + i) * (k + i - 1) / 2) break;
  50.  
  51.         ans = (ans + 1ll * C[m - k][i] * Calc(k + i, last + 1, sum)) % mod;
  52.     }
  53.     return ans;
  54. }
  55. void Solve() {
  56.     n = m = 0;
  57.     scanf("%d", &N);
  58.     for (int i = 0; i < N; ++i)  {
  59.         int x;
  60.         scanf("%d", &x);
  61.         if (x == -1) ++m;
  62.         else a[n++] = x;
  63.     }
  64.     sort(a, a + n);
  65.     int sum = 0;
  66.     for (int i = 0; i < n; ++i) {
  67.         sum += a[i];
  68.         if (i * (i + 1) / 2 > sum) {
  69.             puts("0");
  70.             return;
  71.         }
  72.     }
  73.     scores = sum;
  74.  
  75.     for (int i = 1; i <= m; ++i) {
  76.         int sum = 0;
  77.         exceed[i] = 0;
  78.         for (int j = 0; j < n; ++j) {
  79.             sum += a[j] - (i + j);
  80.             exceed[i] = min(exceed[i], sum);
  81.         }
  82.     }
  83.     ++calcedn;
  84.     cout << Calc(0, 0, 0) << endl;
  85.  
  86. }
  87. int main() {
  88.     for (int i = 0; i < 50; ++i) {
  89.         C[i][0] = 1;
  90.         for (int j = 1; j <= i; ++j) {
  91.             C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
  92.         }
  93.     }
  94.     int tests;
  95.     scanf("%d", &tests);
  96.     while (tests--)
  97.         Solve();
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement