标签:tar lse and output mem ++i ide hdu inpu
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.
求从1到N,含有13且能被13整除的数的个数
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
Print each answer in a single line.
13
100
200
1000
1
1
2
2
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int bit[15],dp[14][14][2][2];
int dfs(int x,int mod,int last,int have,int flag)//
{
if(x==1) return (!mod&&have);
if(!flag&&dp[x][mod][last][have]!=-1) return dp[x][mod][last][have];
int count=flag?bit[x-1]:9,ans=0;
for(int i=0;i<=count;++i)
{
if(last&&i==3) ans+=dfs(x-1,(mod*10+i)%13,0,1,flag&&i==count);
else ans+=dfs(x-1,(mod*10+i)%13,i==1,have,flag&&i==count);
}
return flag?ans:dp[x][mod][last][have]=ans;
}
int start(int x)
{
int len=0;
for(;x;x/=10) bit[++len]=x%10;
memset(dp,-1,sizeof(dp));
return dfs(len+1,0,0,0,1);
}
int main()
{
int n;
while(~scanf("%d",&n)) printf("%d\n",start(n));
return 0;
}
标签:tar lse and output mem ++i ide hdu inpu
原文地址:https://www.cnblogs.com/wuwendongxi/p/13307151.html