UVA - 12063
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Binary numbers and their pattern of bits are always very interesting to computer programmers. In this problem you need to count the number of positive binary numbers that have the following properties:
5 6 3 6 4 6 2 26 3 64 2
Case 1: 1 Case 2: 3 Case 3: 6 Case 4: 1662453 Case 5: 465428353255261088
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 |
Source
求长度为n的串(不含前导0),0的个数和1的个数相等且是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; }
原文地址:http://blog.csdn.net/acvcla/article/details/45546913