URAL1081——DP—— Binary Lexicographic Sequence

Description

Consider all the sequences with length (0 <  N < 44), containing only the elements 0 and 1, and no two ones are adjacent (110 is not a valid sequence of length 3, 0101 is a valid sequence of length 4). Write a program which finds the sequence, which is on K-th place (0 < K < 10 9) in the lexicographically sorted in ascending order collection of the described sequences.

Input

The first line of input contains two positive integers N and K.

Output

Write the found sequence or −1 if the number K is larger then the number of valid sequences.

Sample Input

input output
3 1
000

大意:两个1不能相邻,很像那道完美串,用dp来计算长度为多少时当前为什么数的时候的个数

状态转移方程  dp[i][1] = dp[i-1][0]

      dp[i][0] = dp[i-1][0] + dp[i-1][1]

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[45][2];
int main()
{
    int n,k;
    dp[1][0] = dp[1][1]= 1;
    for(int i = 2; i <= 44; i++){
        dp[i][1] = dp[i-1][0];
        dp[i][0] = dp[i-1][0]+dp[i-1][1];
    }
    while(~scanf("%d%d",&n,&k)){
        if(k > dp[n][1] + dp[n][0]){
            printf("-1\n");
            continue;
        }
        while(n){
            if(dp[n][0] >= k)
                printf("0");
            else {
                k -= dp[n][0];
                printf("1");
            }
            n--;
            }
        printf("\n");
    }
    return 0;
}

  

时间: 2024-07-29 01:49:34

URAL1081——DP—— Binary Lexicographic Sequence的相关文章

递推DP URAL 1081 Binary Lexicographic Sequence

题目传送门 1 /* 2 dp[i][1]/dp[i][0] 表示从左往右前i个,当前第i个放1或0的方案数 3 k -= dp[n][0] 表示当前放0的方案数不够了,所以必须放1,那么dp[n][0]个方案数都不能用了 4 相当于k减去这么多 5 详细解释:http://www.cnblogs.com/scau20110726/archive/2013/02/05/2892587.html 6 */ 7 #include <cstdio> 8 #include <algorithm&

URAL1081 Binary Lexicographic Sequence(递归)

URAL 1081. Binary Lexicographic Sequence Time limit: 0.5 second Memory limit: 64 MB Description Consider all the sequences with length (0 < N < 44), containing only the elements 0 and 1, and no two ones are adjacent (110 is not a valid sequence of l

URAL 1081 Binary Lexicographic Sequence

第13个位置第5个Bit :13>num[4] =>1 第四个bit 13-num[4]=5 :5<num[3] =>0 ,3-1 第三个Bit 5>num[2](3) 5-num[2]=2 ... #include<stdio.h> int num[45]; void init() { num[0]=1; num[1]=2; int k=2; while(k<44) { num[k]=num[k-1]+num[k-2]; k++; } } int main

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

ural Binary Lexicographic Sequence (dp + dfs)

http://acm.timus.ru/problem.aspx?space=1&num=1081 有一个二进制序列,定义为不能有两个连续的1出现,才是合法的.给出序列的长度n,求合法的二进制序列中按字典序排序后第k个序列是什么. 设dp[i][0]和dp[i][1]分别表示第i位上是0和1的个数. 那么dp[i][0] = dp[i-1][0] + dp[i-1][1]:dp[i][1] = dp[i-1][0],打表发现和斐波那契数列相似. 求第k个合法的序列时,因为是按字典序排序,可以发现

Binary Lexicographic Sequence URAL - 1081 (有关数的排列)

1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 #include<map> 5 #include<set> 6 #include<string> 7 using namespace std; 8 typedef unsigned long long LL; 9 const int maxn = 46; 10 LL dp[maxn][2]; 11 12 vo

[DP] UVA-1626 Brackets sequence

Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular sequence. F

UVa 10534 DP LIS Wavio Sequence

两边算一下LIS就出来了,因为数据比较大,所以需要二分优化一下. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 10000 + 10; 8 9 int n; 10 11 int a[maxn], l[maxn], r[maxn]; 12 int g

应用程序的性能: C# vs C/C++

最近一段时间,我在 Timus Online Judge 网站做 ACM 题. 首先,让我们看一下 Timus 1114. Boxes: 这道题要求计算出将两种颜色的球放到盒子中的各种组合的数目.我们发现用同样的算法,C# 程序居然比 C++ 程序慢 62 倍. 真的是 C# 应用程序的性能就一定很差吗?不是的.实际上在这道题中,使用的算法是非常高效的.上面的 0.001 秒和 0.062 秒已经分别是 C/C++ 程序和 C# 程序在 Timus Online Judge 网站运行的最短时间了