题目
杭州人称那些傻乎乎粘嗒嗒的人为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
解题思路
虽然这题能够直接枚举。。。
但这道题也是可以用dp做的
设
f[i][j][0]表示前i-1位数未满,第i位选j的方案数。
f[i][j][1]表示前i-1位数已满,第i为选j的方案数。
(已满的意思是 前i-1位数与给出的上限的前i-1位数均相同
未满的意思是 前i-1位数与给出的上限的前i-1位数有至少一位数不相同
举个例子:上限为43567888
已满:i=5时对于前四位为4356这个转态,第五位能放0~7之间的 数
未满:i=5时对于前四位为4350这个转态,第五位能放0~9之间的数
)
代码
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int f[20][20][2];
int r(int x)
{
if (x==0) return 1;
int a[20],b[20];
a[0]=0;
while (x!=0)
{
a[++a[0]]=x%10;
x/=10;
}
for (int i=1;i<=a[0];i++)
b[i]=a[a[0]-i+1];
b[0]=0;
int len=a[0];
memset(f,0,sizeof(f));
for (int i=0;i<b[1];i++)
if (i!=4) f[1][i][0]=1;
if (b[1]!=4) f[1][b[1]][1]=1;
for (int i=2;i<=len;i++)
for (int j=0;j<=9;j++)
{
for (int k=0;k<=9;k++)
if (k!=4)
{
if (j==6 && k==2) continue;
f[i][k][0]+=f[i-1][j][0];
if (k<b[i]) f[i][k][0]+=f[i-1][j][1];
if (k==b[i])
{
f[i][k][1]+=f[i-1][j][1];
}
}
// f[i][k][0] 表示 前面i-1个数未满
// f[i][k][1] 表示 前面i-1个数已经满
}
int ans=0;
for (int i=0;i<=9;i++)
if (i!=4) ans+=f[len][i][0]+f[len][i][1];
return ans;
}
int main()
{
int a,b;
while (true)
{
scanf("%d%d",&a,&b);
if (a==0 && b==0) break;
cout<<r(b)-r(a-1)<<endl;
}
}
HDU - 3555
题目
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
3
1
50
500
Sample Output
0
1
15
解题思路
该题与上一题类似。。。
代码
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
long long f[30][30][2];
long long r(long long x)
{
int a[30],b[30];
a[0]=0;
while (x!=0)
{
a[++a[0]]=x%10;
x/=10;
}
for (int i=1;i<=a[0];i++)
b[i]=a[a[0]-i+1];
b[0]=0;
int len=a[0];
memset(f,0,sizeof(f));
for (int i=0;i<b[1];i++)
f[1][i][0]=1;
f[1][b[1]][1]=1;
for (int i=2;i<=len;i++)
{
for (int j=0;j<=9;j++)
for (int k=0;k<=9;k++)
{
if (j==4 && k==9) continue;
f[i][k][0]+=f[i-1][j][0];
if (k<b[i]) f[i][k][0]+=f[i-1][j][1];
if (k==b[i]) f[i][k][1]+=f[i-1][j][1];
}
}
long long ans=0;
for (int i=0;i<=9;i++)
ans+=f[len][i][0]+f[len][i][1];
return ans;
}
int main()
{
int T;
cin>>T;
while (T--)
{
long long x;
cin>>x;
cout<<x-(r(x)-1)<<endl;
}
}