HDU 2062 Subset sequence (找规律)
Problem Description
Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3}. A subset sequence is defined as a array of a non-empty subset. Sort all the subset sequece of An in lexicography order. Your task is to find the m-th one.
Input
The input contains several test cases. Each test case consists of two numbers n and m ( 0< n<= 20, 0< m<= the total number of the subset sequence of An ).
Output
For each test case, you should output the m-th subset sequence of An in one line.
Sample Input
1 1
2 1
2 2
2 3
2 4
3 10
Sample Output
1
1
1 2
2
2 1
2 3 1
分析:
给定1,2,3...N个数的集合,现在求所有非空子集(相同元素不同位置视为不同)按字典序排序后的第m个集合是什么?
我们用f[i]来表示i个元素的子序列的个数,可以找出地推的规律:f[n] = n * (f[n-1] + 1);
思路:求n个元素时序列首元素,序列变为n-1,
求n-1个元素时序列首元素......
用t = ceil(m/(f[n-1]+1)),即可求得所求序列在所有序列中是第几组,也就是当前第一个元素在序列数组a中的位置
用数组a表示序列数组[1,2,...,n](需要动态更新,每次求出t之后,都要删除t位置的元素)
在更新之后,序列数组总长度变为n-1,我们要求一下所求序列的新位置m = m - (t-1)*(f[n-1]+1) - 1(前面有t-1组,每组f[n-1]个元素)
代码:
#include<stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
long long m, f[25];
f[0] = 0;
for (int i =1; i < 21; i++)
f[i] = i * (f[i - 1] + 1);
while (scanf("%d%lld", &n, &m) != EOF)
{
int flag = 1, a[25];
for (int i = 1; i <= n; i++)
a[i] = i;
while (m > 0)
{
int t = ceil(m * 1.0 / (f[n - 1] + 1));//求出的是n个数字的首元素的位置
if (!flag)
printf(" ");
flag = 0;
printf("%d", a[t]);
for (int i = t; i < n; i++)//然后要删去这个元素
a[i] = a[i + 1];
m = m - (t - 1) * (f[n - 1] + 1) - 1;//去掉一个元素后,总共的个数也要改变
n--;
}
printf("\n");
}
return 0;
}
HDU 2062 Subset sequence (找规律)的更多相关文章
- HDU 2062 Subset sequence 数位dp,思路 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2062 Subset sequence Time Limit: 1000/1000 MS (Java/Others ...
- 题解报告:hdu 2062 Subset sequence
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2062 Problem Description 考虑集合An = {1,2,...,n}. 例如,A1 ...
- HDU 2062 Subset sequence
我是把它当做一道数学题来做的. 这篇题解写的有点啰嗦,但是是我最原始的思维过程. 对于一个集合An= { 1, 2, …, n },在n比较小的情况下,在纸上按字典顺序把所有子集排列一下. 以n=3, ...
- hdu(2062)-Subset sequence 组合数学
意甲冠军:查找集合{1,2,3...n}第一m一个排列子. 收集的线索所行的大小. 例两个元素的排列子集合按字典树排列是:{1},{1,2},{2},{2,1}: 解法:一个一个元素来确定,每次把剩余 ...
- HDU1005Number Sequence(找规律)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 4861 Couple doubi(找规律|费马定理)
Couple doubi Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- hdu 5241 Friends(找规律?)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- hdu 3951 - Coin Game(找规律)
这道题是有规律的博弈题目,,, 所以我们只需要找出规律来就ok了 牛人用sg函数暴力找规律,菜鸟手工模拟以求规律...[牢骚] if(m>=2) { if(n<=m) {first第一口就 ...
随机推荐
- springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]
在用springmvc+mybatis进行项目开发时,上传文件抛异常... org.springframework.beans.BeanInstantiationException: Could no ...
- python安装报错:Microsoft Visual C++ 14.0 is required
保存详情如下: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build T ...
- 在Google Chrome中快速解除网页屏蔽鼠标右键、复制等限制
第一步,将书签栏设置为显示状态! 第二步,添加新书签——>在标签栏点击右键,选择“添加网页”. 第三步,设置新书签的内容. 1.起名.这个凭个人爱好吧 2.网址栏输入: javascript ...
- Majority Number III
Given an array of integers and a number k, the majority number is the number that occursmore than 1/ ...
- ThreadLocal 定义,以及是否可能引起的内存泄露(threadlocalMap的Key是弱引用,用线程池有可能泄露)
ThreadLocal 也可以跟踪一个请求,从接收请求,处理请求,到返回请求,只要线程不销毁,就可以在线程的任何地方,调用这个参数,这是百度二面的题目,参考: Threadlocal 传递参数(百度二 ...
- linux 关机、重启
一.重启命令:1.reboot2.shutdown -r now 立刻重启(root用户使用)3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.shutdown -r 20 ...
- SPOJ_LCS
经典题目,求两个串的最长公共子串. 是这样来做的. 以第一个串构造SAM,第二个串在自动机上跟新一遍就可以了. 更新的过程是这样的,假设当前到达的状态点为x(初始状态为0点),下一个字符是c,如果当前 ...
- SecureCRT8.1下载+注册机+破解教程
[下载]下载SecureCRT + SecureFX 8.1 Bundle版本软件,官网下载较麻烦,因此在此提供百度云连接. 链接:http://pan.baidu.com/s/1hsIjtSK 密码 ...
- 加密,解密web.config数据库连接字符串
"connectionStrings" 路径是web.config所在的工程目录. 1.加密EncryptWebConfig.bat @echo offC:\Windows\Mic ...
- JVM类加载机制详解(二)类加载器与双亲委派模型
在上一篇JVM类加载机制详解(一)JVM类加载过程中说到,类加载机制的第一个阶段加载做的工作有: 1.通过一个类的全限定名(包名与类名)来获取定义此类的二进制字节流(Class文件).而获取的方式,可 ...