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 ( ...
随机推荐
- Linux 内核管理
Linux内核基础:Linux Kernel: Linux内核的体积结构是单内核的,但充分借鉴了微内核设计体系的优点,为内核引入模块化机制,使得虽然是单内核,但工作在模块化的方式下,并且模块可以动态 ...
- 《代码敲不队》第八次团队作业:Alpha冲刺 第一天
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 代码敲不队 作业学习目标 掌握软件编码实现的工程要求. 第一天 日期:2019/6/15 团队项目 ...
- 提高生产力:Web开发基础平台WebCommon的设计和实现
Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...
- bilibili用户信息查询
bilibili用户信息查询 http://space.bilibili.com/ajax/member/GetInfo?mid= 后缀为用户mid号 # -*- coding:utf-8 -*- # ...
- 在本地生成ssh-key 免密码远程clone GitLab中的项目到本地
每次项目push.pull都需要输入账号和密码,很烦,方便免密pull与push代码,在本地需要用git bash 创建一个公钥,然后在gitlab中把公钥保存下来. 步骤如下: 1.打开 git b ...
- CF786A - Berzerk
/* CF786A - Berzerk http://codeforces.com/contest/786/problem/A 博弈论 直接搜出NP状态图.记得要记忆化剪枝. * */ #includ ...
- ASP.NET - 单元测试
Assert类的使用 Assert.Inconclusive() 表示一个未验证的测试: Assert.AreEqual() 测试指定的值是否相等,如果相等,则测试通过: AreSame() 用于验证 ...
- [BZOJ 3884][欧拉定理]上帝与集合的正确使用方法
看看我们机房某畸形写的题解:http://blog.csdn.net/sinat_27410769/article/details/46754209 此题为popoQQQ神犇所出,在此orz #inc ...
- BZOJ 2124: 等差子序列 线段树维护hash
2124: 等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一 ...
- Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP
E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coinci ...