好多比赛都有数位dp,看着别人都能做上来,决心学一下
给出 讲解最好的链接:点击打开链接
我还是通过两道简单的题目来巩固一下。
HDU 2089
不要62
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 35150 Accepted Submission(s): 12778
Problem Description
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
Input
输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。
Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。
Sample Input
1 100 0 0
Sample Output
80
Author
qianneng
一看就是数位DP,区间很大,给出数的限制。DP[pos][sta]:pos表示当前位,sta表示前一位是否为6,
<span style="font-size:14px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define ll long long
using namespace std;
int a[20];
int dp[20][2];
int dfs(int pos,int sta,bool limit)
{
if(pos==-1)
return 1;
if(!limit&&dp[pos][sta]!=-1)
return dp[pos][sta];
int up=limit?a[pos]:9;
int ans=0;
for(int i=0;i<=up;i++)
{
if(sta==1&&i==2)
continue;
if(i==4)
continue;
ans+=dfs(pos-1,i==6,limit&&i==a[pos]);
}
if(!limit)
dp[pos][sta]=ans;
return ans;
}
int solve(int x)
{
int pos=0;
while(x)
{
a[pos++]=x%10;
x/=10;
}
return dfs(pos-1,0,true);
}
int main()
{
int l,r;
memset(dp,-1,sizeof(dp));
while(scanf("%d%d",&l,&r)&&l+r)
{
printf("%d\n",solve(r)-solve(l-1));
}
return 0;
}
</span>
HDU:3555
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 15555 Accepted Submission(s): 5645
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?
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.
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
3 1 50 500
Sample Output
0 1 15HintFrom 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.
问区间含有49的数有多少, dp[pos][sta] :sta有三个状态0 ,1,2 0表示前面没有4,9 1:前一位是4,2;前面已经构成了49了,
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define ll long long
using namespace std;
int a[20];
ll dp[20][3];
ll dfs(int pos,int sta,bool limit)
{
if(pos==-1)
return sta==2;
if(!limit&&dp[pos][sta]!=-1)
return dp[pos][sta];
int up=limit?a[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++)
{
if(sta==2||sta==1&&i==9)
ans+=dfs(pos-1,2,limit&&i==a[pos]);
else if(i==4)
ans+=dfs(pos-1,1,limit&&i==a[pos]);
else
ans+=dfs(pos-1,0,limit&&i==a[pos]);
}
if(!limit)
dp[pos][sta]=ans;
return ans;
}
ll solve(ll x)
{
int pos=0;
while(x)
{
a[pos++]=x%10;
x/=10;
}
return dfs(pos-1,0,true);
}
int main()
{
ll r;
int t;
memset(dp,-1,sizeof(dp));
scanf("%d",&t);
while(t--)
{
cin>>r;
cout<<solve(r)<<endl;
}
return 0;
}