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 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 < 109) 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 output
3 1 000

Problem Author: Emil Kelevedzhiev

Problem Source: Winter Mathematical Festival Varna ‘2001 Informatics Tournament

题解

题意

考虑长度为N的数字串,仅仅包含01,且1不能相邻。按照字典序增序求出第K个数字串是什么?如果不存在第K个,输出 -1

思路

首先先求出fib数组来保存对于长度为从1-N的数字串的个数。观察字典序增序的数组情况可以发现规律,当前位置i为1的条件是其K值大于fib[i]的值,所以我们可以每次判断是否大于fib[i]的值来确定当前位置为0还是为1。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

const int MAXN = 44+5;
int fib[MAXN];
void init()
{
    fib[0]=1;
    fib[1]=2;
    for (int i=2;i<MAXN;i++)
        fib[i] =fib[i-1]+fib[i-2];
}

int ans[MAXN];

int main(){
    int N,K;
    init();
    while(~scanf("%d %d",&N,&K)){
        memset(ans,0,sizeof(ans));
        if(fib[N]<K){
            printf("-1\n");
        }else{
            int las = K;
            for (int j=N-1; j>= 0; j--)
                if (fib[j]<las){
                    ans[j] =1;
                    las =las -fib[j];
                }
            for(int j=N-1;j>=0;j--){
                if(j!=0)    printf("%d",ans[j]);
                else    printf("%d\n",ans[j]);
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caomingpei/p/9407959.html

时间: 2024-07-31 21:53:50

URAL1081 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&

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

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 seq

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

leetcode——Maximum Depth of Binary Tree (递归,)

Maximum Depth of Binary Tree Total Accepted: 59837 Total Submissions: 132940My Submissions Question Solution Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the f

Binary Search 的递归与迭代实现及STL中的搜索相关内容

与排序算法不同,搜索算法是比较统一的,常用的搜索除hash外仅有两种,包括不需要排序的线性搜索和需要排序的binary search. 首先介绍一下binary search,其原理很直接,不断地选取有序数组的组中值,比较组中值与目标的大小,继续搜索目标所在的一半,直到找到目标,递归算法可以很直观的表现这个描述: int binarySearchRecursive(int A[], int low, int high, int key) { if (low > high) return -1;

CodeForces 743B Chloe and the sequence (递归)

题意:给定n和k,求第n个序列中第k个数是多少,序列是这样构造,原来只有1,然后再copy一遍放在到后面再在中间放上一个没有出现过的最小整数,就变成了 121,下次就成了1213121. 析:很明显是用递归来做,如果k在前半部分,那么就再递归,如果是在后半部分,那么就是先减一半再递归. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <st