HDU 3652 B-number

来源:互联网 发布:云 大数据 广州 编辑:程序博客网 时间:2024/05/17 04:43

HDU 3652  B-number


第一种和第二种问题的结合,水水过


#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<deque>#include<map>#include<algorithm>using namespace std;typedef long long LL;//#pragma comment(linker, "/STACK:102400000,102400000")const double PI = acos(-1.0);const double eps = 1e-6;const int INF=0x3f3f3f3f;const int N=1e6+10;const int mod = 13;int dp[15][3][15]; // 前i位,首位为1或13的,余13为k的个数int bit[15];int dfs(int pos,int fp,int res,int limit){    if(pos == 0)        return fp==2 && res%mod==0;    if(!limit && dp[pos][fp][res] != -1)        return dp[pos][fp][res];    int up = limit ? bit[pos]:9;    int ans = 0;    for(int i = 0; i <= up; i++)    {        if((i == 3 && fp == 1) || fp == 2)            ans += dfs(pos-1,2,(res*10+i)%mod,i==bit[pos] && limit);        else if(i == 1)            ans += dfs(pos-1,1,(res*10+i)%mod,i==bit[pos] && limit);        else            ans += dfs(pos-1,0,(res*10+i)%mod,i==bit[pos] && limit);    }    if(!limit)        dp[pos][fp][res] = ans;    return ans;}int solve(int a){    int pos = 0;    while(a)    {        bit[++pos] = a%10;        a /= 10;    }    return dfs(pos,0,0,1);}int main(){    int n;    memset(dp,-1,sizeof(dp));    while(~scanf("%d",&n)) {        printf("%d\n",solve(n));    }    return 0;}