数位dp 例题

                                    不要62

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 56059    Accepted Submission(s): 21685

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(a,v)  memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define eps 1.0e-8
using namespace std;
typedef long long ll;
const int MAXL(1e5);
const int INF(0x3f3f3f3f);
ll dp[10][20];
void getdp()
{
    memset(dp,0);
    int i,j,k;
    dp[0][0]=1;
    for(i=1;i<=7;i++)
    {
        for(j=0;j<=9;j++)
        {
            if(j==4)
                dp[i][j]=0;
            else
            if(j==6)
            {
                for(k=0;k<=9;k++)
                {
                    if(k!=2)
                        dp[i][j]+=dp[i-1][k];
                }
            }
            else
            {
                for(k=0;k<=9;k++)
                    dp[i][j]+=dp[i-1][k];
            }
        }
    }
}
ll a[20];
ll solve(ll n)
{
    ll i,j,e=1;
    while(n)
    {
        a[e++]=n%10;
        n/=10;
    }
    a[e]=0;
    ll ans=0;
    for(i=e-1;i>=1;i--)
    {
        for(j=0;j<a[i];j++)
        {
            if(j==4||a[i+1]==6&&j==2)
                continue;
            else
                ans+=dp[i][j];
        }
        if(a[i]==4)
            break;
        if(a[i+1]==6&&a[i]==2)
            break;
    }
    return ans;
}
int main()
{
    ll i,j,ans1,ans2,n,m;
    getdp();
    while(~scanf("%lld%lld",&n,&m)&&(n||m))
    {
        ans1=solve(n);
        ans2=solve(m+1);
        printf("%lld
",ans2-ans1);
    }
    return 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.

2的63次方是9223372036854775808(19位)

思路:可以用上面的模板将49相邻的数计算出来 减掉就行

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(a,v)  memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define eps 1.0e-8
using namespace std;
typedef long long ll;
const int MAXL(1e5);
const int INF(0x3f3f3f3f);
ll dp[20][20];
void getdp()
{
    memset(dp,0);
    int i,j,k;
    dp[0][0]=1;
    for(i=1;i<=20;i++)
    {
        for(j=0;j<=9;j++)
        {
            if(j==4)
            {
                for(k=0;k<=9;k++)
                {
                    if(k!=9)
                        dp[i][j]+=dp[i-1][k];
                }
            }
            else
            {
                for(k=0;k<=9;k++)
                    dp[i][j]+=dp[i-1][k];
            }
        }
    }
}
ll a[20];
ll solve(ll n)
{
    ll i,j,e=1;
    while(n)
    {
        a[e++]=n%10;
        n/=10;
    }
    a[e]=0;
    ll ans=0;
    for(i=e-1;i>=1;i--)
    {
        for(j=0;j<a[i];j++)
        {
            if(a[i+1]==4&&j==9)
                continue;
            else
                ans+=dp[i][j];
        }
        if(a[i+1]==4&&a[i]==9)
            break;
    }
    return ans;
}
int main()
{
    ll i,j,ans1,ans2,n,m,T;
    getdp();
    scanf("%lld",&T);
    while(T--)
    {
        scanf("%lld",&n);
        ans1=solve(n+1);
        printf("%lld
",n-ans1+1);
    }
    return 0;
}
//可以改成这样
//ans1=solve(n+1)-solve(1);
//printf("%lld
",n-ans);
原文地址:https://www.cnblogs.com/zcy19990813/p/9702720.html