#include <stdio.h>
#include <stdlib.h>
void print_ary(int *a, size_t sz)
{
int i;
for(i
= 0; i
< sz
; ++i
) printf("%d ", a
[i
]); }
void q_sort(int *a, int left, int right)
{
int i = left, j = right, key = a[(left + right) / 2];
if(left >= right) return;
// printf("i(%d,%d) ", left, right); print_ary(a, 5); printf("\n");
while(1){
for( ; i <= j; ++i) if(a[i] >= key) break;
for( ; j >= i; --j) if(a[j] <= key) break;
if(i >= j) break;
else{ int t = a[j]; a[j] = a[i]; a[i] = t; ++i; --j; }
}
if(right <= left + 1) return;
if(i > left) q_sort(a, left, i);
if(right > j) q_sort(a, j, right);
// printf("o(%d,%d) ", left, right); print_ary(a, 5); printf("\n");
}
int fact(int n)
{
if(n <= 1) return 1;
return n * fact(n - 1);
}
int ipow(int x, int y)
{
int n, r = x;
if(!y) return 1;
for(n = 1; n < y; ++n) r *= x;
return r;
}
void perm(int *a, int sz, int m)
{
int n, p;
int *b
= (int *)malloc((sz
- 1) * sizeof(int)); if(!b) return;
for(n = 0; n < (sz - 1); ++n){
int o = n + 2;
b[n] = (n ? p : m) % o;
p = (n ? p : m) / o;
}
for(n = 0; n < sz; ++n) a[n] = sz - n;
for(n = 0; n < (sz - 1); ++n){
int q = b[sz - 2 - n];
int r = a[n + q];
for( ; q > 0; --q) a[n + q] = a[n + q - 1];
a[n] = r;
}
}
void combi(int *a, int sz, int m)
{
int n;
for(n = 0; n < sz; ++n){
volatile int o = m / ipow(sz, n);
a[n] = o % sz + 1;
}
}
int main(int ac, char **av)
{
int ary[6];
int l, m, n, o = 0;
for(l = 5; l <= 6; ++l){
#if(0) // OK
for(m = 0; m < fact(l); ++m){
perm(ary, l, m);
#else // BAD
for(m = 0; m < ipow(l, l); ++m){
combi(ary, l, m);
#endif
print_ary
(ary
, l
); printf(" -> "); q_sort(ary, 0, l - 1);
print_ary
(ary
, l
); printf(" -> "); for(n = 1; n < l; ++n) if(ary[n] < ary[n - 1]){ n = 0; ++o; break; }
printf("%s\n", n
? "OK" : "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gaW50IHkpCnsKICBpbnQgbiwgciA9IHg7CiAgaWYoIXkpIHJldHVybiAxOwogIGZvcihuID0gMTsgbiA8IHk7ICsrbikgciAqPSB4OwogIHJldHVybiByOwp9Cgp2b2lkIHBlcm0oaW50ICphLCBpbnQgc3osIGludCBtKQp7CiAgaW50IG4sIHA7CiAgaW50ICpiID0gKGludCAqKW1hbGxvYygoc3ogLSAxKSAqIHNpemVvZihpbnQpKTsKICBpZighYikgcmV0dXJuOwogIGZvcihuID0gMDsgbiA8IChzeiAtIDEpOyArK24pewogICAgaW50IG8gPSBuICsgMjsKICAgIGJbbl0gPSAobiA/IHAgOiBtKSAlIG87CiAgICBwID0gKG4gPyBwIDogbSkgLyBvOwogIH0KICBmb3IobiA9IDA7IG4gPCBzejsgKytuKSBhW25dID0gc3ogLSBuOwogIGZvcihuID0gMDsgbiA8IChzeiAtIDEpOyArK24pewogICAgaW50IHEgPSBiW3N6IC0gMiAtIG5dOwogICAgaW50IHIgPSBhW24gKyBxXTsKICAgIGZvciggOyBxID4gMDsgLS1xKSBhW24gKyBxXSA9IGFbbiArIHEgLSAxXTsKICAgIGFbbl0gPSByOwogIH0KICBmcmVlKGIpOwp9Cgp2b2lkIGNvbWJpKGludCAqYSwgaW50IHN6LCBpbnQgbSkKewogIGludCBuOwogIGZvcihuID0gMDsgbiA8IHN6OyArK24pewogICAgdm9sYXRpbGUgaW50IG8gPSBtIC8gaXBvdyhzeiwgbik7CiAgICBhW25dID0gbyAlIHN6ICsgMTsKICB9Cn0KCmludCBtYWluKGludCBhYywgY2hhciAqKmF2KQp7CiAgaW50IGFyeVs2XTsKICBpbnQgbCwgbSwgbiwgbyA9IDA7CiAgZm9yKGwgPSA1OyBsIDw9IDY7ICsrbCl7CiNpZigwKSAvLyBPSwogICAgZm9yKG0gPSAwOyBtIDwgZmFjdChsKTsgKyttKXsKICAgICAgcGVybShhcnksIGwsIG0pOwojZWxzZSAvLyBCQUQKICAgIGZvcihtID0gMDsgbSA8IGlwb3cobCwgbCk7ICsrbSl7CiAgICAgIGNvbWJpKGFyeSwgbCwgbSk7CiNlbmRpZgogICAgICBwcmludF9hcnkoYXJ5LCBsKTsgcHJpbnRmKCIgLT4gIik7CiAgICAgIHFfc29ydChhcnksIDAsIGwgLSAxKTsKICAgICAgcHJpbnRfYXJ5KGFyeSwgbCk7IHByaW50ZigiIC0+ICIpOwogICAgICBmb3IobiA9IDE7IG4gPCBsOyArK24pIGlmKGFyeVtuXSA8IGFyeVtuIC0gMV0peyBuID0gMDsgKytvOyBicmVhazsgfQogICAgICBwcmludGYoIiVzXG4iLCBuID8gIk9LIiA6ICJCQUQiKTsKICAgIH0KICB9CiAgaWYobykgcHJpbnRmKCJcYSVkIGVycm9yKHMpIGZvdW5kXG4iLCBvKTsKICBlbHNlIHByaW50ZigiQWxsIENvcnJlY3RcbiIpOwogIHJldHVybiAwOwp9