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. WebService 之 实例学习一

    新建一个空网站项目,添加新建项 “ Web 服务 ”. 一.WebServiceDemo.asmx 文件,默认内容如下: <%@ WebService Language="C#&quo ...

  2. JavaScript 之 变量

    一:作用域 说起变量第一个要说到的肯定就是作用域,正是因为不熟悉JS的 作用域,往往就会把面向对象的作用域张冠李戴,毕竟有些东西总是习惯性的这样,但是并不是每次照搬都是可以的,那么下一个问题就来了,j ...

  3. Adobe Dynamic Http Streaming的简单配置与实现 (FMS, HLS, HDS)

    http://blog.csdn.net/avsuper/article/details/7663879 Adobe的Http Dynamic Streaming是针对苹果的HLS方案提出基于HTTP ...

  4. Starting MySQL.. ERROR! The server quit without updating PID file (/gechong/mysqldata/10-9-23-119.pid).

    配置文件修改错误了,还原一下重启服务就OK了. # /etc/init.d/mysql start

  5. javaScriptObject转String

    function obj2str(o){ var r = []; if(typeof o =="string") return "\""+o.repl ...

  6. 解决Eclipse的Servers视图中无法添加Tomcat6/Tomcat7

    如何解决 . 关闭Eclipse . 打开WorkSpace所在的位置. {workspace-directory}/.metadata/.plugins/org.eclipse.core.runti ...

  7. JProfiler_SN_8_x key

    按默认选择“Single or evaluation license”Name 和 Company 随意-----------------------忧郁的分割线------------------- ...

  8. urlparse模块(专门用来解析URL格式)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #urlparse模块(专门用来解析URL格式) #URL格式: #protocol ://hostname[ ...

  9. 从 高斯 到 正态分布 到 Z分布 到 t分布

    正态分布是如何被高斯推导出来的, 我感觉高斯更像是猜出了正态分布. 详见这篇文章:<正态分布的前世今生> http://songshuhui.NET/archives/76501 说一说理 ...

  10. Persona——Web人物角色介绍

    一.什么是人物角色? 人物角色,即persona([pə:’səunə]),这里讨论的主要是web persona,是指针对网站目标群体真实特征的勾勒,是真实用户的综合原型.我们对产品使用者的目标.行 ...