Skip to content

Instantly share code, notes, and snippets.

@LCamel
Created November 29, 2011 17:28
Show Gist options
  • Select an option

  • Save LCamel/1405622 to your computer and use it in GitHub Desktop.

Select an option

Save LCamel/1405622 to your computer and use it in GitHub Desktop.
P(N, M) with template metaprogramming
// g++ -std=c++0x a.cpp 2>&1 | grep Print
const int N = 49;
const int M = 7;
template <long long n, int i> struct L { enum : long long {
v = L<n, i + 1>::v / (N - (i + 1))
};};
template <long long n > struct L<n, M - 1> { enum : long long {
v = n
};};
template <long long n, int i> struct B { enum : int {
v = (int) (L<n, i>::v % (N - i))
};};
template <long long n, int i> struct P;
template <long long n, int i, int j, int s, int k> struct U { enum : int {
v = U<n, i, j, (P<n, k>::v <= j ? s + 1 : s), k + 1>::v
};};
template <long long n, int i, int j, int s > struct U<n, i, j, s, i> { enum : int {
v = s
};};
template <long long n, int i, int b0, int j> struct S { enum : int {
v = U<n, i, j, 0, 0>::v + b0 == j ? j : S<n, i, b0, j + 1>::v
};};
template <long long n, int i, int b0 > struct S<n, i, b0, N> { enum : int {
v = 0xDead
};};
template <long long n, int i> struct P { enum : int {
v = S<n, i, B<n, i>::v, B<n, i>::v>::v
};};
template <int n, int m> struct PNM { enum : long long {
v = n * PNM<n - 1, m - 1>::v
};};
template <int n > struct PNM<n, 1> { enum : long long {
v = n
};};
template <int i> struct Print { enum : int {
v = (void) i
};};
template <long long n, int i> struct OL { enum : int {
v = OL<n, i + 1>::v,
v2 = Print<P<n, i>::v + 1>::v
};};
template <long long n > struct OL<n, M> { enum : int {
};};
constexpr const char *T = __TIME__; // hh:mm:ss
constexpr unsigned long long seed =
(T[0] - '0') * 100000 + (T[1] - '0') * 10000 + (T[3] - '0') * 1000 +
(T[4] - '0') * 100 + (T[6] - '0') * 10 + (T[7] - '0') * 1;
constexpr unsigned long long rand =
6364136223846793005LL * seed + 1442695040888963407; // MMIX
constexpr long long n = rand % PNM<N, M>::v;
OL<n, 0> go;
int main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment