HDU1027 Ignatius and the Princess II( 逆康托展开 )
**链接:****传送门 **
题意:给出一个 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( 逆康托展开 )的更多相关文章
- HDU 1027 Ignatius and the Princess II(康托逆展开)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 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 ( ...
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...
- hdoj-1027-Ignatius and the Princess II(逆康拓展开)
题目链接 /* Name: Copyright: Author: Date: 2018/5/2 11:07:16 Description:输出第m小的序列 */ #include <iostre ...
- HDU1027 Ignatius and the Princess II
Problem Description Now our hero finds the door to the BEelzebub feng5166. He opens the door and fin ...
- hdoj 1027 Ignatius and the Princess II 【逆康托展开】
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- ACM-简单题之Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- ACM-简单的主题Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- Ignatius and the Princess II(全排列)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
随机推荐
- geohash:用字符串实现附近地点搜索
转自:http://blog.charlee.li/geohash-intro/ geohash:用字符串实现附近地点搜索 上回说到了用经纬度范围实现附近地点搜索.一些小型应用中这样做没问题,但在大型 ...
- nyoj314-斐波那契数列四吧
斐波那契数列四吧 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 斐波那契数列为:0,1,1,2,3,5,8,13....,常规递推公式为f(n)=f(n-1)+f(n- ...
- vue项目优化--使用CDN和Gzip
使用vue-cli构建的vue项目,在打包发布的时候,发现打包后的文件体积很大,使用webpack-bundle-analyzer分析后,发现占用空间最多的是引用的第三方依赖.第三方的依赖文件可以使用 ...
- 【UOJ#50】【UR #3】链式反应(分治FFT,动态规划)
[UOJ#50][UR #3]链式反应(分治FFT,动态规划) 题面 UOJ 题解 首先把题目意思捋一捋,大概就是有\(n\)个节点的一棵树,父亲的编号大于儿子. 满足一个点的儿子有\(2+c\)个, ...
- 洛谷—— P1457 城堡 The Castle
https://www.luogu.org/problem/show?pid=1457 题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特 ...
- [ASP.NET]EF选一个空表的情况
public List<DAL.StuFillAnswers> FillsToCheck(int sid,int eid) { using (DAL.ExamSysEntities db ...
- 韩国IT业是怎么走向国际我们须要学习什么
无论从国土面积仍是从人口数量上来衡量.韩国都不能算是一个大国,而且自然资本十分缺乏,即是在这种情况下,韩国经过几十年的尽力开展变成技能大国,格外是在IT这种新经济范畴更是引人注目.并诞生了三星等国际级 ...
- Linux下将PHP添加到环境变量,将Mysql加入环境变量。
1.修改/etc/profile vi /etc/profile 2.添加两行 PATH=$PATH:/usr/local/php7/bin:/usr/local/mysql/bin export P ...
- php利用反射真正实现多继承(非接口模拟)
昨天我在写PHP程序的时候,无意发现在PHP的::操作符非常强大,不仅仅是只用在访问parent,sel,静态成员属性.常量上面,其实他的功能强大了去了 . 这个符号在PHP中实际上叫做范围解析符,这 ...
- Modules:手机号码验证
ylbtech-Modules:手机号码验证 手机号码验证,文档以JFB项目架构为原型,介绍实现原理,如何调用和应用实例. 架构包括5个主要模块:Basebase,Base,Service,Api和W ...