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

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →

Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →

Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →

Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

View Solution →