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)