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> ...
随机推荐
- linux下rm命令修改,增加回收站功能【笔记】
一个脚本,linux的用户根目录下.bashrc最后加入如下代码,可以修改rm命令,让人们rm时候不再会全部删除,而是会加入到回收站里,以下是根据别人的资料参考修改的,不是原创 加入后,需要sourc ...
- oracle 自动关闭 数据库连接
数据库启动后过一会自动断开连接(大概10分钟) 方法一.直接修改资源配置文件 分三个步骤在sqlplus环境下完成. 第一步,查询资源文件,找到CONNECT_TIME所在的pr ...
- Web Token JWT
基于Token的WEB后台认证机制 JSON Web Token in ASP.NET Web API 2 using Owin 翻译:Token Authentication in ASP.NET ...
- 20145227《Java程序设计》第2次实验报告
20145227<Java程序设计>第2次实验报告 实验步骤与内容 一.实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 ...
- java面试每日一题13
题目:有一个分数数列2/1.3/2.5/3.8/5.13/8........求出这个数列的前20项之和.运行结果如下32.660263 public class Page80 { /** * * @p ...
- AR专用汉明码
增强现实技术(Augmented Reality,简称 AR),是一种实时地计算摄影机影像的位置及角度并加上相应图像.视频.3D模型的技术,这种技术的目标是在屏幕上把虚拟世界套在现实世界并进行互动. ...
- 关于hibernate对应关系之后取值的问题
hibernate对应关系之后取值,比如一对一关系,取不到值,需要检查PO类中是否生成了getter及setter方法.
- layer
一款实用的web提示框架!下面是实用这款框架的实例 html代码 <p class='layer_notice'>我们是否是好惹的</p> <input type=&qu ...
- Bellman-Ford 算法及其优化
Bellman-Ford 算法及其优化 转自:http://hi.baidu.com/jzlikewei/blog/item/94db7950f96f995a1038c2cd.html Bellman ...
- c# 服务端
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...