Exemplo n.º 1
0
int
main ( void )
{
    long double c ;
    c = n_choose_k ( 12 , 9 ) ;
    printf ( "%.0Lf\n" , c ) ;
    if ( ( int ) c == 220 )
        printf ( "12c9 calculated correctly.\n" ) ;
    else
    {
        printf("Error.\n");
        exit ( -1 ) ;
    }
    c = n_choose_k ( 18 , 3 ) ;
    printf ( "%.0Lf\n" , c ) ;
    if ( ( int ) c == 816 )
        printf ( "18c3 calculated correctly.\n" ) ;
    else
    {
        printf("Error.\n");
        exit ( -1 ) ;
    }
    c = n_choose_k ( 35 , 8 ) ;
    printf ( "%.0Lf\n" , c ) ;
    if ( ( int ) c == 23535820 )
        printf ( "35c8 calculated correctly.\n" ) ;
    else
    {
        printf("Error.\n");
        exit ( -1 ) ;
    }

    return 0 ;
}
Exemplo n.º 2
0
int main() {
  sarl_unsigned_int t = 5;
  sarl_unsigned_int n = 10;
  sarl_unsigned_int i, j;

  sarl_ksubset_t *s;
  
  s = sarl_ksubset_new (n, t);

  sarl_unsigned_int count = 0;

  do {
    for (i=1;i<=t;i++) {
      //       printf ("%u ", sarl_int_array_get(s->c, i));
    }
    //     printf ("\n");
    ++count;
  } while (sarl_ksubset_has_next(s));

  sarl_int_array_t *a = 0;
  for (i = 0; i<=10; i++) {
    SARL_KSUBSET_FOR_EACH(a, n, i);
      for (j=1; j<=i; j++) {
        printf ("%u ", sarl_int_array_get(a, j));
      }
      printf ("\n");
    SARL_KSUBSET_END;
    printf ("------\n");
  }


  sarl_test_assert(count == n_choose_k(n, t));

  sarl_int_array_t *arr = 0;
  count = 0;
  SARL_KSUBSET_FOR_EACH(arr, n, t);
    ++count;
  SARL_KSUBSET_END;

  sarl_test_assert(count == n_choose_k(n, t));

  sarl_bit_set_t *subset, *bset = sarl_bit_set_default_new();
  {
    sarl_unsigned_int i;
    for(i=1;i<=12;i+=2) sarl_bit_set_set(bset, i);
  }

  count = 0;
  SARL_KSUBSET_FOR_EACH_BIT_SET(subset, bset, t);
    ++count;
    sarl_test_assert(sarl_bit_set_count(subset) == t);
  SARL_KSUBSET_END_BIT_SET;

  sarl_test_assert(count == n_choose_k(sarl_bit_set_count(bset), t));

  return 0;
}
Exemplo n.º 3
0
/*
 * generator_init
 *		initialize the generator of combinations
 *
 * The generator produces combinations of K elements in the interval (0..N).
 * We prebuild all the combinations in this method, which is simpler than
 * generating them on the fly.
 */
static CombinationGenerator *
generator_init(int n, int k)
{
	CombinationGenerator *state;

	Assert((n >= k) && (k > 0));

	/* allocate the generator state as a single chunk of memory */
	state = (CombinationGenerator *) palloc(sizeof(CombinationGenerator));

	state->ncombinations = n_choose_k(n, k);

	/* pre-allocate space for all combinations */
	state->combinations = (int *) palloc(sizeof(int) * k * state->ncombinations);

	state->current = 0;
	state->k = k;
	state->n = n;

	/* now actually pre-generate all the combinations of K elements */
	generate_combinations(state);

	/* make sure we got the expected number of combinations */
	Assert(state->current == state->ncombinations);

	/* reset the number, so we start with the first one */
	state->current = 0;

	return state;
}
Exemplo n.º 4
0
    int getSum(int n, int mod) {
        if (n < 3)
            return 0;
        init_n_choose_k(n, mod);

        std::vector<int> res(n + 1, 0);

        for (int i = 3; i <= n; ++i) {
            // j position of i-number sequence min
            for (int j = 0; j < i; ++j) {
                int children_score = (n_choose_k(i - 1, j) * ((res[j] + res[i - j - 1]) % mod)) % mod; // subtree score
                // node score
                int node_score = 0;
                // k is position of left min
                for (int l = 0; l < j; ++l) {
                    // k is position of right min
                    for (int r = j + 1; r < i; ++r) {
                        node_score += (r - l);
                        node_score %= mod;
                    }
                }
                node_score *= smart_factorial(i - 1, mod, j, i - 1 - j);
                node_score %= mod;

                res[i] += (node_score + children_score) % mod;
                res[i] %= mod;
            }
        }

        return res.back();
    }
Exemplo n.º 5
0
int n_choose_k (int a, int b)
{
    /* here N = a and k = b */

    if (!b)
        return 1;

    return (a * n_choose_k(a-1, b-1)) / b;
}
void generate_ith_subset(uint64_t i, uint32_t *subset, uint32_t subset_size, uint32_t max_set_value) {
    uint32_t pos = 0;
    uint32_t current_value = 1;
    uint64_t nck;

    while (pos < subset_size - 1) {
        //TODO: this does not need to be recalcualted, there is a faster way to do this
        //Would be the fastest way to do it if we were using a table of n choose k values -- which we should for a big int representation
        nck = n_choose_k((max_set_value - 1) - current_value, (subset_size - 1) - (pos + 1));

        if (i < nck) {
            subset[pos] = current_value;
            pos++;
        } else {
            i -= nck;
        }
        current_value++;
    }

    subset[subset_size - 1] = max_set_value;
}
Exemplo n.º 7
0
bool ShadeDB8_mpp::get_index(const size_t &N, const size_t &d, const  size_t &t, const size_t &S, const  db_type &DB_TYPE, size_t* ret_ndx)
{
	bool ret_val = false;
	//size_t ret_ndx=-1;
	size_t length=0, offset=0;
//	size_t length_t =10, length_d=10;
	size_t iN = 0, id = 0, it = 0;

	// ret_ndx==0 is an error condition.
	// check N
	if ((N < 1) || (N>8)) return ret_val;
	// check d
	if ((d < 1) || (d>10)) return ret_val;
	// check t
	if ((t < 1) || (t>10)) return ret_val;

	// check S value for validity
	// find number of s vectors
	size_t size_s = n_choose_k(t + N - 1, t);
	if ((S < 1) || (S>size_s)) return ret_val;



	switch (DB_TYPE)
	{
		case VMPP:
			length = 8;
			offset = 0;
			break;
		case IMPP:
			length = 8;
			offset = p_vmpp_uint8_size / 2; // short offset
			break;
	}
	if (length == 0) return ret_val;
	*ret_ndx = 0; // independent vectors for vmpp,impp,vs and is so offset=0

	size_t t_ub = 11; // upper bound of t index for iteration
	size_t d_ub = 10; // upper bound of d index for iteration
	do
	{
		iN++;
		d_ub = ((iN == N) ? d : 10);
		id = 0;
		do
		{
			id++;
			t_ub = (((iN==N) && (id==d)) ? t : 11);
			for (it = 1; it < t_ub; it++)
			{
				// find number of s vectors
				size_s = n_choose_k(it + iN - 1, it);
				// multiply by length of each S vector
				*ret_ndx += size_s*length;
			}
		} while (id < d_ub);
	} while (iN < N);
	*ret_ndx += (S - 1)*length;
	ret_val = true;
	return ret_val;
}