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

题意:给出一个 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. 使用Ansible安装部署nginx+php+mysql之安装php(2)

    二.使用Ansible安装php 1.php.yaml文件内容 - hosts: clong remote_user: root gather_facts: no tasks: # 安装libseli ...

  2. xunsearch实战经验总结

    一.定义好配置文件(非常关键) a):如果需要做精确搜索建议对字段设定index=self,tokenizer = full,不然xunsearch会对字段做分词处理: b):数字区间搜索需设定 ty ...

  3. 01.Python基础-2.判断语句和循环语句

    1判断语句 1.1判断语句介绍 满足条件才能做某件事 1.2 if语句 if 条件: 语句块 在if判断条件的时候 False:False, 0, '', None, [] True :基本除上面之外 ...

  4. @PostConstruct 和 @PreDestroy 指定初始化和销毁方法

    通过实现 @PostConstruct 和 @PreDestroy 注解,也可以指定 bean 的初始化和销毁方法 一.Student 类 public class Student{ public S ...

  5. 参数化取值策略Sequential

    1.Sequential+Each iteration(顺序方式+每次迭代更新取值),设置Run—Logic中action循环迭代11次,并运行以上脚本,结果如下:     2.Sequential+ ...

  6. ES scroll(ES游标) 解决深分页

    ES scroll(ES游标) 解决深分页. Why 当Elasticsearch响应请求时,它必须确定docs的顺序,排列响应结果.如果请求的页数较少(假设每页20个docs), Elasticse ...

  7. CF922B Magic Forest

    CF922B Magic Forest 题意翻译 题目大意 给定一个正整数nn ,求满足如下条件的三元组(a,b,c)(a,b,c) 的个数: 1 \le a \le b \le c \le n1≤a ...

  8. 关于工作,学习中定时备份的几个方法(cron,git,mail)

    首先介绍一下cron这个定时备份的工具: crontab -e : 运行文字编辑器来设定时程表,内定的文字编辑器是 VI.假设你想用别的文字编辑器.则请先设定 VISUAL 环境变数来指定使用那个文字 ...

  9. 怎样改动 VC6.0 4.0 2010 打印预览界面上的文字

    前言:尽管早已下决心不再碰微软的东西,但手头的项目还得须要维护. 遇到问题还能解决. 问题由来: 之前的软件开发是基于中文环境开发的,建立项目的时候,选择了中文,为了方便客户使用.我们使用了静态编译的 ...

  10. POJ 1236 Network of Schools 强连通图

    Description A number of schools are connected to a computer network. Agreements have been developed ...