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. 使用轻量级JAVA 爬虫Gecco工具抓取新闻DEMO

    写在前面 最近看到Gecoo爬虫工具,感觉比较简单好用,所有写个DEMO测试一下,抓取网站 http://zj.zjol.com.cn/home.html,主要抓取新闻的标题和发布时间做为抓取测试对象 ...

  2. JVM:垃圾回收机制和调优手段

    转载请注明出处: jiq•钦's technical Blog - 季义钦 引言: 我们都知道JVM内存由几个部分组成:堆.方法区.栈.程序计数器.本地方法栈 JVM垃圾回收仅针对公共内存区域即:堆和 ...

  3. 解决java.lang.OutOfMemoryError: unable to create new native thread问题

    解决:1.升级JVM到最新的版本 最新版本的JVM一般在内存优化方面做的更好,升级JVM到最新的版本可能会缓解测问题2.从操作系统层面去解决 使用64位操作系统 如果使用32位操作系统遇到unable ...

  4. 解决java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'

    今天使用mysql,用java程序往mysql中写数据,出现如下错误: Caused by: java.sql.SQLException: Incorrect string value: '\xD3\ ...

  5. 如何解决Cydia提示错误

    删除/etc/apt/sources.list.d该目录下的cydia.list进入var/lib/apt/lists删掉此文件夹下的所有文件,以及partial下的文件,但须保留这个空文件夹.进入/ ...

  6. Python之上下文管理器

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之上下文管理器 #http://python.jobbole.com/82620/ #语法形式: ...

  7. js 获取地址栏的值乱码问题

    传过去的参数是:(01) 0 6936841 40029 4. 接收的参数的:'(01)%200%206936841%2040029%204'. 因为包含空格,或者中文,就会乱码.要想不乱码,接收的使 ...

  8. SpringMVC 类内部的RequestMapping注解能否被继承?

    首先注意标题,说的是类内部的注解 结论是: 不能,但是子类却可以享有父类中该注解带来的效果. 看了一下这个:http://elf8848.iteye.com/blog/1621392 自己也试了一下, ...

  9. python第三方包的windows安装文件exe格式

    今天弄了一上午的python-ldap,发现要么安装vc,要么用其他比较麻烦的方法,都比较麻烦.幸好找到这个地址: http://www.lfd.uci.edu/~gohlke/pythonlibs/ ...

  10. 【redis】常用命令

    三.常用命令    1)连接操作命令    quit:关闭连接(connection)    auth:简单密码认证    help cmd: 查看cmd帮助,例如:help quit         ...