Binary numbers and their pattern of bits is always very interesting to computer programmers. The problem need to count the number of positive binary numbers that has the following properties:
- The numbers is exactly N bits wide and they have no leading zeros.
- The frequency of zeros and ones are equal.
- The numbers is multiples of K.
Input
The input file contains several test cases. The first line of the input gives you the number of test cases, T ( 1T100). Then T test Cases would follow, each of the line. The input for each test case consists of integers, n ( 1n) and K ( 0K).
Output
For each set of input print, the test Case number first. Then print the number of binary numbers that has the property that we mentioned.
Sample Input
56 36 46 226 364 2
Sample Output
Case 1:1case 2:3case 3:6case 4:1662453case 5:4.,654,283,532,552,61e,+17
illustration: Here's a table showing the possible numbers for some of the sample test cases:
6 3 |
6 4 |
6 2 |
101010 |
111000 |
111000 |
|
110100 |
110100 |
|
101100 |
101100 |
|
|
110010 |
|
|
101010 |
|
|
100110 |
Main topic:
That is, give you a binary sequence of length n, and then let you find out the number of sequences that can be divisible by K, the standard DP
Problem Solving Ideas:
Begins a definition of a state: Dp[i][j][k] represents the number of sequences that have a binary sequence consisting of I 0 and J 1 and the remainder of K after a K mod
Initial state: Dp[0][1][1%k]=1;
State transition equation: If we join a 1, dp[i][j+1][(k*2+1)%k]+=dp[i][j][k];
If we join a 0, dp[i+1][j][(k*2)%K] + + dp[i][j][k];
Code:
1# include<cstdio>2# include<iostream>3# include<cstring>4 5 using namespacestd;6 7typedefLong LongLL;8 9# define MAX MaxTen OneLL Dp[max][max][max];//Dp[i][j][k] Represents a number composed of I 0,j 1, divided by the number of K of the remainder of K A - - the intMainvoid) - { - intIcase =1; - intT;cin>>T; + while(t-- ) - { +Memset (DP,0,sizeof(DP)); A LL n,k; atCin>>n>>K; -printf"Case %d:", icase++); -LL NUM1 = n/2; -LL num2 =NUM1; - if(n%2==1|| k==0 ) - { incout<<0<<Endl; - Continue; to } +dp[0][1][1%K] =1; - for(LL i =0; I <= num1;i++ ) the { * for(LL j =0; J <= num2;j++ ) $ {Panax Notoginseng for(LL L =0; l <= K1; l++ ) - { thedp[i+1][j][(l*2)%k]+=Dp[i][j][l]; +dp[i][j+1[(l*2+1)%k]+=Dp[i][j][l]; A } the } + } -cout<<dp[num1][num2][0]<<Endl; $ } $ - return 0; -}
UVA 12063 Zeros and Ones (digital DP)