Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstdlib>
- #include <vector>
- #include <set>
- #include <map>
- #include <cassert>
- #include <ctime>
- #include <cmath>
- #include <string>
- #include <cstring>
- #include <queue>
- using namespace std;
- #define f first
- #define s second
- #define mp make_pair
- #define pb push_back
- #define pii pair<int, int>
- #define vi vector<int>
- #define all(v) (v).begin(), (v).end()
- #define forit(it,v) for (__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
- #define f0(a) memset(a, 0, sizeof(a))
- #define ll long long
- const int mod = 1000000007;
- int a[55], exceed[55];
- int C[55][55];
- int n, m, N, scores;
- int calced[42][42][42*41], calcedn;
- int dp[42][42][42*41];
- int Calc(int k, int last, int sum) {
- if (k == m) return scores + sum == N * (N - 1) / 2;
- if (last >= N) return 0;
- int &ans = dp[k][last][sum];
- if (calced[k][last][sum] == calcedn) return ans;
- calced[k][last][sum] = calcedn;
- ans = Calc(k, last + 1, sum);
- for (int i = 1; k + i <= m; ++i) {
- sum += last;
- if (sum + exceed[k + i] < (k + i) * (k + i - 1) / 2) break;
- ans = (ans + 1ll * C[m - k][i] * Calc(k + i, last + 1, sum)) % mod;
- }
- return ans;
- }
- void Solve() {
- n = m = 0;
- scanf("%d", &N);
- for (int i = 0; i < N; ++i) {
- int x;
- scanf("%d", &x);
- if (x == -1) ++m;
- else a[n++] = x;
- }
- sort(a, a + n);
- int sum = 0;
- for (int i = 0; i < n; ++i) {
- sum += a[i];
- if (i * (i + 1) / 2 > sum) {
- puts("0");
- return;
- }
- }
- scores = sum;
- for (int i = 1; i <= m; ++i) {
- int sum = 0;
- exceed[i] = 0;
- for (int j = 0; j < n; ++j) {
- sum += a[j] - (i + j);
- exceed[i] = min(exceed[i], sum);
- }
- }
- ++calcedn;
- cout << Calc(0, 0, 0) << endl;
- }
- int main() {
- for (int i = 0; i < 50; ++i) {
- C[i][0] = 1;
- for (int j = 1; j <= i; ++j) {
- C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
- }
- }
- int tests;
- scanf("%d", &tests);
- while (tests--)
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement