hdu 3652 B-number 数位DP

思路:数位DP

 

 1 #include<cstdio>

 2 #include<cstring>

 3 int bit[10],dp[10][10][13][2];

 4 int dfs(int pos,int pre,int mod,bool h,bool inf)

 5 {

 6     if(pos==-1) return h&&!mod;

 7     if(!inf&&dp[pos][pre][mod][h]!=-1) return dp[pos][pre][mod][h];

 8     int i,ans=0;

 9     int e=inf?bit[pos]:9;

10     for(i=0;i<=e;i++){

11         if(pre==1&&i==3) ans+=dfs(pos-1,i,(mod*10+i)%13,1,inf&&(i==bit[pos]));

12         else ans+=dfs(pos-1,i,(mod*10+i)%13,h,inf&&(i==bit[pos]));

13     }

14     if(!inf) dp[pos][pre][mod][h]=ans;

15     return ans;

16 }

17 int cal(int n)

18 {

19     int pos=0;

20     while(n){

21         bit[pos++]=n%10;

22         n/=10;

23     }

24     return dfs(pos-1,0,0,0,1);

25 }

26 int main()

27 {

28     int n,ans,m,k;

29     memset(dp,-1,sizeof(dp));

30     while(scanf("%d",&n)!=EOF){

31         printf("%d\n",cal(n));

32     }

33 }
View Code

 

 

你可能感兴趣的:(number)