HDU

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<ctype.h>
#include<map>
#include<set>
#include<string>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<stack>
using namespace std;
#define LL long long
LL dp[45][45][3],num[45];
LL dfs(int pos,int mod,int st,int limit)//pos为数位从高位开始枚举(达到上限再枚举下一位),mod为对13求余,初始为0,st为状态,初始为0,limit为是否达到上限,1达到上限,0没有达到上限继续枚举
{
    if(pos<0)
    return mod==0&&st==2;
    if(!limit&&dp[pos][mod][st]!=-1)
        return dp[pos][mod][st];
    int len=limit?num[pos]:9;//判断上一位是否到上限,若达到则只能枚举到num【pos】,否则可以枚举0——9
    int modx,stx;
    LL ans=0;
    for(int i=0;i<=len;i++)
    {
        modx=(mod*10+i)%13;
        stx=st;
        if(st==1&&i!=1)
            stx=0;
        if(st==0&&i==1)
            stx=1;
        if(st==1&&i==3)
            stx=2;
        ans+=dfs(pos-1,modx,stx,limit&&i==len);
    }
    if(!limit)//没有达到上限的都要存储一下该状态下的符合条件数;
        dp[pos][mod][st]=ans;
    return ans;

}
LL sv(LL a)
{
    int len=0;
    memset(dp,-1,sizeof(dp));
    memset(num,-1,sizeof(num));
    while(a)
    {
        num[len++]=a%10;
        a/=10;
    }
    dfs(len-1,0,0,1);
}
int main()
{
    LL n;
    while(~scanf("%lld",&n))
    {
        LL ee=sv(n);
        printf("%lld
",ee);
    }
}
原文地址:https://www.cnblogs.com/nr1999/p/9401262.html