转载请注明出处: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

Problem Description
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will
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?
 
Input
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of file.
 
Output
For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.
 
Sample Input
6 4
11 8
 
Sample Output
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
 
Author
Ignatius.L
 

题目: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的更多相关文章

  1. ACM-简单题之Ignatius and the Princess II——hdu1027

    转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...

  2. hdu1027 Ignatius and the Princess II (全排列 &amp; STL中的神器)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...

  3. (全排列)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 ...

  4. 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 ...

  5. HDU 1027 Ignatius and the Princess II(康托逆展开)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  6. Ignatius and the Princess II(全排列)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  7. 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 ( ...

  8. Ignatius and the Princess II

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  9. (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 ( ...

随机推荐

  1. 安装Oracle时可能碰到的常见问题-1

    安装Oracle可能有些人觉得是一件非常easy的事情,但事实上是在安装的过程中蕴含着丰富的知识点.尤其安装在Linux平台,可能会碰到这样或那样各种诡异的问题,透过问题看到本质,这才是从深处理解Or ...

  2. Android开源框架AsyncHttpClient (android-async-http)使用

    android-async-http 开源框架可以使我们轻松地获取网络数据或者向服务器发送数据,最关键的是,它是异步框架,在底层使用线程池处理并发请求,效率很高,使用又特别简单. 以往我们在安卓上做项 ...

  3. Problem J: Island Buses

    主要题意是:大海之间有岛,有的岛之间有桥,问你岛的个数,桥的个数,以及没有桥联通岛的个数,其中最后一次输入的没有回车,不注意的话最后一次会被吞,第二,桥的两端的标记是“X”(X也代表陆地),“X”的四 ...

  4. uva 699

    #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #i ...

  5. SDUTOJ 1489 求二叉树的先序遍历

    <img src="http://img.blog.csdn.net/20141014212549703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...

  6. c++ anonymous namespace -- 匿名空间

    c++ anonymous namespace -- 匿名空间 匿名空间,匿名类,匿名联合体,匿名结构体.   匿名空间   #include <stdio.h> namespace A ...

  7. CF 327D - Block Tower 数学题 DFS 初看很难,想通了就感觉很简单

    D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性加入图标

    注:(图中每个条目和图标都是由代码动态生成) 代码动态布局,并须要为每个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon"  父x ...

  9. 编写自己的单点登录(SSO)服务

    王昱 yuwang881@gmail.com   博客地址http://yuwang881.blog.sohu.com 摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统其中.本文从 ...

  10. latex表格线的颜色设置(边框添加颜色)

    添加了如下包:边框颜色要用到booktabs, colortbl, 包,下面代码里有一个自定义的颜色tabcolor \usepackage{ctexcap} \usepackage{graphicx ...