HDU 1027 Ignatius and the Princess II(康托逆展开)
Ignatius and the Princess II
"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?
11 8
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; int a[] = {, , , , , , , , };
int vis[]; int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(vis, , sizeof(vis));
m--;
int temp = ;
while(temp<n)
{
if((n-temp)<=)
{
int k = m/a[n-temp];
int mod = m%a[n-temp];
int count = ;
for(int i=; i<=n; i++)
{
if(!vis[i]) ++count;
if((count-)==k)
{
printf("%d ", i);
vis[i] = ; break;
}
}
m = mod;
}
else
{
for(int i=; i<=n; i++)
{
if(!vis[i] == )
{
vis[i] = ;
printf("%d ", i); break;
}
}
}
++temp;
}
for(int i=; i<=n; i++)
if(!vis[i]) printf("%d\n", i);
}
return ;
}
#include <stdio.h>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std; const int maxn = ;
int a[maxn];
int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
for(int i=; i<=n; i++) a[i] = i;
int num = ;
while(num<m)
{
next_permutation(a+, a++n);
num++;
}
for(int i=; i<n; i++)
printf("%d ", a[i]);
printf("%d\n", a[n]);
}
return ;
}
HDU 1027 Ignatius and the Princess II(康托逆展开)的更多相关文章
- 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 ( ...
- hdoj 1027 Ignatius and the Princess II 【逆康托展开】
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- hdu 1027 Ignatius and the Princess II(正、逆康托)
题意: 给N和M. 输出1,2,...,N的第M大全排列. 思路: 将M逆康托,求出a1,a2,...aN. 看代码. 代码: int const MAXM=10000; int fac[15]; i ...
- HDU 1027 Ignatius and the Princess II 选择序列题解
直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS. 使用删除优化,那么就能够达到0MS了. 删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针 ...
- HDU 1027 - Ignatius and the Princess II
第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...
- HDU 1027 Ignatius and the Princess II 排列生成
解题报告:1-n这n个数,有n!中不同的排列,将这n!个数列按照字典序排序,输出第m个数列. 第一次TLE了,没注意到题目上的n和m的范围,n的范围是小于1000的,然后m的范围是小于10000的,很 ...
- hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)
题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...
随机推荐
- href="#"会导致location.replace(location.href);脚本不工作
我们经常这样:<a onclick="xxx" href="#" 其实这不是一个好习惯,当点下这个连接后,主页面的URL后面会加个#号,这样就会导致很多J ...
- linux命令:du 命令
Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...
- SpringMVC 接收ajax发送的数组对象
本文粘贴自:http://my.oschina.net/jiefalcon/blog/384153?fromerr=24Lewn46 [转]SpringMVC @RequestBody接收Json对象 ...
- Prince2的七大原则(5)
[Prince2科普]Prince2的七大原则(5) 按照惯例我们先来回顾一下,PRINCE2七大原则分别是指:持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 第五个原则 ...
- 关于数据库表中的索引及索引列的CRUD
-- 查询一个数据库表中的索引及索引列use [RuPengWangDB]GOSELECT indexname = a.name , tablename = c. name , indexcolu ...
- Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏
Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...
- wpf的研究和反思
WPF的研究和反思 目前是否适合使用wpf WPF(Windows Presentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET F ...
- JAVA基础知识之JVM-——自定义类加载器
JVM中除了根加载器之外其他加载器都是ClassLoader的子类实例, 可以通过扩展ClassLoader的子类,通过重写方法来实现自定义的类加载器. ClassLoader中有两个关键的方法如下, ...
- hiho(1081),SPFA最短路,(非主流写法)
题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...
- apache-common pool的使用
Apache commons-pool本质上是"对象池",即通过一定的规则来维护对象集合的容器;commos-pool在很多场景中,用来实现"连接池"/&quo ...