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. selenium 加载jquery

    packagecom.example.tests; import staticorg.junit.Assert.*; importjava.util.*; importorg.junit.*; imp ...

  2. TP框架中APP_SUB_DOMAIN_DEPLOY什么意思?

    'APP_SUB_DOMAIN_DEPLOY' => false, // 是否开启子域名部署 thinkphp开启域名部署/子域名部署/泛域名部署/IP访问部署            Think ...

  3. 关于SimHash去重原理的理解(能力工场小马哥)

    阅读目录 1. SimHash与传统hash函数的区别 2. SimHash算法思想 3. SimHash流程实现 4. SimHash签名距离计算 5. SimHash存储和索引 6. SimHas ...

  4. SettingsEclipse

      迁移时间--2017年5月20日08:45:07 CreateTime--2016年11月15日11:07:44Author:Marydon --------------------------- ...

  5. ORA-27090 故障一例

    近期的alert日志中碰到了ORA-27090的错误信息.其错误提示为Unable to reserve kernel resources for asynchronous disk I/O.依据这个 ...

  6. memcached 下载安装

    wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x.x ./configure &am ...

  7. 全球免费知名DNS服务器

    全球免费知名DNS服务器 jalone 2013-06-18 14:25:46 最近老是发表DNS相关文章,今天继续说DNS,国内75%以上的家用宽带路由器存在严重的安全隐患:用户浏览网页的时候其DN ...

  8. nnCron LITE

    nnCron LITE is a small, but full-featured scheduler that can start applications and open documents a ...

  9. Linux密钥认证错误解决

    问题描述: Xshell用key认证登录,提示所选的用户密钥未在远程主机上注册 问题解决: 查看日志/var/log/secure,基本上都是用户根目录的权限问题 根据日志提示: Authentica ...

  10. 关于iOS 类扩展Extension的进一步理解

    很多人可能会问  iOS的分类和扩展的区别,网上很多的讲解,但是一般都是分类讲的多,而这也是我们平常比较常用的知识:但是,对于扩展,总觉得理解的朦朦胧胧,不够透彻. 这里就讲一下我自己的理解,但是这个 ...