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

题意:给出一个 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. XSS Chanllenges 6-10

    Stage #6 测试代码</xss> 存在过滤,并且也没有其他输入点,尝试构建" onmousemove="alert(document.domain),并查看源代码 ...

  2. nyoj125-盗梦空间

    盗梦空间 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 <盗梦空间>是一部精彩的影片,在这部电影里,Cobb等人可以进入梦境之中,梦境里的时间会比现实中的时 ...

  3. 园区IP地址规划(非常详细)

    转:https://mp.weixin.qq.com/s/Zlm7x5eunIYLAG7Sp0yVCQ 经过这些年工作,接触从几万.几十万到上亿的项目都有: 我简单总结了接触的大部分的项目,将园区网核 ...

  4. PHP 中 call_user_func 的使用

    call_user_func函数类似于一种特别的调用函数的方法,使用方法如下 第一种情况: function set_max($a,$b) { if($a>$b) echo $a; else e ...

  5. 基于ALSA的WAV播放和录音程序

    http://blog.csdn.net/azloong/article/details/6140824 这段时间在探索ALSA架构,从ALSA Core到ALSA Lib,再到Android Aud ...

  6. Git clone时出现Please make sure you have the correct access rights and the repository exists.问题已解决。

    看了好多资料终于搞定了git 中clone命令报错这个问题,废话不多说直接上步骤希望对大家有帮助. 1   删除.ssh文件夹(直接搜索该文件夹)下的known_hosts(手动删除即可,不需要git ...

  7. swiper.js在隐藏/显示切换时,轮播出现bug的解决办法

    swiper在 swiper-container正常状态下显示,轮播是没有问题,但是当 swiper-container由隐藏切换至显示时(比如做图片查看时)会出现滑动bug,滑动卡顿而且最后一张可以 ...

  8. vue+Ueditor集成 [前后端分离项目][图片、文件上传][富文本编辑]

    后端DEMO:https://github.com/coderliguoqing/UeditorSpringboot 前端DEMO:https://github.com/coderliguoqing/ ...

  9. switch 的穿透, 以及穿透利用

    switch 穿透测试: outputs: 添加break 阻止switch穿透: outputs: 利用switch的穿透功能:

  10. NEFU 109

    n最大为2000000000(不知为什么OJ上是1000),若为判断2000000000是素数,则必有一个素数在sqrt(n)内,求出这个范围 的所有素数,其比最大数据小的n'的sqrt(n')也在这 ...