/* comb2index.c
 * indexComb.c
 * Calcule le rang (index) d'une combinaison donnée de p éléments de [1 n]
 * C'est une application de l'ensemble de ces combinaisons sur l'ensemble
 * [0 (Cnp -1)]
 * comb2index  est l'application inverse de index2comb
 * 
 * Copyright (C) 2006 - 2009 Jean-Paul Davalan <jeux-et-mathematiques@davalan.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * 
 * 1) usage comb2index :
 *       comb2index n liste
 *    retour :
 *       n p index
 *
 * 2) usage index2comb :
 *       index2comb n p index
 *    retour :
 *       liste
 *
 *       
 * exemple :
 * ---------
 * index2comb 5 3
 * 10
 * (les numéros vont de 0 à 9=10-1)
 * 
 * index2comb 5 3 8
 * 4 2 1
 * (4 2 1 est la combinaison numéro 8)
 * 
 * comb2index 5  4 2 1
 * 5 3 8
 * (Dans [1 5] la combinaison {4, 2, 1} porte le numéro 8)
 * 
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned long long int
binom(int n, int p)
{
  unsigned long long int b = 1;
  int     i;
  if (p < 0 || n < 0 || p > n)
    return 0L;
  for (i = 0; i < p; i++) {
    b *= n - i;
    b /= i + 1;
  }
  return b;
}

int
isarg(int r, int argc, char *argv[])
{
  int     i;
  for (i = 2; i < argc; i++) {
    if (r == atoi(argv[i]))
      return 1;
  }
  return 0;
}

int
main(int argc, char *argv[])
{
  int     p = 0, n, i;
  unsigned long long k = 0L, b;
  if (argc < 2) {
    printf("usage : %s n liste\n", argv[0]);
    return 0;
  }
  n = atoi(argv[1]);
  for (i = 0; i < n; i++) {
    if (isarg(i + 1, argc, argv)) {
      p++;
    } else {
      b = binom(i, p - 1);
      k += b;
    }

  }
  printf("%d %d %Ld\n", n, p, k);
  return 0;
}

