Created
November 28, 2011 18:47
-
-
Save LCamel/1401491 to your computer and use it in GitHub Desktop.
no-loop c version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
const int N = 49; | |
const int M = 7; | |
//const int N = 5; | |
//const int M = 3; | |
long long l(const long long n, const int i) { // left | |
return (i == M - 1) ? n : l(n, i + 1) / (N - (i + 1)); | |
} | |
int b(const long long n, const int i) { //branch | |
return (int) (l(n, i) % (N - i)); | |
} | |
int p(const long long n, const int i); | |
int u(const long long n, const int i, const int j, const int s, const int k) { // used | |
return (k == i) ? s : u(n, i, j, p(n, k) <= j ? s + 1 : s, k + 1); | |
// for (int k = 0; k < i; k++) | |
// if (p(n, k) <= j) | |
// s++; | |
// return s; | |
} | |
int s(const long long n, const int i, const int b0, const int j) { // search | |
return u(n, i, j, 0, 0) + b0 == j ? j : s(n, i, b0, j + 1); | |
} | |
int p(const long long n, const int i) { // n-th permutation | |
return s(n, i, b(n, i), b(n, i)); | |
} | |
//int p(const long long n, const int i) { // permutation | |
// const int b0 = b(n, i); | |
// for (int j = b0; j < N; j++) | |
// if (u(n, i, j, 0, 0) + b0 == j) | |
// return j; | |
//} | |
long long P(int n, int m) { // permutation, only invoked once | |
return m == 1 ? n : n * P(n - 1, m - 1); | |
} | |
int main(void) { | |
// for (int n = 0; n < P(N, M); n++) { | |
// for (int i = 0; i < M; i++) | |
// printf("%d ", p(n, i)); | |
// printf("\n"); | |
// } | |
srand((unsigned int) time(NULL)); | |
unsigned long long n = (0LL + rand()) << 32 | rand(); | |
//n %= 49LL * 48 * 47 * 46 * 45 * 44 * 43; | |
n %= P(N, M); | |
for (int i = 0; i < M; i++) | |
printf("%d ", p(n, i) + 1); | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment