Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created February 21, 2009 12:38
Show Gist options
  • Save mrkn/68028 to your computer and use it in GitHub Desktop.
Save mrkn/68028 to your computer and use it in GitHub Desktop.
#include <iostream>
template<bool cond, class THEN, class ELSE>
struct if_
{
typedef ELSE type;
};
template<class THEN, class ELSE>
struct if_<true, THEN, ELSE>
{
typedef THEN type;
};
template<unsigned long long N>
struct unsigned_long_long
{
static const unsigned long long value = N;
};
template<unsigned long N, unsigned long R>
struct choose
{
static const unsigned long long value = if_<(N<R),
unsigned_long_long<0>,
unsigned_long_long<(choose<N-1, R-1>::value +
choose<N-1, R >::value)>
>::type::value;
};
template<unsigned long N>
struct choose<N, 0>
{
static const unsigned long long value = 1;
};
template<unsigned long N>
struct choose<N, N>
{
static const unsigned long long value = 1;
};
int
main(int argc, char** argv)
{
std::cout << choose<500, 16>::value << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment