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
3
1
50
500
Sample Output
0
1
15
Hint
From 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.
思路
题目是让求1~n
中包含数字49
的数的个数,我们可以先求出包含49
的数的个数,然后再用总的数量减去它。
定义:dp[i][j]为有i
位以数字j
开头的不包含数字49
的数的个数。
先预处理出来20之内的所有dp[i][j],最后再分解出数字的每一位,按照转移方程加上去就好了
代码
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include<list>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
typedef long long ll;
ll dp[25][25];
void init()
{
mem(dp,0);
dp[0][0]=1;
for(ll i=1; i<=20; i++)
for(ll j=0; j<=9; j++)//枚举第i位
for(ll k=0; k<=9; k++)//枚举第i-1位
if(!(j==4&&k==9))
dp[i][j]+=dp[i-1][k];
}
ll bit[25];
ll solve(ll n)
{
ll ans=0,len=0;
while(n)
{
bit[++len]=n%10;
n/=10;
}
bit[len+1]=0;
for(ll i=len; i>=1; i--)
{
for(ll j=0; j<bit[i]; j++)
if(!(j==9&&bit[i+1]==4))
ans+=dp[i][j];
if(bit[i+1]==4&&bit[i]==9)
break;
}
return ans;
}
int main()
{
ll t,n;
init();
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
printf("%lld\n",n+1-solve(n+1));
}
return 0;
}
方法2:
和HDU2089 不要62(数位dp,3种写法)这道题一样,也可以使用记忆化搜索,这里的dp[pos][num]表示枚举到第pos为的前一个位置是否为4的状态
代码
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include<list>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
typedef long long ll;
const ll N=25;
ll dp[N][2],bit[N];
ll dfs(ll pos,bool num,bool flag)
{
if(pos==0) return 1;
if(!flag&&dp[pos][num]!=-1)
return dp[pos][num];
ll len=flag?bit[pos]:9;
ll res=0;
for(ll i=0; i<=len; i++)
{
if(num&&i==9)continue;
res+=dfs(pos-1,i==4,flag&&i==len);
}
if(!flag) dp[pos][num]=res;
return res;
}
ll solve(ll n)
{
ll len=0;
while(n)
{
bit[++len]=n%10;
n/=10;
}
mem(dp,-1);
return dfs(len,false,true);
}
int main()
{
ll t,n;
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
printf("%lld\n",n+1-solve(n));
}
return 0;
}