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 :



title-img


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








View More Similar Problems

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →