跳过正文
  1. Posts/

hdu 5787 K-wolf Number 2016 Multi-University Training Contest 5 1007 (不允许前导0的数位dp)

·1 分钟

hdu5787

题意:给出l,r,k求区间[l,r]中满足任意相邻k个数字都不相同的数的个数.

思路:数位dp,dp[i][k1][k2][k3][k4]表示长度为i,前1位是k1,前2位是k2,前3位是k3,前4位是k4的方案数. 注意不允许前导0.2A

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年08月02日 星期二 16时28分29秒
 4File Name :code/multi2016/#5/1007.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <map>
14#include <string>
15#include <cmath>
16#include <cstdlib>
17#include <ctime>
18#define fst first
19#define sec second
20#define lson l,m,rt<<1
21#define rson m+1,r,rt<<1|1
22#define ms(a,x) memset(a,x,sizeof(a))
23typedef long long LL;
24#define pi pair < int ,int >
25#define MP make_pair
26using namespace std;
27const double eps = 1E-8;
28const int dx4[4]={1,0,0,-1};
29const int dy4[4]={0,-1,1,0};
30const int inf = 0x3f3f3f3f;
31LL l,r;
32int k;
33LL digit[25];
34LL dp[22][11][11][11][11];
35int LEN;
36LL dfs(int pos,int k1,int k2,int k3,int k4,bool limit,bool prehasnonzero)
37{
38    if (pos==0) return 1;
39    if (prehasnonzero&&!limit&&dp[pos][k1][k2][k3][k4]!=-1LL) return dp[pos][k1][k2][k3][k4];
40
41    int mx = limit?digit[pos]:9;
42    LL res = 0LL;
43    if (!prehasnonzero)
44    {
45	for  (int i = 0 ; i <= mx ;i++)
46	{
47	    res +=dfs(pos-1,i,10,10,10,limit&&i==mx,i==0?false:true);
48	}
49    }
50    else
51    {
52	for ( int i = 0 ; i <= mx ; i++)
53	{
54	   // if (i==k1||i==k2||i==k3||i==k4) continue;
55	    if (k>=2&&i==k1) continue;
56	    if (k>=3&&i==k2) continue;
57	    if (k>=4&&i==k3) continue;
58	    if (k>=5&&i==k4) continue;
59
60	    res +=dfs(pos-1,i,k1,k2,k3,limit&&i==mx,true);
61	}
62    }
63
64    if (prehasnonzero&&!limit) dp[pos][k1][k2][k3][k4] = res;
65    return res;
66}
67LL solve ( LL n)
68{
69    ms(digit,0);
70    int len = 0 ;
71    while (n)
72    {
73	digit[++len] = n;
74	n/=10;
75    }
76    LEN = len;
77    return dfs(len,10,10,10,10,true,false);
78}
79int main()
80{
81	#ifndef  ONLINE_JUDGE
82	freopen("code/in.txt","r",stdin);
83  #endif
84	while (~scanf("%lld%lld%d",&l,&r,&k))
85	{
86	    ms(dp,-1);
87//	    cout<<"solve(100):"<<solve(100)<<endl;
88//	    cout<<"solve(r):"<<solve(r)<<" solve(l-1):"<<solve(l-1)<<endl;
89	    LL ans = solve(r)-solve(l-1);
90	    printf("%lld\n",ans);
91	}
92  #ifndef ONLINE_JUDGE
93  fclose(stdin);
94  #endif
95    return 0;
96}

相关文章

hdu 4734 F(x) (数位dp)

·3 分钟
题目链接s 题意:将一个10进制数x按照2进制展开后得到的值设为f(x)…现在给出a,b(10^9)问【0,b】中满足f[x]<=f[a]的数的个数。 思路:先算出f[a]…然后我们发现f(x)最大也就10*2^10=10240.。。数组可以存下。。搞之。和上一道题类似。。我们不关心两个f函数的值具体是多少。。。只关心他们的相对大小情况。。所以还是可以合并成一个变量。。。然而我用两个变量为什么错了。。不懂==

hdu 3709 Balanced Number (数位dp)

·3 分钟
题目链接 题意:找到某区间中平衡数的个数。所谓平衡数是指,存在某个位置,值得两边的力矩相等。举个例子。。比如14326,如果把4作为中间。。那么左边=11=1 右边=31+22+62=19。。。 思路:枚举中间的pivot。。。注意如果是个位数也是平衡数(就是认为两边的力矩都是0了。。。),所以每一个位置都可能是平衡位置。。枚举的时候从1到len… 一开始我是分别记录两边的值。。非常浪费空间。。。然而发现其实没必要。。我们只关心左边是否相等。。而不关心左右的值到底是多少。。所以可以把两边的值带符号合并成一个值(pivot左边为+,pivot右边为负)。。。如果最后为0。。说明左右相等。。。

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

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