Ignatius and the Princess II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4865    Accepted Submission(s): 2929
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
 

注意:由于1000的阶乘太大,并且M小于等于10000,所以我们仅仅须要算到阶乘大于10000的为就能够了,也就是8。。之后推断是不是第八位的特殊推断就可以。

代码:

#include <stdio.h>
#include <string.h>
int a[9] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
int vis[1005];
int main(){
int n, m;
while(scanf("%d%d", &n, &m) == 2){
memset(vis, 0, sizeof(vis));
m -= 1;
int cou, temp = 1;
while(temp < n){
if((n - temp) <= 8){
int s = m/a[n-temp];
int p = m%a[n-temp];
int c = 0;
for(int i = 1; i <= n; i ++){
if(!vis[i]) ++c;
if((c-1) == s){
printf("%d ", i);
vis[i] = 1; break;
}
}
m = p;
}
else{
for(int i = 1; i <= n; i ++){
if(!vis[i]) {
vis[i] = 1;
printf("%d ", i); break;
}
}
}
++temp;
}
for(int i = 1; i <= n; i ++){
if(!vis[i]) printf("%d\n", i);
}
}
return 0;
}

hdoj 1027 Ignatius and the Princess II 【逆康托展开】的更多相关文章

  1. HDU 1027 Ignatius and the Princess II(康托逆展开)

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

  2. HDU 1027 Ignatius and the Princess II(求第m个全排列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/10 ...

  3. HDU - 1027 Ignatius and the Princess II 全排列

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

  4. HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]

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

  5. poj 1027 Ignatius and the Princess II全排列

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

  6. hdu 1027 Ignatius and the Princess II(正、逆康托)

    题意: 给N和M. 输出1,2,...,N的第M大全排列. 思路: 将M逆康托,求出a1,a2,...aN. 看代码. 代码: int const MAXM=10000; int fac[15]; i ...

  7. 【HDOJ】1027 Ignatius and the Princess II

    这道题目最开始完全不懂,后来百度了一下,原来是字典序.而且还是组合数学里的东西.看字典序的算法看了半天才搞清楚,自己仔细想了想,确实也是那么回事儿.对于长度为n的数组a,算法如下:(1)从右向左扫描, ...

  8. HDU 1027 Ignatius and the Princess II 选择序列题解

    直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS. 使用删除优化,那么就能够达到0MS了. 删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针 ...

  9. HDU 1027 - Ignatius and the Princess II

    第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...

随机推荐

  1. wine的使用

    ubuntu-ubuntu10.04使用wine安装SourceInsight http://www.cnblogs.com/eddy-he/archive/2012/03/08/ubuntu_win ...

  2. PHP高级教程-安全邮件

    PHP Secure E-mails 在上一节中的 PHP e-mail 脚本中,存在着一个漏洞. PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html> < ...

  3. 图说十大数据挖掘算法(一)K最近邻算法

    如果你之前没有学习过K最近邻算法,那今天几张图,让你明白什么是K最近邻算法. 先来一张图,请分辨它是什么水果 很多同学不假思索,直接回答:“菠萝”!!! 仔细看看同学们,这是菠萝么?那再看下边这这张图 ...

  4. document.ready、window.onload、body.onload的区别

    document的ready事件通常会比window的onload事件先发生,为什么呢? 因为document的ready是在浏览器加载解析并构建完doc文档模型时发生的,而window的onload ...

  5. webservice系统学习笔记3-分析wsdl文件的组成

    详细分析前面章节的服务的wsdl文件 1.http://localhost:8888/ws01?wsdl 2.http://localhost:8888/ws01?xsd=1 在接口服务中添加复杂类型 ...

  6. 微信小程序Tab选项卡切换大集合

    代码地址如下:http://www.demodashi.com/demo/14028.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  7. iOS-APP启动页加载广告

    概述 加载广告页, 展现跳过按钮实现倒计时功能, 并判断广告页面是否更新. 详细 代码下载:http://www.demodashi.com/demo/10698.html 目前市场上很多APP(如淘 ...

  8. document.body、document.documentElement和window获取视窗大小的差别

    来源:http://www.ido321.com/906.html 在w3school关于window对象的介绍中,介绍了获取浏览器窗体大小的三种方法(浏览器的视口,不包含工具栏和滚动栏). 对于In ...

  9. mybatis对mysql进行分页

    Mybatis对mysql数据库分页 在generator中增加插件,下载地址http://download.csdn.net/detail/shunlongjin/6937045 <plugi ...

  10. 照片管家iOS-实现本地相册、视频、安全保护、社交分享源码下载Demo

    <照片管家> APP功能: 1.本地照片批量导入与编辑 2.本地视频存储与播放 3.手势密码.数字密码.TouchID安全保护 4.QQ.微信.微博.空间社交分享 5.其他细节功能. 运用 ...