**链接:****传送门 **

题意:给出一个 n ,求 1 ~ n 全排列的第 m 个排列情况

思路:经典逆康托展开,需要注意的时要在原来逆康托展开的模板上改动一些地方。

  • 分析:已知 1 <= M <= 10000,10000 < 8!,根据逆康托展开的原理可以发现,A[n] * (n-1)! + A[n-1] * (n-2)! + A[n-2] * (n-3)! + ...... + A[2] * 1! + A[1] * 0! ,在前 n - 8 项之前,Ai == 0,所以每次都是取剩余排列中第 0 个最大元素,也就是 0 1 2 3 ... n - 9( 从0开始 ),后面的项直接按照逆康托计算得到。

/*************************************************************************
> File Name: hdu1027.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月18日 星期四 16时13分40秒
************************************************************************/ #include<bits/stdc++.h>
using namespace std; #define ll long long
#define cls(x) memset(x,0,sizeof(x))
const int MAX_N = 1010; // 排列长度
const int MAX_C = 9; // 需要的最大阶乘N!
ll fac[MAX_C]; // 初始化阶乘系数
void init_fac(){
fac[1] = fac[0] = 1;
for(int i = 2 ; i < MAX_C ; i ++) fac[i] = fac[i-1]*(ll)i;
}
// 寻找由1~n组成全排列按字典序排序后第x个排列
void uCT(int n,int x){
bool vis[MAX_N]; cls(vis);
int ans[MAX_N]; cls(ans);
x--;
int i , j;
for(i = 0 ; i < n ; i++){
if( i >= n - 8 ){
int t = x/fac[n-i-1]; // 每次都寻找第t大的数
for(j = 0 ; j < n ; j++){
if(!vis[j]){
if( t == 0 ) break;
t -- ;
}
}
ans[i] = j;
vis[j] = 1;
x %= fac[n-i-1];
}
else{
ans[i] = i;
vis[i] = 1;
}
}
for(i = 0 ; i < n-1 ; i++) printf("%d ",ans[i] + 1);
printf("%d\n",ans[n-1]+1);
} int main(){
int n , x;
init_fac();
while(~scanf("%d%d",&n,&x)){
uCT(n,x);
}
return 0;
}

HDU1027 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. HDU1027 Ignatius and the Princess II 【next_permutation】【DFS】

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

  3. hdu1027 Ignatius and the Princess II (全排列 &amp; STL中的神器)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...

  4. hdoj-1027-Ignatius and the Princess II(逆康拓展开)

    题目链接 /* Name: Copyright: Author: Date: 2018/5/2 11:07:16 Description:输出第m小的序列 */ #include <iostre ...

  5. HDU1027 Ignatius and the Princess II

    Problem Description Now our hero finds the door to the BEelzebub feng5166. He opens the door and fin ...

  6. hdoj 1027 Ignatius and the Princess II 【逆康托展开】

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

  7. ACM-简单题之Ignatius and the Princess II——hdu1027

    转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...

  8. ACM-简单的主题Ignatius and the Princess II——hdu1027

    转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...

  9. Ignatius and the Princess II(全排列)

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

随机推荐

  1. Centos 搭建activemq

    Centos 搭建activemq 1,官方下载  http://activemq.apache.org/activemq-5122-release.html apache-activemq-5.15 ...

  2. webpack学习笔记(2)--webpack.config.js

    3 模式 mode mode 参数设置为 development(开发模式), production(生产模式) 或 none(无),可以启用对应环境下 webpack 内置的优化.默认值为 prod ...

  3. 谈谈python里面关于任务队列

    谈谈python里面关于任务队列 为什么要做任务队列 要回答这个问题我们首先看看在流水线上的案列,如果人的速度很慢,机器的速度比人的速度快很多,就会造成,机器生产的东西没有及时处理,越积越多,造成阻塞 ...

  4. OA项目总结3

    struts2自定义标签中  使用in  判断当前值 是否在某个集合中 该属性一方面可以获取前端页面传递过来的参数   另外一个作用就是在数据回显时把用户已经拥有的权限id存入该集合中   放在栈顶 ...

  5. (5)全局异常捕捉【从零开始学Spring Boot】

    在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 新建一个类GlobalDefaultExceptionHandler, 在class注解上@ControllerAdvice ...

  6. Mysql提升大数据导入速度的绝妙方法

    一.对于Myisam类型的表,可以通过以下方式快速的导入大量的数据.      ALTER TABLE tblname DISABLE KEYS;     loading the data     A ...

  7. UVA - 1642 Magical GCD 数学

                                  Magical GCD The Magical GCD of a nonempty sequence of positive integer ...

  8. 在单机上安装多个oracle实例

    1   在 hp unix上安装 oracle 10g ,这个不解释,直接安装好. 创建组oinstall,dba,用户oracle  [root@node1 ~]# groupadd oinstal ...

  9. 杂项-Java:Spring

    ylbtech-杂项-Java:Spring Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于200 ...

  10. PHP中出现Notice: Undefined index的三种解决办法

    前一段做的一个PHP程序在服务器运行正常,被别人拿到本机测试的时候总是出现“Notice: Undefined index:”这样的警告,这只是一个因为PHP版本不同而产生的警告(NOTICE或者WA ...