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.
Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
Output
Print each answer in a single line.
Sample Input
13
100
200
1000
Sample Output
1
1
2
2
/*flag:初值为0,若有13变为1
limit:初值为1,若有限制则为0
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int f[20][20][20][20];
int dig[20];
int dfs(int pos,int pre,int flag,int limit,int val)//flag是判断是否有13的。而val是判断能否被13整除的
{
int ans=0;
if(pos==-1)//到了最后一位
{
if(flag!=1)//没有13,返回0
return 0;
else//否则继续判断是否可被13整除
{
if(val==0)
return 1;
else
return 0;
}
}
if(!limit&&f[pos][pre][flag][val]!=-1)
return f[pos][pre][flag][val];
int up=limit?dig[pos]:9;
for(int i=0;i<=up;i++)
{
// ans+=dfs(pos-1,i,pre==1&&i==3||flag,limit==0&&i==up,(val*10+i)%13);//此处(vai*10+i)%13)用的很是巧妙,可以用手算除法的那种式子解释
ans+=dfs(pos-1,i,(pre==1&&i==3)||flag,limit&&i==up,(val*10+i)%13);
}
return limit?ans:f[pos][pre][flag][val]=ans;
}
int n;
int solve()
{
//int lala=x;
int tt=0;
while(n)
{
dig[tt++]=n%10;
n/=10;
}
return dfs(tt-1,0,0,1,0);
}
int main()
{
memset(f,-1,sizeof(f));
while(~scanf("%d",&n))
{
cout<<solve()<<endl;
}
return 0;
}