题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2062

题目大意:

给出n和m,集合{1,2,,,,n}的非空子集,按照一定方式排列,例如n==3时,

1
1    2
1    2    3
1    3
1    3    2
2
2    1
2    1    3
2    3
2    3    1
3
3    1
3    1    2
3    2
3    2    1
然后输出第m个序列。
 
思路:
先小规模枚举,n = 1时有1个序列,n=2时有4个序列,n=3时有15个序列。
可推出F[n] = n * (F[n - 1] + 1),F[n]表示n个元素的序列个数。那就可以想到,给定一个m,可以先推出第1位,比如n = 3,m = 6时,第一位肯定是2,那我们可以递推下去,确定第一位,然后m减小,再递推出下一位。
还是之前的例子,n = 3, m = 6, 1开头的有5个,2开头的有5个,这样可以用7除以5向上取整,求出目前是第几位,所以得出以下解法:
 
首先设c = (m + F[n - 1] ) / (F[n - 1] + 1)(这是m除以F[n - 1] + 1 向上取整),c就表示当前这一位是第c小的。
然后m = m - (c - 1) * (F[n - 1] + 1),n--,这样就可以循环下去,求目前的n和m中的第一位是什么,这里要注意的是m还得自减一,还是用上面的例子说明,求出第一位为2后,m = 1, n = 2;我们知道m = 1, n = 2按照上面的公式算出来c = 1,也就是在1,3之中第一小的数字,就是1,序列是2,1,这与答案有误,答案应该就是2,错误的原因在于这个序列的构造,n = 2时只有4种,而我们的m算出来最大可以为5(就是n=3,m = 10的时候,循环之后n = 2, m = 5),所以m应该自减一,这样正好可以对应上,如果自减一之后等于0,就应该退出循环,结束构造,这样就可以得到之前的正确答案“2”。在每次求出一位之后,数组应该左移一位,这样可以直接利用移动后的数组来求当前第c小的数字
 
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
int cases;
const int maxn = 1e5 + ;
typedef long long ll;
ll n, m, a[];
void init()
{
a[] = ;
for(ll i = ; i < ; i++)a[i] = i * (a[i - ] + );
//for(int i = 1; i <= 20; i++)cout<<a[i]<<endl;
}
int main()
{
init();
while(cin >> n >> m)
{
int b[], ans[], tot = ;
for(int i = ; i <= n; i++)b[i] = i;
while(m > )
{
int c = (m + a[n - ]) / (a[n - ] + );
//cout<<m << " "<<c<<" "<< n<<endl;
ans[tot++] = b[c];
n--;
for(int i = c; i <= n; i++)b[i] = b[i + ];
m -= (c - ) * (a[n] + );
m--;
if(m <= )break; }
cout<<ans[];
for(int i = ; i < tot; i++)cout<<" "<<ans[i];
cout<<endl;
}
}
 

hdu2062 Subset sequence----递推的更多相关文章

  1. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  2. hdu-5496 Beauty of Sequence(递推)

    题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  3. HDU 5950 Recursive sequence 递推转矩阵

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

  5. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

  6. bzoj 2656 [Zjoi2012]数列(sequence) 递推+高精度

    2656: [Zjoi2012]数列(sequence) Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Descri ...

  7. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  8. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  9. POJ_1019 Number Sequence 【递推】

    题目: A single positive integer i is given. Write a program to find the digit located in the position ...

  10. POJ_2478 Farey Sequence 【欧拉函数+简单递推】

    一.题目 The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbe ...

随机推荐

  1. 关于数据库的左,右,内,外连接,Union和Union all---------笔记

    1.左连接 select a.filed1,a.filed2,b.filed1 from a (左表) left join b(右表) on a.commonfiled = b.commonfiled ...

  2. [SCOI2011] 糖果

    luogu Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到 ...

  3. 不root手机的情况下查看Android数据库

    最近写Android的时候发现想要读取数据库很不方便,使用adb工具的方法需要root手机,而华为手机root十分麻烦,需要解除密码,一些品牌手机即使root了也没有sqlite3命令,总之十分麻烦. ...

  4. poj 3664

    http://poj.org/problem?id=3664 进行两轮选举,第一轮选前n进入第二轮,第二轮选最高 #include<algorithm> #include<cstdi ...

  5. poj-1131-(大数)八进制转化成十进制

    Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For e ...

  6. 网络通信 --> select()用法

    select()用法 头文件 #include <sys/time.h> #include <sys/types.h> #include <unistd.h> 定义 ...

  7. 计时器setInterval()、setTimeout()

    计时器setInterval() 在执行时,从载入页面后每隔指定的时间执行代码. 语法: setInterval(代码,交互时间); 参数说明: 1. 代码:要调用的函数或要执行的代码串. 2. 交互 ...

  8. java排序算法(五):快速排序

    java排序算法(五):快速排序 快速排序是一个速度非常快的交换排序算法,它的基本思路很简单,从待排的数据序列中任取一个数据(如第一个数据)作为分界值,所有比它小的元素放到左边.所有比它大的元素放到右 ...

  9. java基础笔记(6)----面向对象的三大特性

    简介:面向对象的三大特性就是封装,继承,多态,是面向对象的核心. 封装 简介:封装是类的边界,可以对数据起到保护作用 特性:属性私有,提供公开的get/set方法 属性私有:private 数据类型 ...

  10. C语言程序设计(基础)- 第2周作业

    1.阅读提问的智慧,要求仔细阅读链接内容,用自己的话描述你的收获,并举例子说明应该如何提问. 2.所有同学请在自己电脑上配置git.编译器(win10 系统的话就Dev-C++).翻译软件,十一回校后 ...