Hduoj2062【数学】

/*Subset sequence
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3183    Accepted Submission(s): 1652

Problem Description
Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3}. A subset sequence is defined as a array of a non-empty subset. 
Sort all the subset sequece of An in lexicography order. Your task is to find the m-th one.

Input
The input contains several test cases. Each test case consists of two numbers n and m ( 0< n<= 20, 0< m<= the total number of the subset 
sequence of An ).
 
Output
For each test case, you should output the m-th subset sequence of An in one line.

Sample Input
1 1
2 1
2 2
2 3
2 4
3 10
 

Sample Output
1
1
1 2
2
2 1
2 3 1
 

Author
LL
 

Source
校庆杯Warm Up 
*/
#include<stdio.h>
int main()
{
	__int64 i, j, k, m, n, a[22], out, len[22];
	len[1] = 1;
	for(i = 2; i < 21; i++)
		len[i] = (i-1) * len[i-1] + 1;//对于每个n 以1~n为开头的序列数的组数是一样的 所以可以求出每个数的组数 
	while(scanf("%I64d%I64d", &n, &m) != EOF)
	{
		for(i = 1; i < 21; i++)// 刚开始时每个i的对应位置都为i  随着i被取走,i位置后的值需要往前移 
		a[i] = i;
		while( m)
		{
			k = (m-1)/len[n] + 1;//求输出中的每个位置的数 
			printf("%I64d", a[k] );
			for(j = k; j < 21; j++)//输出后更新数组中的值 
			a[j] = a[j+1];
			m %= len[n];//每次还要更新输出中的下一个位置的位置 
			if(m == 0)
			m = len[n];
			m--;
			n--;//输出一个数减一个 
			if(m)
			printf(" ");
			else 
			printf("\n"); 
		}
	}
	return 0;
} 


关键在于找规律,想出求每一位的数字的方法

你可能感兴趣的:(c)