数位DP模板

来源:互联网 发布:端口能ping吗 编辑:程序博客网 时间:2024/06/09 13:53

不管什么鬼,直接long long 上

#include <cstdio>#include <cstring>#include <iostream>using namespace std;#define ll long longconst int maxn=20;int bit[maxn];ll dp[maxn][2][2][13]; //dp[i][j][k][k]:长度为i,是否以1结尾,有无13,模13的结果//[a,b]中含13且取模13为0的数个数ll dfs(int len,bool end1,bool have13,int mod,bool ismax){    if(len==0)    return (!mod && have13);    if(!ismax && (dp[len][end1][have13][mod] != -1))    return dp[len][end1][have13][mod];    int pmax = ismax ? bit[len] : 9;    ll cnt = 0;    for (int i = 0; i <= pmax; ++i){        int tmod = (mod*10+i) % 13;        cnt += dfs(len-1,i==1,have13||(end1&&i==3),tmod,ismax && i == pmax);    }    return ismax ? cnt : dp[len][end1][have13][mod] = cnt;}ll solve(ll n){    int cnt=0;    while(n>0){        bit[++cnt]=n%10;        n/=10;    }    return dfs(cnt,false,false,0,true);}int main(){    ll a,b;    memset(dp,-1,sizeof(dp));    while(cin>>a>>b){        cout<<solve(b)-solve(a-1)<<endl;    }    return 0;}

相关题目链接

0 0