当前位置: 代码迷 >> 综合 >> Bomb ( 数位 DP )
  详细解决方案

Bomb ( 数位 DP )

热度:93   发布时间:2023-11-23 12:46:03.0

Bomb

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.

给定一个数字,判断小于这个数字的所有数中有多少个数中有连续的 49 ,要注意是连续的才算
f [ pos ][ pre ][ flag1 ] ;pos 代表当前的位数,pre 代表上一位的位数,flag1 代表是否存在 49

AC 代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N=300;
int t;
ll p[N];
ll f[N][10][2];ll dp(int pos,int pre,int flag1,int flag2)
{
    if(pos==-1) return flag1;if(flag2&&f[pos][pre][flag1]!=-1) return f[pos][pre][flag1];ll ans=0;int up=flag2?9:p[pos];for(int i=0;i<=up;i++){
    ans+=dp(pos-1,i,(pre==4&&i==9)||flag1,flag2||i<up);}if(flag2) f[pos][pre][flag1]=ans;return ans;
} ll slove(ll x)
{
    int pos=0;while(x){
    p[pos++]=x%10;x/=10;}return dp(pos-1,0,0,0);
}int main()
{
    cin>>t;while(t--){
    memset(f,-1,sizeof f);ll x;cin>>x;cout<<slove(x)<<endl;}return 0;
}