hdu 1027

http://acm.hdu.edu.cn/showproblem.php?pid=1027

此题没什么算法,就是考组合数学吧!细节想了好久~

/*************************************************************************
	> File Name: main.cpp
	> Author: huangshuai
	> Mail: [email protected] 
	> Created Time: Wed 13 Mar 2013 12:04:31 PM CST
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int xx[10]={0,1,2,6,24,120,720,5040,40320};
int num[1005];
void inser(int a,int b)
{
	int t=num[b];
	int i;
	for(i=b-1;i>=a;i--)
	{
		num[i+1]=num[i];
	}
	num[a]=t;
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i;
		for(i=1;i<=n;i++)
			num[i]=i;
		while(m>1)
		{
			for(i=1;i<=8;i++)
			{
				if(m>xx[i])
					;
				else
					break;
			}
			int t=(m-1)/xx[i-1];
			inser(n-i+1,n-i+1+t);
			m=m-t*xx[i-1];
			
		}
		printf("%d",num[1]);
		for(i=2;i<=n;i++)
			printf(" %d",num[i]);
		printf("\n");
	}


你可能感兴趣的:(hdu 1027)