P1066 2^k binary number

Source: Internet
Author: User

P1066 2^k binary number
      • 204 through
      • 373 Submit
    • Topic provider Rokua Onlinejudge
    • Tag number theory (mathematics-related) high-precision Noip increase Group 2006
    • Difficulty Increase +/province selection-

Submit a discussion of the problem record

Recent discussions
    • No discussion at the moment
Title Description

Set R is a 2^k number and meets the following conditions:

(1) R is at least a 2-bit 2^k binary number.

(2) as the number of 2^k, except the last one, each of the R is strictly less than its right adjacent to the one.

(3) After the R is converted to 2 Q, the total number of digits of Q does not exceed W.

Here, the positive integers K (1≤k≤9) and W (k<w< span>≤30000) are given beforehand.

Q: How many different r are there to meet the above conditions?

Let us explain from another angle that s is a 01 string with a length of W (that is, the string s consists of W "0" or "1") and s corresponds to Q in the above conditions (3). The S is divided from right to several segments of length k, each corresponding to a number of 2^k, if s can be divided into at least 2 segments, then s corresponding to the binary number can be converted to the 2^k number R.

Example: Set k=3,w=7. Then R is an octal number (23=8). Because of w=7, 01 strings of length 7 are divided by 3 bits and can be broken into 3 segments (i.e. 1,3,3, the first paragraph on the left has only one bits), then the octal number that satisfies the condition is:

2 digits: High is 1:6 (i.e. 12,13,14,15,16,17), High is 2:5, ..., High is 6:1 (i.e. 67). A total of 6+5+...+1=21.

3 digits: The high can only be 1, the 2nd bit is 2:5 (that is, 123,124,125,126,127), 2nd is 3:4, ..., 2nd is 6:1 (that is, 167). A total of 5+4+...+1=15.

So there are 36 r that meet the requirements.

Input output Format input format:

Enter only 1 lines, two positive integers, separated by a space:

Kw

Output format:

The output is 1 lines, is a positive integer, for the calculated result, that satisfies the condition of the number of different r (in decimal number), the highest bit must not be 0, the numbers must not be inserted in addition to the number of characters (such as spaces, line breaks, commas, etc.).

(Hint: A positive integer as a result may be large, but not more than 200 bits)

Input and Output Sample input example # #:
3 7
Sample # # of output:
36
Description

NOIP 2006 Improvement Group Fourth question

Exercises

The topic of the analysis from another perspective has already contained the basic idea of the problem. In the case of the topic, for example, a 7-bit 01-character string is divided by 3 bits: 0 000 000. In addition to the first paragraph, each paragraph is less than (111) 2, which is less than 2k, and the first paragraph is naturally less than 2w%k (for the w%k is 0 o'clock also established).

If the first paragraph is 0, then when the 2k binary digits are 2, 3 、...、 [n/k], if using B_max to represent 2k, the corresponding number of solutions is c[b_max-1][2],c[b_max-1][3] 、...、c[b_ MAX-1][N/K] (c[i][j] means to select J from the I number to form a group of combinations).

If the first paragraph is not 0, set the first field x, then the solution has c[b_max-x-1][n/k].

In this way, the number of solutions is done, the rest of the work is high-precision. The number of combinations can be used with this formula: C[n][m]=c[n-1][m-1]+c[n-1][m], so that the high-precision only with the addition.

Summary: Answer=c (2^k-1,i) (2<=i<=w div k) +c (2^k-i-1,w div k) (1<=i<=2^ (w mod k-1))

AC Code:

#include <bits/stdc++.h>using namespace Std;int k,w,ans[220],f[1050][250];void Add (int a[],int b[]) {    int last=0;    A[0]=max (A[0],b[0]);    for (int i=1;i<=a[0];i++) {        a[i]+=b[i]+last;        LAST=A[I]/10;        a[i]%=10;    }    if (last>0) a[++a[0]]=last;} int main () {    scanf ("%d%d", &k,&w);    if (w<=k) {printf ("0\n"); return 0;}    int first,bitmax,lenth;    bitmax= (1<<k)-1;    if (w%k==0) {        First=bitmax;        lenth=w/k-1;    }    else{        first= (1<< (w%k))-1;        lenth=w/k;    }    F[1][0]=1;    F[1][1]=1;    ans[0]=0;    for (int i=1;i<=bitmax;i++) {for        (int j=i+1;j>=1;j--) Add (F[j],f[j-1]);        if (I>=bitmax-first&&i<bitmax) Add (ans,f[lenth+1]);    }    for (int i=2;i<=lenth;i++) Add (ans,f[i+1]);    for (int i=ans[0];i;i--) printf ("%d", ans[i]);    printf ("\ n");    return 0;}

  

P1066 2^k number of binary

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.