您的位置:首页 > 其它

HDU2089-不要62(数位dp)

2016-09-19 20:06 267 查看


不要62

                                                                            Time Limit: 1000/1000 MS (Java/Others)    Memory Limit:
32768/32768 K (Java/Others)

                                                                                                     Total Submission(s): 35176    Accepted Submission(s): 12793


Problem Description

杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。

杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。

不吉利的数字为所有含有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

 

Source

迎接新学期——超级Easy版热身赛

 

Recommend

lcy

解题思路:dp[i][0]表示不存在不吉利数字,dp[i][1]表示不存在不吉利数字,且最高位为2,dp[i][2]表示存在不吉利数字 

dfs版:dp[pos][sta]表示当前第pos位,前一位是否是6的状态,这里sta只需要去0和1两种状态就可以了,不是6的情况可视为同种,不会影响计数

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

int dp[10][3];

int solve(int n)
{
int len=0,temp=n,ans,flag,a[10];
while(n)
{
a[++len]=n%10;
n/=10;
}
a[len+1]=ans=0;
flag=0;
for(int i=len; i>=1; i--)
{
ans+=dp[i-1][2]*a[i];
if(flag)
ans+=dp[i-1][0]*a[i];
if(!flag&&a[i]>4)
ans+=dp[i-1][0];
if(!flag&&a[i+1]==6&&a[i]>2)
ans+=dp[i][1];
if(!flag&&a[i]>6)
ans+=dp[i-1][1];
if(a[i]==4||(a[i+1]==6&&a[i]==2))
flag=1;
}
return temp-ans;
}

int main()
{
int l,r;
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1; i<=6; i++)
{
dp[i][0]=dp[i-1][0]*9-dp[i-1][1];
dp[i][1]=dp[i-1][0];
dp[i][2]=dp[i-1][2]*10+dp[i-1][0]+dp[i-1][1];
}
while(~scanf("%d %d",&l,&r)&&(l+r))
{
printf("%d\n",solve(r+1)-solve(l));
}
return 0;
}


dfs版

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int a[20];
int dp[20][2];

int dfs(int pos, int pre, 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 tmp = 0;
for (int i = 0; i <= up; i++)
{
if (pre == 6 && i == 2)continue;
if (i == 4) continue;
tmp += dfs(pos - 1, i, i == 6, limit && i == up);
}
if (!limit) dp[pos][sta] = tmp;
return tmp;
}

int solve(int x)
{
int pos = 0;
while (x)
{
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos - 1, -1, 0, true);
}

int main()
{
int a, b;
while (~scanf("%d%d", &a, &b) && a + b)
{
memset(dp, -1, sizeof dp);
printf("%d\n", solve(b) - solve(a - 1));
}
return 0;
}


dfs版:逆向思维

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int a[20];
int dp[20][20][2];

int dfs(int pos, int pre, int sta, bool limit)
{
if (pos == -1) return sta;
if (!limit && dp[pos][pre][sta] != -1) return dp[pos][pre][sta];
int up = limit ? a[pos] : 9;
int tmp = 0;
for (int i = 0; i <= up; i++)
tmp += dfs(pos - 1, i, sta||(pre==6&&i==2)||i==4, limit && i == up);
if (!limit) dp[pos][pre][sta] = tmp;
return tmp;
}

int solve(int x)
{
int pos = 0;
while (x)
{
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos - 1, -1, 0, true);
}

int main()
{
int a, b;
while (~scanf("%d%d", &a, &b) && a + b)
{
memset(dp, -1, sizeof dp);
printf("%d\n", b+1-solve(b) - (a-solve(a - 1)));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: