# Count Scorecards

### Problem Statement :

```In a tournament, n 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 t denoting the number of test cases. t test cases follow.

The first line of each test case contains a single integer n. The second line contains n space-separated integers s1,s2,...,sn. si denotes the score of the ith player. If the score of the ith player has been erased, it is represented by -1.

Constraints
1 <= t <= 20
1 <= n <= 40
-1 <= si < n

Output Format

For each test case, output a single line containing the answer for that test case modulo 10^9+7.```

### Solution :

```                            ```Solution in C :

In C++ :

#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std ;
#define MAXN 42
#define MOD 1000000007

int n ;
vector<int> G ;

int check(vector<int> gg)
{
sort(gg.begin(),gg.end()) ;
int sum = 0 ;
for(int i = 0;i < n;i++)
{
sum += gg[i] ;
if(sum < i * (i + 1) / 2) return 0 ;
}
return 1 ;
}

int solve1(int k,int sum)
{
if(sum > n * (n - 1) / 2) return 0 ;
if(k == n) return sum == n * (n - 1) / 2 && check(G) ? 1 : 0 ;
if(G[k] != -1) return solve1(k + 1,sum + G[k]) ;

int ret = 0 ;
for(int i = 0;i < n;i++)
{
G[k] = i ;
ret += solve1(k + 1,sum + i) ;
G[k] = -1 ;
}
return ret ;
}

int solve1(vector<int> g)
{
G = g ;
n = G.size() ;
int ret = solve1(0,0) ;
return ret ;
}

int fac[MAXN],inv[MAXN] ;
int pow(int a,int b)
{
if(b == 0) return 1 ;
int ret = pow(a,b / 2) ;
ret = 1LL * ret * ret % MOD ;
if(b & 1) ret = 1LL * ret * a % MOD ;
return ret ;
}

int occ[MAXN],big[MAXN] ;
int memo[MAXN][MAXN][MAXN * MAXN] ;
int solve2(int k,int last,int sum)
{
if(last == n) return 0 ;
if(sum > n * (n - 1) / 2) return 0 ;
if(k == n) return big[last + 1] == 0 && sum == n * (n - 1) / 2 ? 1 : 0 ;
if(memo[k][last + 1][sum] != -1) return memo[k][last + 1][sum] ;
int occr = occ[last + 1] ;
int ret = 0,added = 0 ;
for(int i = 0;k + i <= n;i++)
{
if(sum + added < (k + i) * (k + i - 1) / 2) break ;
if(i >= occr) ret += 1LL * inv[i - occr] * solve2(k + i,last + 1,sum + added) % MOD ;
if(ret >= MOD) ret -= MOD ;
added += last + 1 ;
}
return memo[k][last + 1][sum] = ret ;
}

int solve2(vector<int> g)
{
fac[0] = inv[0] = 1 ;
for(int i = 1;i < MAXN;i++)
{
fac[i] = 1LL * i * fac[i - 1] % MOD ;
inv[i] = pow(fac[i],MOD - 2) ;
}
n = g.size() ;

int start = 0 ;
memset(occ,0,sizeof occ) ;
memset(big,0,sizeof big) ;
for(int i = 0;i < g.size();i++) if(g[i] != -1) occ[g[i]]++,start++ ;
for(int i = 0;i < n;i++)
for(int j = i;j < n;j++)
big[i] += occ[j] ;
memset(memo,255,sizeof memo) ;
int ret = solve2(0,-1,0) ;
ret = 1LL * ret * fac[n - start] % MOD ;
return ret ;
}

vector<int> gen()
{
int n = rand() % 10 + 1 ;
vector<int> ret ;
for(int i = 0;i < n;i++)
{
if(rand() % 2 == 0) ret.push_back(-1) ;
else ret.push_back(rand() % n) ;
}
return ret ;
}

void test()
{
for(int t = 1;t < 100;t++)
{
vector<int> g = gen() ;
int ret1 = solve1(g) ;
int ret2 = solve2(g) ;

cout << ret1 << " " << ret2 << endl ;
if(ret1 != ret2)
{
cout << "failed on: " << t << endl ;
cout << g.size() << " : " ;
for(int i = 0;i < g.size();i++) cout << g[i] << " " ; cout << endl ;
while(1) ;
}
}
}

void generate()
{
char in[] = "in .txt" ;
for(int test = 0;test < 10;test++)
{
in[2] = test + '0' ;
FILE * fout = fopen(in,"w") ;
int runs = 20 ;
fprintf(fout,"%d\n",runs) ;
for(int t = 0;t < runs;t++)
{
if(rand() % 3 != 0) n = rand() % 40 + 1 ;
else n = 40 - rand() % 10 ;
int per = 70 ;

vector<int> g ;
for(int i = 0;i < n;i++)
{
if(rand() % 100 < per) g.push_back(-1) ;
else g.push_back(rand() % n) ;
}

fprintf(fout,"%d\n",n) ;
for(int i = 0;i < n;i++)
{
if(i) fprintf(fout," ") ;
fprintf(fout,"%d",g[i]) ;
}
fprintf(fout,"\n") ;
}
}
}

int main()
{
// srand(time(NULL)) ;
// generate() ; return 0 ;
// test() ; return 0 ;

int runs ;
cin >> runs ;
while(runs--)
{
int n ;
vector<int> g ;
cin >> n ;
for(int i = 0;i < n;i++)
{
int k ;
cin >> k ;
g.push_back(k) ;
}
int ret = solve2(g) ;
printf("%d\n",ret) ;
}
return 0 ;
}

In C :

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
#include <stdio.h>
#include <stdlib.h>

#define P 1000000007
#define N 450

long long bin[50][50],jj,kk,h,ll,ii,t,a[50][50][1010],i,j,k,l,m,n,c[100],b[100];

int com(const void * xx, const void *yy)
{
if(*(long long *)xx < *(long long*)yy) return 1;
return -1;
}

int main()
{

for(i=0;i<50;i++) bin[i][0] = bin[i][i]=1;
for(i=1;i<50;i++)
for(j=1;j<i;j++) bin[i][j] = (bin[i-1][j-1]+ bin[i-1][j])%P;

scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
for(i=0;i<n;i++) scanf("%lld",&c[i]);

qsort(c,n,sizeof(c[0]),com);

for(i=0;i<=n;i++) b[i]=0;

m=0;
for(i=0;i<n;i++) if(c[i]!=-1) b[c[i]]++; else m++;

for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
for(k=0;k<=N;k++) a[i][j][k]=0;

a[n][0][0] = 1;
l=0;

for(i=n-1;i>=0;i--)
{
for(j=0;j<=m;j++)
{
h=0;
ll=l;
for(ii=0;ii<b[i];ii++)
{
h += (n-ll-j-1) - i;
ll++;
}

// printf("%lld %lld h %lld %lld=l\n",i,j,h,l);

for(k=0;k<=N;k++)
if(k+h>=0) a[i][j][k+h] = (a[i][j][k+h] + a[i+1][j][k])%P;
}

//for(ii=0;ii<=n;ii++)
// for(jj=0;jj<=m;jj++)
//  for(kk=0;kk<=1000;kk++)
//   if(a[ii][jj][kk]) printf("%lld %lld %lld -> %lld\n", ii,jj,kk,a[ii][jj][kk]);
//printf("stred -----------%lld\n",i);

// continue;

for(j=m;j>=0;j--)
for(k=0;k<N;k++)
{
h = k;
for(jj=1;jj<=j && h>=0;jj++)
{
h -= (n-l-b[i]-(j-jj)-1) - i;

if(h<0) break;

a[i][j][k] = (a[i][j][k] + bin[m-j+jj][jj]*a[i][j-jj][h])%P;
//    if(a[i][j-jj][h]>0)     printf("...%lld=i %lld=j %lld=k %lld=jj %lld=h\n",i,j,k,jj,h);
}
}

//for(ii=0;ii<=n;ii++)
// for(jj=0;jj<=m;jj++)
//  for(kk=0;kk<=1000;kk++)
//   if(a[ii][jj][kk]) printf("%lld %lld %lld -> %lld\n", ii,jj,kk,a[ii][jj][kk]);

//printf("-----------%lld\n",i);

l+=b[i];
}

/*
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
for(k=0;k<=1000;k++)
if(a[i][j][k]) printf("%lld %lld %lld -> %lld\n", i,j,k,a[i][j][k]);

for(i=0;i<=n;i++) printf("%lld %lld\n",i,c[i]);
*/
printf("%lld\n",(a[0][m][0]+P)%P);
//return 0;

}

return 0;
}```
```

## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

## The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T