地址: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. FireBug与FirePHP

    a.FireBug,平时用的比较多.就是在客户端调试代码.如:hTML ,CSS,JS等 b.FireBug安装较容易. b-1,打开火狐浏览器 b-2,打开“附加组件” b-3.搜索“firebug ...

  2. java网络编程3-Socket

    // 有参构造函数创建的socket会访问网络,无参的则不会 //Socket socket=new Socket("www.baidu.com",80); // 当本地端口为0则 ...

  3. iOS 横向菜单

    MKHorizMenu 源码地址 现在想要实现以下效果,其中“选时间”这部分是一个NavigationBar,“日期”是横向的菜单,“电影时段”是TableView. 比较难实现的是横向菜单,因为没有 ...

  4. AderTemplate

    http://www.cnblogs.com/kwklover/archive/2007/07/12/815509.html 概述 AderTemplate是一个小型的模板引擎.无论是拿来直接使用还是 ...

  5. iOS 开发之--打测试包的时候报错的解决方法

    在打测试包的时候,一直出现一个报错,解决方法如下: 方法一: 描述文件所在目录是:~/Library/MobileDevice/Provisioning\ Profiles/,可以直接前往文件夹,把所 ...

  6. std::deque

    deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素.但它又与vector不同,deque支持高效插入和删除容器的头部元素 ...

  7. libevent(2)

    client.cpp // App02.cpp : 定义控制台应用程序的入口点. // #include <string.h> #include <errno.h> #incl ...

  8. SharePoint BI

    本篇博客主要针对SharePoint BI整体结构进行整理,为读者分析几种Sharepoint BI场景 先附一张自己做的结构图:

  9. phpstorm 9.0最新 注册码

    phpstorm注册码: User Name:newasp 01.License Key: ===== LICENSE BEGIN ===== 14617-12042010 00001xrVkhnPu ...

  10. vitess数据中心迁移步骤

    在使用vitess的时候我们会遇到刚开始所有的元数据都在同一个ETCD集群中,后续随着业务的增长需要进行数据中心拆分: 分别拆分成多个Local集群的数据中心: 以下就简单说明下迁移步骤: 迁移拓扑图 ...