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

题目:

Problem Description
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."

"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?

 
Input
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of file.
 
Output
For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.
 
Sample Input
6 4
11 8
 
Sample Output
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
 
思路:
  通过观察可以发现,n个数有n!种排法;
  所以可以先列出1-9内的阶乘,然后求出要改变多少个数的位置;
  举个例子: 9 21
  t=21-1;(因为从小大大排列是最小的顺序,)
  t/3!=3 ,不等于0,所以要改变最后3+1个数字(6 7 8 9), 推出选第3+1个数字选9;剩下(6 7 8)
  令t=t%3;
  t/2=1;推出选第1+1个数字7;剩下(6 8);
      t=t%2;
  t/1=0;推出选第1+0个数字6;剩下( 8);
      t=t%2;
  t/1=0;推出选第1+0个数字6;无剩下数字;
  结束;
  另外注意下输出格式,第一次交的时候就是PE了。。。。。
   

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std;
int j[];
int num[];
void so(int n,int m)
{
int change=,point,all=n;
memset(num,,sizeof(num));
while(m/j[change]==)
change--;
point = n-change;
all=change +;
for(int i =point; i<=n; i++)
num[i]=;
printf("%d",);
for(int i =; i<point; i++)
printf(" %d",i);
while(all--)
{
int t=m/j[change];
for(int i =point,k=; i<=n; i++)
if(num[i])
{
if(k==t)
{
printf(" %d",i);
num[i]=;
break;
}
k++;
}
m%=j[change--];
}
putchar('\n');
}
int main(void)
{
int n, m;
j[]=j[] = ;
for (int i = ; i <= ; i++)
j[i] = j[i - ] * i;
while (scanf("%d%d", &n, &m) == )
{
if(m == )
{
for(int i=; i<=n-; i++)
printf("%d ",i);
printf("%d\n",n);
}
else
so(n,m-);
} return ;
}
  

杭电1027Ignatius and the Princess II模拟的更多相关文章

  1. HDU——1027Ignatius and the Princess II(next_permutation函数)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  2. 杭电 1002 A + B Problem II【大数相加】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 解题思路:就是把大的数用数组存放起来,像小学的时候用竖式加法来算两个数相加那样算: 反思:思路很 ...

  3. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  4. 杭电ACM刷题(1):1002,A + B Problem II 标签: acmc语言 2017-05-07 15:35 139人阅读 评

    最近忙于考试复习,没有多少可供自己安排的时间,所以我利用复习之余的空闲时间去刷刷杭电acm的题目,也当对自己编程能力的锻炼吧. Problem Description I have a very si ...

  5. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  6. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  7. 杭电dp题集,附链接还有解题报告!!!!!

    Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...

  8. 杭电ACM题单

    杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...

  9. 2018 Multi-University Training Contest 1 杭电多校第一场

    抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001  Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...

随机推荐

  1. [Tips] Resolve error: server certificate verification failed.

    # sympton: piaoger@piaoger-ubuntu:~/w/temp$ git clone https://mygit/solidmcp/solidmcp.gitCloning int ...

  2. 马尔科夫毯(Markov Blanket)

    最优特征子集:选出特征的子集,能够比较准确的代表原来的特征.马尔科夫毯(MB)是贝叶斯网络(BN)的最有特征子集. 推测贝叶斯网络的网络结构是NP问题.贝叶斯网络中一个节点T的马尔科夫毯是其父节点,子 ...

  3. 【BZOJ】3412: [Usaco2009 Dec]Music Notes乐谱(二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3412 维护前缀和,然后直接二分即可... #include <cstdio> #incl ...

  4. tplink 703刷固件

    1.软件下载: ImageBuilder链接 如果是全新刷机的话,使用:http://downloads.openwrt.org/snapshots/trunk/ar71xx/generic/open ...

  5. ShadowCaster 代码

    Pass { Name "ShadowCaster" Tags{"LightMode" = "ShadowCaster"} CGPROGRA ...

  6. js的实例方法和静态方法分析

    var Person=function(){}; Person.say=function(){ console.log('I am a Person,I can say.') }; Person.pr ...

  7. cannot be cast to javax.servletFilter

    java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast ...

  8. vue+node+mongoDB 火车票H5(一)---准备工作,基本配置

    前端菜鸟一枚,由于公司项目用到了vue,我虽然参与了,但是很多环境配置和流程还不是特别清楚,就想自己个人业余做个webapp看看, 对于完全新手而言,很多坑会纠结很久,所以想借此机会自己做的同时记录各 ...

  9. idle命令行按ALT+P重复调出上个语句

    idle命令行按ALT+P重复调出上个语句

  10. Java应用多机器部署定时任务解决方案

    Java多机部署下定时任务的处理方案. 本文转自:http://www.cnblogs.com/xunianchong/p/6958548.html 需求: 有两台服务器同时部署了同一套代码, 代码中 ...