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. NI License Activator 用法

    双击打开后,看到这种界面,将白色方格通过鼠标右击点绿就能够了. NI <wbr>License <wbr>Activator <wbr>用法 可能会出现这样的情况, ...

  2. Eclipse Console 加大显示的行数和禁止错误弹出

    在 Preferences-〉Run/Debug-〉Console里边,去掉对Limit console output的选择,或者选择,设置一下buffer size的设定值 禁止弹出: Prefer ...

  3. 〖Linux〗Ubuntu13.04解决Chrome的flash中文乱码的问题。

    1. 安装flash sudo aptitude install flashplugin-installer 2. 禁用chrome自带的flash插件 在chrome浏览器中输入 chrome:// ...

  4. js getAttribute getAttributeNode

    getAttribute():返回属性值,是一个文本字符串 getAttributeNode("属性名"):返回属性节点,是一个对象 <p id="bj" ...

  5. js 排序

    在本例中,我们将创建一个数组,并按字母顺序进行排序: <script type="text/javascript"> var arr = new Array(6) ar ...

  6. SAP接口设计的扩展性考虑

            由于现在的系统和SAP的接口出现了几次变更,因此需要对系统进行设计改造.由于系统中和SAP交互的接口不止一处,而且也是在不同的时间段进行开发,并由不同的人员来完成的,因此我在维护升级的 ...

  7. @Html.Display @Html.LabelFor @Html.EditorFor Html.DisplayForModel Html.LabelForModel Html.EditorForModel

  8. Jmeter----HTTP Request Defaults

    一.HTTP Request Defaults的作用: 该组件可以为我们的http请求设置默认的值.假如,我们创建一个测试计划有很多个请求且都是发送到相同的server,这时我们只需添加一个Http ...

  9. Win8.1设置ftp服务器并设定用户操作权限的详细教程

    http://wenku.baidu.com/link?url=VTDLnDa_yfQN9OldjVnYsOBf7UdIj76QjaLDyHP-I0A6iFEfzB8EyBf9uztwm2JDXlFL ...

  10. fork函数相关总结

    fork的作用是根据一个现有的进程复制出一个新进程,原来的进程称为父进程(Parent Process),新进程称为子进程(Child Process).系统中同时运行着很多进程,这些进程都是从最初只 ...