UVA-12063
Zeros and Ones
Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
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.
InputThe input file contains several test cases. The first line of the input gives you the number of test cases,
T(1
T). Then
TTest cases would follow, each on one line. The input for each test case consists of integers,
N(1
N) and
K(0
K).
OutputFor 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 |
/tbody>
Source
Root:: Prominent problemsetters:: Monirul Hasan
Root:: ACM-ICPC Dhaka Site Regional contests:: 2004-dhaka
Root:: AOAPC ii:beginning algorithm Contests (Second Edition) (Rujia Liu):: Chapter 10. Maths:: Exercises
Root:: Competitive programming 2:this increases the lower bound of programming contests. Again (Steven & Felix Halim):: More advanced Topics:: More advanced Dynamic Programming:: DP + Bitmask
Submit Status
For a string of length n (without leading 0), the number of 0 is equal to the number of 1 and is the number of multiples of K.
#include <bits/stdc++.h> #define foreach (it,v) for (__typeof ((v). Begin ()) it = (v). Begin (); it! = (v). End (); ++it) Using namespace Std;typedef long long ll;ll dp[40][40][100];int n,k,tot;ll d (int ones,int zeros,int MoD) { ll & Res = Dp[ones][zeros][mod]; if (res!=-1) return res; if (Ones==tot&&zeros==tot) return res = (mod==0); if (Ones==tot) return res = d (ones,zeros+1, (mod<<1)%k); if (Zeros==tot) return res = d (Ones+1,zeros, (mod<<1|1)%k); return res = d (Ones+1,zeros, (mod<<1|1)%k) + D (ones,zeros+1, (mod<<1)%k);} int main () { int T; scanf ("%d", &t); for (int cas = 1; CAs <= T; ++cas) { memset (dp,-1,sizeof dp); scanf ("%d%d", &n,&k); ll res = 0; tot = n>>1; if ((n&1) ==0&&k) res = d (1,0,1); printf ("Case%d:%lld\n", cas,res); } return 0;}
UVA 12063 (DP memory)