地址: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. 本地连接linux虚拟机的方法

    1.给虚拟机添加一个ehtX: 2. 3.添加一个ethX 4. 5.设置使用的是哪一个网卡,ifconfig的时候变会得到相应的ip 6. 再在cmd那ping ifconfig出来的自动获取的ip ...

  2. PHP 表格 post 提交 接受

    <form action="<?php echo site_url('home/order/sned_order'); ?>" method="post ...

  3. PAT004 Root of AVL Tree

    题目: An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...

  4. 微软公布带外安全更新MS14-068

     11月19日,微软公布了带外更新MS14-068以解决Windows 系统中的一个安全漏洞,该补丁安全等级为"严重". MS14-068 | Kerberos 中的漏洞可能同 ...

  5. HtmlEncode

    String.prototype.toHtmlEncode = function() { var str = this; str=str.replace("&"," ...

  6. 第6步:检查grid安装环境

    6.1 检查系统包 grid 身份下校验安装环境(检测crs安装环境(sgdb1)) [root@node1 soft]#su – grid [grid@node1 ~]$ cd /soft/grid ...

  7. 如何修改3D模型的原子属性

    Chem3D是专门用于绘制化学三维模型和进行计算化学数据的ChemOffice组件,在三维模型中每个原子都有众多属性,比如原子类型.原子符号.原子编号以及原子颜色等等.掌握Chem 3D模型的原子属性 ...

  8. MathType可以编辑带圈乘号吗

    在数学中有很多符号,可能这些符号我们用得上,也有些符号我们很少用,甚至用不上,但是我们用不上,不代表不存在这个符号,也不代表别人用不上,只是各自所涉及到的知识领域不一样而已.而对于加减乘除运算,几乎每 ...

  9. server r2 系统更新文件清理

    https://support.microsoft.com/zh-cn/kb/2852386

  10. Laravel5.1 搭建博客 --展示简单的首页

    今天起开始搭建博客,把之前学的东西运用下. 1 创建 配置项目 1.1 创建项目 composer create-project laravel/laravel blog 5.1.1 1.2 配置数据 ...