Cerinta completa

In a tournament, players play against each other exactly once. Each game results in exactly one player winning. There are no ties. You have been given a scorecard containing the scores of each player at the end of the tournament. The score of a player is the total number of games the player won in the tournament. However, the scores of some players might have been erased from the scorecard. How many possible scorecards are consistent with the input scorecard?

Input Format

The first line contains a single integer denoting the number of test cases. test cases follow.

The first line of each test case contains a single integer . The second line contains space-separated integers . denotes the score of the th player. If the score of the th player has been erased, it is represented by .

Constraints

Output Format

For each test case, output a single line containing the answer for that test case modulo .

Sample Input 0

5
3
-1 -1 2
3
-1 -1 -1
4
0 1 2 3
2
1 1
4
-1 -1 -1 2

Sample Output 0

2
7
1
0
12

Explanation 0

For the first case, there are 2 scorecards possible: (0,1,2) or (1,0,2).
For the second case, the valid scorecards are (1,1,1), (0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0).
For the third case, the only valid scorecard is (0,1,2,3).
For the fourth case, there is no valid scorecard. It is not possible for both players to have score of 1.
For the fifth case, 6-variations of {(3,1,0)[2]}, and 3 variations each of {(2,2,0)[2]} and {(2,1,1)[2]}.


Limbajul de programare folosit: cpp14

Cod:

#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;
}

Scor obtinut: 1.0

Submission ID: 464649126

Link challenge: https://www.hackerrank.com/challenges/count-scorecards/problem

Count Scorecards