1. 程式人生 > >正向記憶化DFS實現的數位DP

正向記憶化DFS實現的數位DP

Problem E

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 17   Accepted Submission(s) : 13

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.

Input

Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).

Output

Print each answer in a single line.

Sample Input

13
100
200
1000

Sample Output

1
1
2
2

題目大意:是13的倍數以及含有13的數有幾個

程式碼:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[15][15][15],bit[30];
ll dfs(ll n,ll t,ll mo,bool up)
{
    ll hehe=0,nt,m,upp;
    if(n==0) return mo==0&&t==2;
    if(up==0&&dp[n][t][mo]!=-1) return dp[n][t][mo];
    if(up==1) upp=bit[n];
    else upp=9;
    for(int i=0;i<=upp;i++)
    {
        nt=t;
    	m=(mo*10+i)%13;
        if(t==0&&i==1) nt=1;
        if(t==1&&i!=1) nt=0;
        if(t==1&&i==3) nt=2;
        hehe+=dfs(n-1,nt,m,up&&i==upp);
    }
    if(up==0) dp[n][t][mo]=hehe;
    return hehe;
}
ll ans(ll m)
{
    ll len;
    for(len=0;m>0;m/=10) bit[++len]=m%10;
    return dfs(len,0,0,true);
}
int main()
{
    ll a,t;
    memset(dp,-1,sizeof(dp));
    while(cin>>a) cout<<ans(a)<<endl;
}