//hdu 1027 Ignatius and the Princess II(阶乘进位模拟)
/*
比如 11 22
1*2*3*4>=22;
后面四位变动 8 9 10 11,前面按顺序输出
1、(23+1*2*3-1)/(1*2*3)=4; 选第四小的 11;22%(1*2*3)=4;
2、(4+1*2-1)/(1*2)=2;选择前面没选过的第2小 9;4%(2*1)=0 ;m=1*2;
3、(2+1-1)/1=2;选择前面没选过的第2小 10;2%1=0;m=1;
4、(1+1-1)/1=1;选择前面没有选过的第1小 8;1%1=0;m=1;
所以数列1 2 3 4 5 6 7 11 9 10 8
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1001;
int ans[N];
int ansn;
bool flag[N];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(flag,false,sizeof(flag));
int t=1,dt;
for(int i=1;i<1000;i++)
{
t=t*i;
if(t>=m)
{
dt=i;
break;
}
}
ansn=n-dt+1;
for(int i=0;i<ansn;i++)
{
ans[i]=i;
}
int dn=ansn;
for(int i=dt;i>=1;i--)
{
t=t/i;
int tmod=(m+t-1)/t;
for(int j=1;j<=dt;j++)
{
if(tmod==1&&flag[j]==false)
{
flag[j]=true;
ans[ansn++]=j+dn-1;
break;
}
else if(flag[j]==false)
{
tmod--;
}
}
m=m%t;
if(m==0) m=t;
}
for(int i=1;i<n;i++)
{
cout<<ans[i]<<" ";
}
cout<<ans[n];
cout<<endl;
}
return 0;
}
/hdu 1027 Ignatius and the Princess II(阶乘进位模拟)
最新推荐文章于 2024-07-22 10:30:00 发布