Ejemplo n.º 1
0
long stirling(int n, int k)
{
    long stir, binco1, binco2;
    binco1 = binomial_coefficient(n-1, k-1);
    binco2 = binomial_coefficient(n-1, k);
    stir = binco1 + (n-1) * binco2;
    return stir;
}
long binomial_coefficient(int n,int m)
{
  if(m == 0 || m == n)
    return 1;
  long row[n+1],tmp[n+1]; // 保存上一行的计算结果
  int i,j;
  for(i = 2;i < n;++i){
    row[i] = 0;
    tmp[i] = 0;
  }
  row[1] = 1;
  tmp[i] = 1;
  row[0] = 1;
  tmp[0] = 1;
  row[n] = 1;
  for(i = 2;i <= n;++i){
    for(j = 1; j < i;++j){


int main()
{
  int n,m;
  long binomial_coefficient(int,int);
  while(1){
    scanf("%d%d",&n,&m);
    printf("c(%d,%d)=%ld\n",n,m,binomial_coefficient(n,m));
  }
  return 0;
}
Ejemplo n.º 3
0
int binomial_coefficient(int n, int k) {
  if (n <= 1 || k == 0) {
    return 1;
  } else {
    return binomial_coefficient(n - 1, k - 1) * n / k;
  }
}
Ejemplo n.º 4
0
// Return largest value v where v < a and  Choose(v,b) <= x.
static uint_fast8_t largest_v(uint_fast8_t a, uint_fast8_t b, subset_t x)
{
	uint_fast8_t v = a - 1;

	while(binomial_coefficient(v,b) > x)
		v--;

	return(v);
}
Ejemplo n.º 5
0
Archivo: 369.c Proyecto: treblih/oj
int main(int argc, const char *argv[])
{
	int n, m;
	binomial_coefficient();
	while (scanf("%d%d", &n, &m), n) {
		printf("%d things taken %d at a time is %ld exactly.\n",
			n, m, bc[n][m]);
	}
	return 0;
}
Ejemplo n.º 6
0
matrix binomial(int n) {
  if ((n & 1) == 0) {
    throw std::invalid_argument("n must be odd");
  }

  matrix x, y;
  x.create(1, n);
  y.create(n, 1);

  for (int i = 0; i < n / 2; i++) {
    x(0, i) = x(0, n - i - 1) = binomial_coefficient(n - 1, i);
    y(i, 0) = y(n - i - 1, 0) = binomial_coefficient(n - 1, i);
  }

  x(0, n / 2) = binomial_coefficient(n - 1, n / 2);
  y(n / 2, 0) = binomial_coefficient(n - 1, n / 2);

  return y * x;
}
Ejemplo n.º 7
0
// Convert unsigned integer from combinadic to machine representation.
subset_t comb_to_int(subset_t c)
{
	uint_fast8_t k, n;
	subset_t i;

	i = 0;
	for(k = 0, n = 1; k < num_vertices; k++, c >>= 1)
		if(c & 1)
			i += binomial_coefficient(k, n++);
	return(i);
}
Ejemplo n.º 8
0
// Convert unsigned integer from machine representation to combinadic.
static subset_t int_to_comb(subset_t i)
{
	uint_fast8_t j, v;
	uint_fast8_t a = num_vertices;
	uint_fast8_t b = subset_size;
	subset_t c = 0;

	for(j = 0; j < subset_size; j++, b--)
	{
		v = largest_v(a, b, i);
		i = i - binomial_coefficient(v, b);
		a = v;
		c |= (1ul << v);
	}

	return(c);
}
Ejemplo n.º 9
0
void calculate_all(void)
{
	uint_fast8_t i;

	for(i = 0; i < num_vertices; i++)
	{
		slots[1ul << i] = cut_rank(1ul << i);
		cslots[1ul << i] = 0x0;
	}

	for(subset_size = 2; subset_size <= num_vertices; subset_size++)
	{
		subset_t i;
		const subset_t end = binomial_coefficient(num_vertices, subset_size);
		for(i = 0; i < end; i++)
			fill_slot(i);
	}
}
Ejemplo n.º 10
0
/**
 * @brief Given `x` this function calculates the probability of a shustring
 * with a length less than `x`.
 *
 * Let X be the longest shortest unique substring (shustring) at any position.
 * Then this function computes P{X <= x} with respect to the given parameter
 * set. See Haubold et al. (2009).
 *
 * @param x - The maximum length of a shustring.
 * @param g - The the half of the relative amount of GC in the DNA.
 * @param l - The length of the subject.
 * @returns The probability of a certain shustring length.
 */
double shuprop(size_t x, double p, size_t l) {
	double xx = (double)x;
	double ll = (double)l;
	size_t k;

	double s = 0.0;

	for (k = 0; k <= x; k++) {
		double kk = (double)k;
		double t = pow(p, kk) * pow(0.5 - p, xx - kk);

		s += pow(2, xx) * (t * pow(1 - t, ll)) *
			 (double)binomial_coefficient(x, k);
		if (s >= 1.0) {
			s = 1.0;
			break;
		}
	}

	return s;
}
Ejemplo n.º 11
0
void calculate_level(uint_fast8_t ss)
{
	uint_fast8_t i;

	subset_size = ss;

	if(subset_size == 0)
		slots[0] = 0;
	else if(subset_size == 1)
		for(i = 0; i < num_vertices; i++)
		{
			slots[1ul << i] = cut_rank(1ul << i);
			cslots[1ul << i] = 0x0;
		}
	else
	{
		subset_t i;
		const subset_t end = binomial_coefficient(num_vertices, subset_size);
		for(i = 0; i < end; i++)
			fill_slot(i);
	}
}
Ejemplo n.º 12
0
int main(int argc, char **argv)
{
    unsigned long i = 0;
    unsigned long seq_count = 0;
    unsigned long limit = 0;
    char **sequences = NULL;
    unsigned long *sizes = NULL;
    char *endptr = NULL;
    char *filename = NULL;
    int c = 0;
    int test_scores = 1;
    int test_stats = 0;
    char *matrixname = NULL;
    const parasail_matrix_t *matrix = NULL;
    gap_score_t gap = {INT_MIN,INT_MIN};
    int do_sse2 = 1;
    int do_sse41 = 1;
    int do_avx2 = 1;
    int do_disp = 1;
    int do_nw = 1;
    int do_sg = 1;
    int do_sw = 1;

    while ((c = getopt(argc, argv, "f:m:n:o:e:vsSi:")) != -1) {
        switch (c) {
            case 'f':
                filename = optarg;
                break;
            case 'm':
                matrixname = optarg;
                break;
            case 'n':
                errno = 0;
                seq_count = strtol(optarg, &endptr, 10);
                if (errno) {
                    perror("strtol");
                    exit(1);
                }
                break;
            case 'o':
                errno = 0;
                gap.open = strtol(optarg, &endptr, 10);
                if (errno) {
                    perror("strtol gap.open");
                    exit(1);
                }
                break;
            case 'e':
                errno = 0;
                gap.extend = strtol(optarg, &endptr, 10);
                if (errno) {
                    perror("strtol gap.extend");
                    exit(1);
                }
                break;
            case 'v':
                verbose = 1;
                break;
            case 's':
                test_stats = 1;
                break;
            case 'S':
                test_scores = 0;
                break;
            case 'i':
                do_sse2 = (NULL == strstr(optarg, "sse2"));
                do_sse41 = (NULL == strstr(optarg, "sse41"));
                do_avx2 = (NULL == strstr(optarg, "avx2"));
                do_disp = (NULL == strstr(optarg, "disp"));
                do_nw = (NULL == strstr(optarg, "nw"));
                do_sg = (NULL == strstr(optarg, "sg"));
                do_sw = (NULL == strstr(optarg, "sw"));
                break;
            case '?':
                if (optopt == 'f' || optopt == 'n') {
                    fprintf(stderr,
                            "Option -%c requires an argument.\n",
                            optopt);
                }
                else if (isprint(optopt)) {
                    fprintf(stderr, "Unknown option `-%c'.\n",
                            optopt);
                }
                else {
                    fprintf(stderr,
                            "Unknown option character `\\x%x'.\n",
                            optopt);
                }
                exit(1);
            default:
                fprintf(stderr, "default case in getopt\n");
                exit(1);
        }
    }

    if (filename) {
        parse_sequences(filename, &sequences, &sizes, &seq_count);
    }
    else {
        fprintf(stderr, "no filename specified\n");
        exit(1);
    }

    /* select the matrix */
    if (matrixname) {
        matrix = parasail_matrix_lookup(matrixname);
        if (NULL == matrix) {
            fprintf(stderr, "Specified substitution matrix not found.\n");
            exit(1);
        }
    }

    limit = binomial_coefficient(seq_count, 2);
    printf("%lu choose 2 is %lu\n", seq_count, limit);


#if HAVE_SSE2
    if (do_sse2 && parasail_can_use_sse2()) {
        if (test_scores) {
            if (do_nw) check_functions(parasail_nw_sse2, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_sse2, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_sse2, sequences, sizes, limit, matrix, gap);
        }
        if (test_stats) {
            if (do_nw) check_functions(parasail_nw_stats_sse2, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_stats_sse2, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_stats_sse2, sequences, sizes, limit, matrix, gap);
        }
    }
#endif

#if HAVE_SSE41
    if (do_sse41 && parasail_can_use_sse41()) {
        if (test_scores) {
            if (do_nw) check_functions(parasail_nw_sse41, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_sse41, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_sse41, sequences, sizes, limit, matrix, gap);
        }
        if (test_stats) {
            if (do_nw) check_functions(parasail_nw_stats_sse41, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_stats_sse41, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_stats_sse41, sequences, sizes, limit, matrix, gap);
        }
    }
#endif

#if HAVE_AVX2
    if (do_avx2 && parasail_can_use_avx2()) {
        if (test_scores) {
            if (do_nw) check_functions(parasail_nw_avx2, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_avx2, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_avx2, sequences, sizes, limit, matrix, gap);
        }
        if (test_stats) {
            if (do_nw) check_functions(parasail_nw_stats_avx2, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_stats_avx2, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_stats_avx2, sequences, sizes, limit, matrix, gap);
        }
    }
#endif

#if HAVE_KNC
    {
        if (test_scores) {
            if (do_nw) check_functions(parasail_nw_knc, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_knc, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_knc, sequences, sizes, limit, matrix, gap);
        }
        if (test_stats) {
            if (do_nw) check_functions(parasail_nw_stats_knc, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_stats_knc, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_stats_knc, sequences, sizes, limit, matrix, gap);
        }
    }
#endif

    if (do_disp) {
        if (test_scores) {
            if (do_nw) check_functions(parasail_nw_disp, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_disp, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_disp, sequences, sizes, limit, matrix, gap);
        }
        if (test_stats) {
            if (do_nw) check_functions(parasail_nw_stats_disp, sequences, sizes, limit, matrix, gap);
            if (do_sg) check_functions(parasail_sg_stats_disp, sequences, sizes, limit, matrix, gap);
            if (do_sw) check_functions(parasail_sw_stats_disp, sequences, sizes, limit, matrix, gap);
        }
    }

    for (i=0; i<seq_count; ++i) {
        free(sequences[i]);
    }
    free(sequences);
    free(sizes);

    return 0;
}
 void binomial_coefficient(data__ &v, natural__ n, natural__ k)
 {
   binomial_coefficient(v, static_cast<data__>(n), k);
 }