数位DP,一句话概括,就是在一个给定区间内求出满足某中奇葩条件的数字个数,这真是奇葩题目,但是总体写起来又有一定规律性。
主要可以分为以下几个步骤:
确定主体框架,确定一个大方向,想想该如何设计状态;
下面基本就是模板,直接DFS就行了,一位一位处理,这也是他叫按位DP的原因。
数位DP代码一般都很短,不过效率挺好,解决一些竞赛中出现的问题非常有用 。
如果看了这部分 ,你感觉还是不会的话,(这是当然啊,狂汗~~),那么请继续往下看。
下面用几个例子来说明一下,具体注释都附在代码内:
PS:我是菜狗,这些都是很水的数位DP,求大神勿喷~~
————————————————————— ——华丽的分割线—————————————————————
Problem 1160 - 科协的数字游戏I
Time Limit: 1000MS
Memory Limit: 65536KB
Difficulty:
Total Submit: 181
Accepted: 29
Special Judge: No
Description
科协里最近很流行数字游戏。某人命名了一种不降数,这种数字必须满足从左到右各位数字成大于等于的关系,如123,446。现在大家决定玩一个游戏,指定一个整数闭区间[a,b],问这个区间内有多少个不降数。
Input
题目有多组测试数据。每组只含2个数字a, b (1 <= a, b <= 2^31)。
Output
每行给出一个测试数据的答案,即[a, b]之间有多少阶梯数。
Sample Input
1 9
1 19
Sample Output
9
18
Hint
-
-
-
-
-
-
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
-
- using namespace std;
-
- #define LL long long
-
- const int N=25;
- int digit[N];
- LL dp[N][N];
-
- LL dfs(int pos,int statu,int limit)
- {
- int i,end,s;
- LL res=0;
- if(pos==-1)
- return 1;
- if(!limit&&dp[pos][statu]!=-1)
- return dp[pos][statu];
-
- end=limit?digit[pos]:9;
- for(i=statu;i<=end;i++)
- res+=dfs(pos-1,i,limit&&i==end);
- if(!limit)
- dp[pos][statu]=res;
- return res;
- }
-
- LL calc(LL n)
- {
- int len=0;
- memset(dp,-1,sizeof(dp));
- while(n)
- {
- digit[len++]=n%10;
- n/=10;
- }
- return dfs(len-1,0,1);
- }
-
- int main()
- {
- LL a,b;
- while(scanf("%lld %lld",&a,&b)!=EOF)
- printf("%lld\n",calc(b)-calc(a-1));
- return 0;
- }
————————————————————————————————————分割线—————————————————————————————————————
Problem 1161 - 科协的数字游戏II
Time Limit
: 1000MS
Memory Limit
: 65536KB
Difficulty
:
Total Submit
: 108
Accepted
: 13
Special Judge
: No
Description
由于科协里最近真的很流行数字游戏。(= =!汗一个)某人又命名了一种取模数,这种数字必须满足各位数字之和 mod N为0。现在大家又要玩游戏了,指定一个整数闭区间[a,b],问这个区间内有多少个取模数。
Input
题目有多组测试数据。每组只含3个数字a, b, n (1 <= a, b <= 2^31,1 <= n < 100)。
Output
每个测试用例输出一行,表示各位数字和 mod N为0 的数的个数。
Sample Input
1 19 9
Sample Output
2
Hint
Source
tclh123
-
-
-
-
-
-
-
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
-
- using namespace std;
-
- typedef long long LL;
- const int maxn=100+5;
-
- int dp[maxn][105];
- int digit[maxn];
-
- int mod,l,r;
- int DFS(int pos,int pre,bool limit)
- {
- if(pos==-1)
- return pre==0;
- if(!limit&&dp[pos][pre]!=-1)
- return dp[pos][pre];
-
- LL res=0,end=limit?digit[pos]:9;
-
- for(int i=0;i<=end;i++)
- {
- int new_pre=(pre+i)%mod;
- res+=DFS(pos-1,new_pre,limit&&i==end);
- }
- if(!limit)
- dp[pos][pre]=res;
- return res;
- }
-
- LL solve(int n)
- {
- int len=0;
- while(n)
- {
- digit[len++]=n%10;
- n/=10;
- }
- return DFS(len-1,0,true);
- }
-
- int main()
- {
- while(scanf("%d%d%d",&l,&r,&mod)!=EOF)
- {
- memset(dp,-1,sizeof(dp));
- printf("%lld\n",solve(r)-solve(l-1));
- }
- return 0;
- }
————————————分割线————————
HDU3052--B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1556 Accepted Submission(s): 852
Problem Description
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
Output
Print each answer in a single line.
Sample Input
Sample Output
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
-
- using namespace std;
-
- typedef long long LL;
- const int maxn=100+5;
-
- int dp[10][13][3];
- int digit[10];
-
- int DFS(int pos,int status,int pre,bool limit)
- {
- if(pos==-1)
- return status==2&&pre==0;
- if(!limit&&dp[pos][pre][status]!=-1)
- return dp[pos][pre][status];
-
- LL res=0;
- int end=limit?digit[pos]:9;
-
- for(int i=0;i<=end;i++)
- {
- int new_pre=(pre*10+i)%13;
- int new_status=status;
-
-
- if(status==0&&i==1)
- new_status=1;
- if(status==1&&i==1)
- new_status=1;
- else if(status==1&&i!=3)
- new_status=0;
- if(status==1&&i==3)
- new_status=2;
-
- res+=DFS(pos-1,new_status,new_pre,limit&&i==end);
-
-
- }
- if(!limit)
- dp[pos][pre][status]=res;
- return res;
- }
-
- LL solve(int n)
- {
- int len=0;
- while(n)
- {
- digit[len++]=n%10;
- n/=10;
- }
- return DFS(len-1,0,0,true);
- }
-
- int main()
- {
- LL m,n;
- while(scanf("%lld",&n)!=EOF)
- {
- memset(dp,-1,sizeof(dp));
- printf("%lld\n",solve(n));
- }
- return 0;
- }
HDU3555----BombTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4594 Accepted Submission(s): 1601
Problem Description
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.
The input terminates by end of file marker.
Output
For each test case, output an integer indicating the final points of the power.
Sample Input
Sample Output
-
-
-
-
- #include <cstdio>
- #include <cstring>
-
- using namespace std;
-
- int bit[25];
- __int64 dp[25][3];
-
-
-
-
-
-
-
- __int64 Dfs (int pos,int s,bool limit)
- {
- if (pos==-1)
- return s==2;
- if (limit==false && ~dp[pos][s])
- return dp[pos][s];
- int i ,end=limit?bit[pos]:9;
- __int64 ans=0;
- for (i=0;i<=end;i++)
- {
- int nows=s;
- if(s==0 && i==4)
- nows=1;
- if(s==1 && i!=9)
- nows=0;
- if(s==1 && i==4)
- nows=1;
- if(s==1 && i==9)
- nows=2;
- ans+=Dfs(pos-1 , nows , limit && i==end);
- }
-
-
- return limit?ans:dp[pos][s]=ans;
- }
-
- int main ()
- {
- __int64 n;
- int T;
- memset(dp,-1,sizeof(dp));
- scanf("%d",&T);
- while (T--)
- {
- scanf("%I64d",&n);
- int len=0;
- while (n)
- {
- bit[len++]=n%10;
- n/=10;
- }
- printf("%I64d\n",Dfs(len-1,0,1));
- }
- return 0;
- }
HDU2089--不要62
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13687 Accepted Submission(s): 4402
Problem Description
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
Input
输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。
Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。
Sample Input
Sample Output
-
-
-
-
-
-
-
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
-
- using namespace std;
-
- typedef long long LL;
- const int maxn=100+5;
-
- int dp[maxn][3];
- int digit[maxn];
-
-
-
- int DFS(int pos,int status,bool limit)
- {
- if(pos==-1)
- return status==2;
- if(!limit&&dp[pos][status]!=-1)
- return dp[pos][status];
-
- LL res=0,end=limit?digit[pos]:9;
-
- for(int i=0;i<=end;i++)
- {
- int new_status=status;
-
- if(i==4)
- new_status=2;
- else if(status==0&&i==6)
- new_status=1;
- else if(status==1&&i==6)
- new_status=1;
- else if(status==1&&i!=2)
- new_status=0;
- else if(status==1&&i==2)
- new_status=2;
-
- res+=DFS(pos-1,new_status,limit&&i==end);
- }
- if(!limit)
- dp[pos][status]=res;
- return res;
- }
-
- LL solve(int n)
- {
- int len=0;
- while(n)
- {
- digit[len++]=n%10;
- n/=10;
- }
- return DFS(len-1,0,true);
- }
-
- int main()
- {
- int n,m;
- while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
- {
- memset(dp,-1,sizeof(dp));
- printf("%lld\n",m-n+1-solve(m)+solve(n-1));
- }
- return 0;
- }
HDU4734F(x)
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 485 Accepted Submission(s): 179
Problem Description
For a decimal number x with n digits (A
nA
n-1A
n-2 ... A
2A
1), we define its weight as F(x) = A
n * 2
n-1 + A
n-1 * 2
n-2 + ... + A
2 * 2 + A
1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B, inclusive, whose weight is no more than F(A).
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
For each test case, there are two numbers A and B (0 <= A,B < 10
9)
Output
For every case,you should output "Case #t: " at first, without quotes. The
t is the case number starting from 1. Then output the answer.
Sample Input
Sample Output
Case #1: 1
Case #2: 2
Case #3: 13
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
-
- using namespace std;
-
- const int maxn=300;
- const int maxm=6000;
-
- int dp[maxn][maxm];
- int a[maxn];
-
- int DFS(int pos,int cur,int limit)
- {
- int i,ed,s,ans=0;
- if(pos==-1)
- return cur>=0;
- if(!limit&&dp[pos][cur]!=-1)
- return dp[pos][cur];
-
- ed=limit?a[pos]:9;
- for(i=0;i<=ed;i++)
- {
- s=cur-i*(1<<pos);
- if(s<0)
- break;
- ans+=DFS(pos-1,s,limit&&i==ed);
- }
-
- if(!limit&&dp[pos][cur]==-1)
- dp[pos][cur]=ans;
- return ans;
- }
-
- int transfer(int n)
- {
- int res=0,m=1;
- while(n)
- {
- res+=(n%10)*m;
- n/=10;
- m*=2;
- }
- return res;
- }
-
- int solve(int n,int m)
- {
- int st=-1;
- while(m)
- {
- a[++st]=m%10;
- m/=10;
- }
- int res=DFS(st,transfer(n),1);
- return res;
- }
-
- int main()
- {
- int test,n,m;
- scanf("%d",&test);
- memset(dp,-1,sizeof(dp));
- for(int ii=1;ii<=test;ii++)
- {
- scanf("%d%d",&n,&m);
- printf("Case #%d: %d\n",ii,solve(n,m));
- }
- return 0;
- }
windy数
Description
windy定义了一种windy数。
不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。
windy想知道,在A和B之间,包括A和B,总共有多少个windy数?
Input
包含两个整数,A B。
满足 1 <= A <= B <= 2000000000 。
Output
包含一个整数:闭区间[A,B]上windy数的个数。
Sample Input
1 10
Sample Output
9
- <pre class="cpp" name="code">
-
-
-
-
-
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
-
- using namespace std;
- const int maxn=20;
-
- typedef long long LL;
- LL dp[maxn][11];
- LL digit[maxn];
-
- LL DFS(int pos,int pre,bool limit,bool first_place)
- {
- if(pos==-1)
- return first_place==0;
- if(!limit&&dp[pos][pre]!=-1&&first_place==false)
- return dp[pos][pre];
-
- int end=limit?digit[pos]:9;
- LL ans=0;
-
- for(int i=0;i<=end;i++)
- {
- if(first_place!=0)
- ans+=DFS(pos-1,i,limit&&i==end,first_place&&i==0);
- else if(abs(i-pre)>=2)
- ans+=DFS(pos-1,i,limit&&i==end,first_place);
- }
- if(!limit&&first_place==false)
- dp[pos][pre]=ans;
- return ans;
- }
-
- LL solve(LL n)
- {
- LL len=0;
- while(n)
- {
- digit[len++]=n%10;
- n/=10;
- }
- return DFS(len-1,0,true,true);
- }
-
-
- int main()
- {
-
- LL l,r;
- while(scanf("%lld%lld",&l,&r)!=EOF)
- {
- memset(dp,-1,sizeof(dp));
- LL ans=solve(r)-solve(l-1);
- printf("%lld\n",ans);
- }
- return 0;
- }