Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

A solution for the "Binomial Showdown" problem

A solution for the "Binomial Showdown" problem.

Problem description:
http://acm.uva.es/p/v5/530.html

Author: Joana Matos Fonseca da Trindade
Date: 2008.04.11

   1  
   2  /*
   3   * Solution for the "Binomial Showdown" problem.
   4   * UVa ID: 530
   5   */
   6  
   7  #include <iostream>
   8  
   9  using namespace std;
  10  
  11  /* main */
  12  int main() {
  13      int n, k;
  14      unsigned long long r; /* result */
  15  
  16      while(cin >> n >> k && ((n != 0) || (k != 0))) {
  17          /* init result */
  18          r = 1;
  19  
  20          /* if k is more than half of n, then use the complement */
  21          if(k > (n / 2)) {
  22              k = n - k;
  23          }
  24  
  25          /*         
  26           * C(n,k) = n! / (k!(n-k)!) =
  27           * (n)(n-1)(...)(n-k+1) / 2*3*4*(...)*k
  28           */ 
  29          for (int i=0; i<k; i++) {
  30              r = r * (n - i);   /* (n)(n-1)(...)(n-k+1) */
  31              r = r / (1 + i);   /* 2*3*4*(...)*k */
  32          }
  33          cout << r << endl;
  34      }
  35  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS