/*
 * This program is written by Ender.
 * If you have any question,please send email to thelastender@gmail.com
 * File name: da.c
 * Compile and Link: gcc -c da.c -o da.o && gcc da.o -o da
 * 
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define LENGTH 13
#define MAX_NUM 4000000000U

void getSISN(int * S, unsigned long n); //get the steadily increase scale number(得到递增进位制数,我只知道这么翻译了)

char TABLE[2][LENGTH] = { {'a','b','c','d','e','f','g','h','i','j','k','l', 'm'}, {0,0,0,0,0,0,0,0,0,0,0,0} };

int main(int argc,char * argv[])
{
	if(argc < 2) {
		goto error1;
	}
	int l = strlen(argv[1]);
	if(l > 10) {
		goto error2;
	} 
	int i ;
	unsigned long n = 0;
	for(i = 0; i < l ; i++) {
		int c = argv[1][i];
		if(isdigit(c)) {
			n = n*10 + (c - 48);
			if(l == 10 && i == 0) {
				if( (c - 48) >= 4 ) {
					goto error2;
				}
			}
		} else {
			goto error2;
		}
	}
	/*fprintf(stderr, "%lu\n", n);
	if(n > MAX_NUM) {
		goto error2;
	}*/
	fprintf(stderr,"You want to find the %luth arrange type.\n",n);

	int S[LENGTH];
	for(i = 0; i < LENGTH; i++) {
		S[i] = 0;
	}

	getSISN(S,n);
	/*fprintf(stderr,"                           \t");
	for(i = (LENGTH - 1); i >= 0; i--) {
		fprintf(stderr,"%c ",TABLE[0][i]); 
	}
	fprintf(stderr,"\n");*/
	fprintf(stderr,"The intermediary number is:\t");
	for(i = (LENGTH - 1); i >= 0; i--) {
		fprintf(stderr,"%d ",S[i]); 
	}
	fprintf(stderr,"\n");

	i = LENGTH - 1;
	while(S[i] == 0) {
		i--;
	}

	//fprintf(stderr,"i is %d.\n",i);
	char * a = calloc(i + 2, sizeof(char));
	
	int len = i;
	int k = 0;
	int x = 0;
	int y = 0;
	for(i = len; i >= 0; i--) {
		x = 0;
		y = 0;
		k = 0;
		while(x < (S[i] + 1)) {
			if(TABLE[1][k] == 0) {
				x++;
			} else {
				y++;
			}
			k++;
		}
		//fprintf(stderr, "\tk=%d, x=%d, y=%d\n", k, x, y);
		a[i+1] = TABLE[0][x+y-1];
		TABLE[1][x+y-1] = 1;
	}
	i=0;
	while(TABLE[1][i] != 0 && i < l + 2) {
		i++;
	}
	a[0] = TABLE[0][i];
	fprintf(stderr,"Length is:\t%d.\n",len+2);

	fprintf(stderr,"Result is:\t");
	for(i=(len+1);i>=0;i--) {
		fprintf(stderr,"%c ",a[i]);
	}
	fprintf(stderr,"\n");

	free(a);
	goto successed;

	error1:
	fprintf(stderr,"Usage: %s [number]\n",argv[0]);
	return 1;

	error2:
	fprintf(stderr,"error:\tInput format error\n");
	fprintf(stderr,"error:\tThe biggest number supported is %u.\n",MAX_NUM);
	fprintf(stderr,"error:\tand no other character\n");
	return 2;

	successed:
	return 0;
}


void getSISN(int * S, unsigned long n) {
	int l = 0;
	int k = 2;
	unsigned long m;
	m = n;
	do {
		S[l] = m % k;
		m = m / k;
		l++;
		k++;
	} while(m != 0);
	return ;
}