#include<stdio.h>
#define TOP_WIDTH (5) // ← 最上行の数字個数
#define ALL_N (TOP_WIDTH * (TOP_WIDTH + 1) / 2)
// 並べ替え結果処理
void kekka_shori(int *d, int len)
{
int i, j, tmp[2][TOP_WIDTH], *p[2], *swap, ans[ALL_N], *pa = &ans[0];
unsigned long b = 0x0;
for (i = 0; i < TOP_WIDTH; i++) {
*pa++ = tmp[0][i] = d[i];
}
p[0] = &tmp[0][0];
p[1] = &tmp[1][0];
for (j = TOP_WIDTH; j >= 2; j--) {
for (i = 0; i < j; i++) {
b |= 1 << (p[0][i] - 1);
}
for (i = 0; i < j - 1; i++) {
p[1][i] = p[0][i] - p[0][i + 1];
if (p[1][i] < 0) {
p[1][i] = -p[1][i];
}
*pa++ = p[1][i];
}
// swap
swap = p[0];
p[0] = p[1];
p[1] = swap;
}
b |= 1 << (p[0][0] - 1);
if (b == ~(0xFFFFFFFF << ALL_N)) {
pa = &ans[0];
for (j = TOP_WIDTH; j >= 1; j--) {
printf("%*s", (TOP_WIDTH
- j
) * 2, ""); for (i = 0; i < j; i++) {
}
}
}
}
// 並べ替える
// (len個の配列の右n個を左にスライドして再帰)
void narabe(int *d, int len, int n)
{
int i, j;
int tmp;
if (n <= (ALL_N - TOP_WIDTH)) {
kekka_shori(d, len);
return;
}
for (j = 0; j < n; j++) {
narabe(d, len, n - 1);
tmp = d[len - n];
for (i = 0; i < n - 1; i++) {
d[len - n + i] = d[len - n + i + 1];
}
d[len - n + i] = tmp;
}
}
int main()
{
int d[ALL_N];
int i;
if (ALL_N > 32) {
fprintf(stderr
, "現状、総数32個までしか計算出来ないので %d個は無理です\n", ALL_N
); return 1;
}
for (i = 0; i < ALL_N; i++) {
d[i] = i + 1;
}
narabe(d, ALL_N, ALL_N);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCiNkZWZpbmUgVE9QX1dJRFRIICg1KSAgICAgICAgICAgLy8g4oaQIOacgOS4iuihjOOBruaVsOWtl+WAi+aVsAoKI2RlZmluZSBBTExfTiAgICAgKFRPUF9XSURUSCAqIChUT1BfV0lEVEggKyAxKSAvIDIpCgovLyDkuKbjgbnmm7/jgYjntZDmnpzlh6bnkIYKdm9pZCBrZWtrYV9zaG9yaShpbnQgKmQsIGludCBsZW4pCnsKICAgIGludCBpLCBqLCB0bXBbMl1bVE9QX1dJRFRIXSwgKnBbMl0sICpzd2FwLCBhbnNbQUxMX05dLCAqcGEgPSAmYW5zWzBdOwogICAgdW5zaWduZWQgbG9uZyBiID0gMHgwOwoKICAgIGZvciAoaSA9IDA7IGkgPCBUT1BfV0lEVEg7IGkrKykgewogICAgICAgICpwYSsrID0gdG1wWzBdW2ldID0gZFtpXTsKICAgIH0KICAgIHBbMF0gPSAmdG1wWzBdWzBdOwogICAgcFsxXSA9ICZ0bXBbMV1bMF07CiAgICBmb3IgKGogPSBUT1BfV0lEVEg7IGogPj0gMjsgai0tKSB7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IGo7IGkrKykgewogICAgICAgICAgICBiIHw9IDEgPDwgKHBbMF1baV0gLSAxKTsKICAgICAgICB9CiAgICAgICAgZm9yIChpID0gMDsgaSA8IGogLSAxOyBpKyspIHsKICAgICAgICAgICAgcFsxXVtpXSA9IHBbMF1baV0gLSBwWzBdW2kgKyAxXTsKICAgICAgICAgICAgaWYgKHBbMV1baV0gPCAwKSB7CiAgICAgICAgICAgICAgICBwWzFdW2ldID0gLXBbMV1baV07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgKnBhKysgPSBwWzFdW2ldOwogICAgICAgIH0KICAgICAgICAvLyBzd2FwCiAgICAgICAgc3dhcCA9IHBbMF07CiAgICAgICAgcFswXSA9IHBbMV07CiAgICAgICAgcFsxXSA9IHN3YXA7CiAgICB9CiAgICBiIHw9IDEgPDwgKHBbMF1bMF0gLSAxKTsKCiAgICBpZiAoYiA9PSB+KDB4RkZGRkZGRkYgPDwgQUxMX04pKSB7CiAgICAgICAgcGEgPSAmYW5zWzBdOwogICAgICAgIHByaW50ZigiLS0tXG4iKTsKICAgICAgICBmb3IgKGogPSBUT1BfV0lEVEg7IGogPj0gMTsgai0tKSB7CiAgICAgICAgICAgIHByaW50ZigiJSpzIiwgKFRPUF9XSURUSCAtIGopICogMiwgIiIpOwogICAgICAgICAgICBmb3IgKGkgPSAwOyBpIDwgajsgaSsrKSB7CiAgICAgICAgICAgICAgICBwcmludGYoIiUyZCAgIiwgKnBhKyspOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHByaW50ZigiXG4iKTsKICAgICAgICB9CiAgICB9Cn0KCi8vIOS4puOBueabv+OBiOOCiwovLyAgKGxlbuWAi+OBrumFjeWIl+OBruWPs27lgIvjgpLlt6bjgavjgrnjg6njgqTjg4njgZfjgablho3luLApCnZvaWQgbmFyYWJlKGludCAqZCwgaW50IGxlbiwgaW50IG4pCnsKICAgIGludCBpLCBqOwogICAgaW50IHRtcDsKCiAgICBpZiAobiA8PSAoQUxMX04gLSBUT1BfV0lEVEgpKSB7CiAgICAgICAga2Vra2Ffc2hvcmkoZCwgbGVuKTsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBmb3IgKGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgbmFyYWJlKGQsIGxlbiwgbiAtIDEpOwogICAgICAgIHRtcCA9IGRbbGVuIC0gbl07CiAgICAgICAgZm9yIChpID0gMDsgaSA8IG4gLSAxOyBpKyspIHsKICAgICAgICAgICAgZFtsZW4gLSBuICsgaV0gPSBkW2xlbiAtIG4gKyBpICsgMV07CiAgICAgICAgfQogICAgICAgIGRbbGVuIC0gbiArIGldID0gdG1wOwogICAgfQp9CgppbnQgbWFpbigpCnsKICAgIGludCBkW0FMTF9OXTsKICAgIGludCBpOwoKICAgIGlmIChBTExfTiA+IDMyKSB7CiAgICAgICAgZnByaW50ZihzdGRlcnIsICLnj77nirbjgIHnt4/mlbAzMuWAi+OBvuOBp+OBl+OBi+ioiOeul+WHuuadpeOBquOBhOOBruOBpyAlZOWAi+OBr+eEoeeQhuOBp+OBmVxuIiwgQUxMX04pOwogICAgICAgIHJldHVybiAxOwogICAgfQogICAgZm9yIChpID0gMDsgaSA8IEFMTF9OOyBpKyspIHsKICAgICAgICBkW2ldID0gaSArIDE7CiAgICB9CgogICAgbmFyYWJlKGQsIEFMTF9OLCBBTExfTik7CiAgICBwcmludGYoIlxuIik7CiAgICByZXR1cm4gMDsKfQ==