Bnu4286--adjacent Bit Counts —————— "DP"

Source: Internet
Author: User

Adjacent Bit countstime limit:1000msmemory limit:65536kb64-bit integer IO format:%lld Java class name: Main Prev Submit Status Statistics discuss nextfor a string of NBits x 1 x 2 , x 3 ,. .., x N, B the adjacent bit Countof the string ( ADJBC (x)) is given by x 1 *x 2 + x 2 *x 3 + x 3 *x 4 + ... + x N -1 *x NWhich counts the number of times a 1 bit is adjacent to another 1 bit. For EXAMPLE:ADJBC (011101101) = 3AdjBC (111101101) = 4AdjBC (010101010) = 0Write A program which takes as input integers Nand kand returns the number of bit strings xOf NBits (out of 2 ?) that satisfy ADJBC ( x) = k. For example, for 5 bit strings, there is 6 ways of getting ADJBC ( x) = 2:

11100, 01110, 00111, 10111, 11101, 11011

Input

The first line of input contains a single integer p, (1≤ P ≤1000), which is the number of the data sets th at follow. Each data set is a contains the data set number, followed by a space, and followed by a decimal integer givin G The number (n) of bits in the bit strings, followed by a single space, followed by a decimal integer (k) giving the desired adjacent bit count. The number of bits (n) won't be greater than and the parameters N and K 'll be Chos En so, the result would fit in a signed 32-bit Integer.

Output

For each data set there are one line of output. It contains the data set number followed by a once space, followed by the number of n-bit strings with adjacent Bit count equal to K.

Sample Input
101 5 22 20 83 30 174 40 245 50 376 60 527 70 598 80 739 90 8410 100 90
Sample Output
1 62 634263 18612254 1682125015 448747646 1609167 229373088 991679 1547610 23076518


Problem-Solving ideas: Use Dp[i][j][k] to define the state. I means the current number is the I bit, J is the J value, K for the end is 0 or 1. Because when the end is 0 to the new one is not required, that is, J value does not change, so when dp[i][j][0] the transfer equation is dp[i][j][0]=dp[i-1][j][0]+dp[i-1] [j] [1]. The value of j is affected if the value of the new plus bit is 1 when the end is 1. So when Dp[i][j][1] dp[i][j][1]=dp[i-1][j-1][1]+dp[i-1][j][0].



#include <bits/stdc++.h>using namespace Std;int dp[500][500][2];void dp (int n) {    memset (dp,0,sizeof (DP));    Dp[1][0][0]=1;    Dp[1][0][1]=1;    for (int. i=2;i<n;i++) {for        (int j=0;j<i;j++) {            dp[i][j][0]=dp[i-1][j][0]+dp[i-1][j][1];            if (j) {                dp[i][j][1]=dp[i-1][j][0]+dp[i-1][j-1][1];            } else{                dp[i][j][1]=dp[i-1][j][0];}}}    int main () {    int n;    scanf ("%d", &n);    DP ();    while (n--) {        int t,ta,tb;        scanf ("%d%d%d", &T,&TA,&TB);        cout<<t<< "" <<dp[ta][tb][0]+dp[ta][tb][1]<<endl;    }    return 0;}

  

Bnu4286--adjacent Bit Counts —————— "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.