ACM-简单题之Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree
Ignatius and the Princess II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4436 Accepted Submission(s): 2642
release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."
"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once
in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
6 4
11 8
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
题目: pid=1027">http://acm.hdu.edu.cn/showproblem.php?pid=1027
这道题,题意就是求 N的第m种排列。
应该属于组合数学中的一种。刚好之前做过康托展开,就感觉能够用康托展开来做。
(康拓展开详情可戳→http://blog.csdn.net/lttree/article/details/24798653)
可是,我看了看数据范围就被吓到了, N and M(1<=N<=1000, 1<=M<=10000)。
阶乘,最多仅仅是10。怎么N能够到1000.。。。
细致一想就能够发现,M最大为10000。也就是说。最多也就仅仅有后面8个数才会动。前面不会动的。
由于1~8的阶乘为:1,2,6,24,120,720,5040,40320.
8的阶乘为40320>10000 10000种以内的排列序,仅仅能在最后8个变化。
换种说法,不管N为多少,当N>8时,前N-8是不变的。仅仅有后面8个在变化。
比如:N为11,那么前面3个数为1 2 3顺序一定不变。变化的永远是后面4~11
思路想出来后。解决这道题就不算太难。
本来我用的是,边算遍输出。可是总是PE,可能还是有地方没想到吧。
就直接将答案存在一个ans数组里。最后一起输出,就AC了。
/****************************************
*****************************************
* Author:Tree *
*From :http://blog.csdn.net/lttree *
* Title : Ignatius and the Princess II *
*Source: hdu 1027 *
* Hint : 康托展开 *
*****************************************
****************************************/ #include <iostream>
using namespace std;
int fac[]={1,1,2,6,24,120,720,5040,40320};
// 存储答案
int ans[10001],len;
// 康托展开的逆 n为要对几位数排序,k为第几个数,num为这n个数应该从多少開始
void reverse_kangtuo(int n,int k,int num)
{
int i, j, t, vst[11]={0};
char s[11]; --k;
for (i=0; i<n; i++)
{
t = k/fac[n-i-1];
for (j=1; j<=n; j++)
if (!vst[j])
{
if (t == 0) break;
--t;
}
s[i] = '0'+j;
vst[j] = 1;
k %= fac[n-i-1];
}
// 排序后的赋给答案数组
for(int kk=0;kk<n;++kk)
ans[len++]=s[kk]-'1'+num;
} int main()
{
int n,m;
int i,j,temp1,temp2;
while( cin>>n>>m )
{
i=1;
len=0;
if( n>8 )
{
temp1=n%8;
temp2=(n/8-1)*8;
// 对应答案赋值
for(;i<=temp1;++i)
ans[len++]=i;
for(j=0;j<temp2;++j,++i)
ans[len++]=i;
reverse_kangtuo(8,m,i);
}
else reverse_kangtuo(n,m,i); // 输出,注意最后一个数后面没有空格
for(i=0;i<len-1;++i)
cout<<ans[i]<<" ";
cout<<ans[len-1]<<endl;
}
return 0;
}
ACM-简单题之Ignatius and the Princess II——hdu1027的更多相关文章
- ACM-简单的主题Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...
- Ignatius and the Princess II(全排列)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- (全排列)Ignatius and the Princess II -- HDU -- 1027
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/100 ...
- HDU 1027 Ignatius and the Princess II(求第m个全排列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/10 ...
- 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 ( ...
- Ignatius and the Princess II
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- (next_permutation)Ignatius and the Princess II hdu102
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
随机推荐
- QiniuUpload- 一个方便用七牛做图床然后插入markdown的小工具
最近一段时间有用markdown做笔记,其他都好,但是markdown插入图片挺麻烦的,特别是想截图之后直接插入的时候.需要首先把图片保存了,然后还要上传到一个地方生成链接才能插入.如果有个工具可以直 ...
- 转载:android——eclipse如何去除Ctrl+shift+R组合键查找到的.class文件
转载自:http://blog.csdn.net/virgilli/article/details/22500409 AS里面的build文件下一堆的.class 文件,当你要定位资源文件的时候,有些 ...
- JavaScript 函数方法 - toString()
Function.prototype.toString() 返回函数代码的字符串形式. 描述 Function 对象覆盖了从 Object 继承来的 Object.prototype.toString ...
- 如何在pl/sql developer 7运行到oracle存储过程设置断点的地方
如何高效调试oracle存储过程,尤其是父子网状调用的存储过程 1,在需要设置断点的oracle存储过程处设置断点 如何设置断点:直接在某行oracle存储过程处单击行首,会在行首显示 ...
- IL(Intermediate Language)
释义: IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不是CPU ...
- ios文件读取
/* * @brief 沙盒下的各种文件 */ - (void)getPaths { /** * @brief 获取沙盒的路径 */ NSString * HomeDirectory = NSHo ...
- The Love-Letter Mystery
def numOperations(testStr): num = 0 n = len(testStr) for i in range(n / 2): num += abs(ord(testStr[i ...
- 【7】使用css/js/html模板来实现一个注册、登录和管理的功能
分支:auth static添加文件 css文件夹: app.css 自定义css样式[*] bootstrap.min.cs bootstrap样式 compomemts文件夹: 插件用 ...
- python文件批量改名
python对文件进行批量改名用到的是os模块中的listdir方法和rename方法. os.listdir(dir) :获取指定目录下的所有子目录和文件名 os.rename(原文件名,新文件名 ...
- 树莓派 wheezy安装与远程登录配置
一.准备工作 1:wheezy系统镜像 2:Win32DiskImager-0.9.5-install 3:SDFormatter 4:SD卡 我用的是8G 二.安装系统 流程:SDFormatter ...