Usaco 3.2 Stringsobits Digital DP

Source: Internet
Author: User

Stringsobits
Kim schrijvers

Consider an ordered set S of Strings of n (1 <= n <=) bits. Bits, of course, are either 0 or 1.

This set of strings are interesting because it is ordered and contains all possible strings of length N that has L (1 < = L <= N) or fewer bits that is ' 1 '.

Your task is to read a number I (1 <= I <= sizeof (S)) from the input and print the Ith element of the ordered set fo R N Bits with no further than L bits that is ' 1 '.

Program Name:kimbitsinput FORMAT

A single line with three space separated integers:n, L, and I.

SAMPLE INPUT (file kimbits.in)
5 3 19
OUTPUT FORMAT

A single line containing the integer this represents the Ith element from the order set, as described.

SAMPLE OUTPUT (file kimbits.out)
10011

The number of 1 for which the binary length is n (which can include the leading 0) does not exceed the number of L.

In counter-consideration, set the number of M, digital DP can easily find the [0,m] of the number of matching conditions (the binary length of n (can include the number of 1 of the leading 0) Count K, obviously M is the number of K.

So the direct two-point judgment is possible.

/*id:xidian5601prog:kimbitslang:c++*/#include<bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1e9+Ten; ll N,l,i;ll f[ +][ +];intnum[ +],len;ll DFS (intIintCntinte) {    if(i==-1)returncnt<=m; if(!e&&~f[i][cnt])returnf[i][cnt]; ll Res=0; intU=e?num[i]:1; REP (d,0, U) {        if(cnt+d<=l) Res+=dfs (i-1, cnt+d,e&&d==T); }    returne?res:f[i][cnt]=Res;} ll F (ll N) {len=0;  while(n) {Num[len++]=n%2; N/=2; }    returnDFS (len-1,0,1);}intCNT (ll N) {intres=0;  while(n) {res+=n%2; N/=2; }    returnRes;} ll bin (ll L,ll r,ll kth) { while(l<=R) {ll m= (l+r) >>1; ll T=f (m), c=CNT (m); if(c<=l&&t==kth)returnm; if(t==kth&&c>l) r=m-1; Else if(t>kth) r=m-1; Elsel=m+1; }}voidPrint (ll N) {len=0;  while(n) {num[++len]=n%2; N/=2; } REP (I,len+1, N) printf ("0");  for(inti=len;i>=1; i--) printf ("%d", Num[i]);p UTS ("");}intMain () {//freopen ("In.txt", "R", stdin);Freopen ("kimbits.in","R", stdin); Freopen ("Kimbits.out","W", stdout);  while(cin>>n>>l>>I) {memset (F,-1,sizeof(f)); Print (Bin (0, (1ll<<n)-1, I)); }    return 0;}
View Code

Usaco 3.2 Stringsobits Digital 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.