Ural 1081 binary lexicographic sequence (DP)

Source: Internet
Author: User

Subject address: Ural 1081

Use DP to obtain the number of valid sequences (starting with 1) in each length. Then calculate the prefix and. We will find that it is exactly a Fibonacci series. Then, each time you determine whether the value is greater than the minimum number of characters in the length at this time, if the value is greater than, it indicates that this bit must be 1. If the value is smaller than, it must be 0. Then output the data continuously.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const int INF=0x3f3f3f3f;LL dp[3][50], sum[50], s[50];int main(){    int i, j, n, k;    memset(dp,0,sizeof(dp));    dp[0][1]=0;    dp[1][1]=1;    sum[1]=2;    s[1]=2;    for(i=2; i<44; i++)    {        dp[1][i]+=dp[0][i-1];        dp[0][i]+=dp[0][i-1]+dp[1][i-1];        sum[i]=dp[0][i]+dp[1][i];        s[i]=s[i-1]+sum[i];    }    /*for(i=1;i<=44;i++)    {        printf("%d ",s[i]);    }    puts("");*/    s[0]=1;    while(scanf("%d%d",&n,&k)!=EOF)    {        if(k>s[n])            printf("-1\n");        else        {            while(n--)            {                if(k>s[n])                {                    printf("1");                    k-=s[n];                }                else                    printf("0");            }        }    }    return 0;}


Ural 1081 binary lexicographic sequence (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.