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设备驱动归纳总结(四):2.进程调度的相关概念【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-65555.html linux设备驱动归纳总结(四):2.进程调度的相关概念 xxxxxxxxxxxx ...
- TreeNode动态邦定事件
1. 显示不能用TextField,应该用StaticText 2. public void SetNodesAction(Tree t, String methodbind){ ArrayList ...
- peak num
class Solution {public: int findPeakElement(vector<int>& nums) { int i=0; int n=nums ...
- href="#"会导致location.replace(location.href);脚本不工作
我们经常这样:<a onclick="xxx" href="#" 其实这不是一个好习惯,当点下这个连接后,主页面的URL后面会加个#号,这样就会导致很多J ...
- 使用MeanJS Yeoman Generator
1.首先全局安装该生成器 sudo npm install -g generator-meanjs 2.为项目创建一个路径 mkdir xmen && cd xmen 3.创建app ...
- 11、Jsp加强/EL表达式/jsp标签
1 Jsp基础回顾 Jsp基础 1)Jsp的执行过程 tomcat服务器完成:jsp文件->翻译成java文件->编译成class字节码文件-> 构造类对象-> 调用方法 to ...
- Python查看函数代码内容
方法1:使用help(random) >>> import random >>> help(random) Help on module random: NAME ...
- python :列表 字典 集合 类 ----局部变量可以改全局变量
#列表 字典 集合 类 ----局部变量可以改全局变量,除了整数和字符串 names=["alex","jack","luck"] def ...
- 杭电1013-Digitai Root(另解)
#include<stdio.h>#define maxsize 1000 int main(){ char N[maxsize+1]; int i,j,sum,n; c ...
- andriod 新建Activity_ Form
在一个Android工程,如何新建一个Activity? 一:新建一个类(*.class),继承自android.app.Activity类. 二:在res/layout目录下新建一个布局xml文件, ...