↓ Skip to main content
  1. Posts/

poj3252 Round Numbers (不允许前导0的二进制数位dp)

·530 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接 题意:问某区间中,round number 的个数是多少。所谓round number,当且仅当一个数的二进制表示中,‘0’的个数大于等于‘1’的个数。 思路:简单数位dp..和windy数那道题类似,都是不允许前导0.。。所以在dfs中要加一维判断前面是否有非0的数。。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年03月17日 星期四 16时17分07秒
 4File Name :code/poj/3252.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33int l,r;
34int digit[35];
35int dp[35][35][35]; //dp[i][j][k]表示长度为i,有j个0,k个1的方案数。
36
37int dfs( int pos,int cnt0,int cnt1,bool limit,bool prehasnonzero)  //不允许前导0,所以要加prehasnonzero这个参数
38								    //来确定是否位数减少了....
39{
40    if (pos==0) return cnt0>=cnt1;
41    if (!limit&&dp[pos][cnt0][cnt1]!=-1) return dp[pos][cnt0][cnt1];
42    int mx = limit?digit[pos]:1; //2进制。。最大是1.
43    int res = 0;
44    if (prehasnonzero)
45    {
46	for ( int i = 0 ; i <= mx;  i++)
47	{
48	    res+=dfs(pos-1,i==0?cnt0+1:cnt0,i==1?cnt1+1:cnt1,limit&&i==mx,true);
49	}
50    }
51    else
52    {
53	for ( int i = 0 ; i <= mx  ; i++)
54	{
55	    if (i==0)
56	    {
57		res+=dfs(pos-1,0,0,limit&&i==mx,false);
58	    }
59	    else
60	    {
61		res+=dfs(pos-1,0,1,limit&&i==mx,true);
62	    }
63	}
64    }
65	if (!limit) dp[pos][cnt0][cnt1] = res;
66	return res;
67    }
68int solve ( int n)
69{
70    ms(digit,0);
71    int len =  0 ;
72
73    while (n)
74    {
75	digit[++len] = n % 2;
76	n /= 2;
77    }
78
79    return  dfs(len,0,0,true,false);
80}
81int main()
82{
83	#ifndef  ONLINE_JUDGE
84	freopen("code/in.txt","r",stdin);
85  #endif
86
87	ms(dp,-1);
88	cin>>l>>r;
89	int ans = solve ( r ) - solve ( l - 1 );
90	cout<<ans<<endl;
91
92  #ifndef ONLINE_JUDGE
93  fclose(stdin);
94  #endif
95    return 0;
96}

Related

hdu 4722 good numbers (带整除的数位dp)

·478 words·1 min
题目链接 题意:求一个区间内所有位数字之和能被10整除的数的个数。 思路:数位dp,dfs要一个参数记录从最高位到现在的pos位置的数字之和的结果。 代码实现 1 dp[i][j] 表示长度为i,和为j的方案数。 2 记得开long long ,然而我开了那么多long long 忘了dp 的long long 结果wa到死。。果然大早上不清醒吗== 3 4 5 6/* *********************************************** 7Author :111qqz 8Created Time :2016年03月16日 星期三 08时10分19秒 9File Name :code/hdu/4722.cpp 10************************************************ */ 11 12#include <cstdio> 13#include <cstring> 14#include <iostream> 15#include <algorithm> 16#include <vector> 17#include <queue> 18#include <set> 19#include <map> 20#include <string> 21#include <cmath> 22#include <cstdlib> 23#include <ctime> 24#define fst first 25#define sec second 26#define lson l,m,rt<<1 27#define rson m+1,r,rt<<1|1 28#define ms(a,x) memset(a,x,sizeof(a)) 29typedef long long LL; 30#define pi pair < int ,int > 31#define MP make_pair 32 33using namespace std; 34const double eps = 1E-8; 35const int dx4[4]={1,0,0,-1}; 36const int dy4[4]={0,-1,1,0}; 37const int inf = 0x3f3f3f3f; 38LL l,r; 39int digit[30]; 40LL dp[30][15]; //dp 数组忘记开long long ,wa到死。。。。。。。。。日了哈士奇。 41LL dfs ( int pos,int sum,bool limit) 42{ 43 if (pos==0) 44 { 45 if (sum==0) return 1; 46 else return 0; 47 } 48 if (!limit&&dp[pos][sum]!=-1) return dp[pos][sum]; 49 50 int mx = limit?digit[pos]:9; 51 52 LL res = 0 ; 53 for ( int i = 0 ; i <= mx; i ++) 54 { 55 res+=dfs(pos-1,(sum+i),limit&&i==mx); 56 } 57 58 if (!limit) dp[pos][sum] = res; 59 60 return res; 61 62} 63LL solve ( LL n) 64{ 65// if (n==0) return 1; 66 // if (n<=9) return 0; 67 if (n<0) return 0; 68 ms(digit,0); 69 int len = 0 ; 70 while (n) 71 { 72 digit[++len] = n % 10; 73 n /= 10; 74 } 75 76 return dfs(len,0,true); 77} 78int main() 79{ 80 #ifndef ONLINE_JUDGE 81 freopen("code/in.txt","r",stdin); 82 #endif 83// ios::sync_with_stdio(false); 84 int T; 85 cin>>T; 86 ms(dp,-1); 87 int cas = 0 ; 88 while (T--) 89 { 90 scanf("%lld %lld",&l,&r); 91 LL ans = solve (r)-solve(l-1); 92 93 printf("Case #%d: %lld\n",++cas,ans); 94 } 95 96 #ifndef ONLINE_JUDGE 97 fclose(stdin); 98 #endif 99 return 0; 100}

bzoj 1026 windy数(数位dp入门题)

·757 words·2 mins
题目链接 题意:不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 思路:数位dp 这道题的特点是前面不允许前导0,也就是说,如果第i位前面全是0的话,这个数就变成了i位数,i就变成了最高位,而最高位没有前面的数(**如果这里不考虑不允许前导0这个因素而把前面的一个数认为成是0就错了) **最高位的数可以直接取。 还有记忆化调用以及存储的时候也要注意…只有当位数相同的时候转移才有意义。 具体的方法是dfs中多了一个prehasnonzero的bool变量,就是字面意思,判断当前位置前面的位置是够存在一个非0的值。

hdu 3555 Bomb (数位dp入门题)

·401 words·1 min
题目链接 题意:问从1到n的所有数中,有多少个数含有数字串“49” 思路:和上一道不要62很像,但是由于是要统计有49的,但是有49的情况实在太多了,正难则反,用减法定理反过来考虑,先统计出不含49的数的个数,这样就和不要62一样了,然后再用总数减。