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
列举1到n中带有13或被13整除的整数的数量
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=50;
ll dp[50][50][50];
int a[maxn];
ll dfs(int pos,int n0,int n1,bool lead,bool limit)
{
if(pos==0)
{
if(lead) return 1;
return n0>=n1;
}
if(!limit&&!lead&&dp[pos][n0][n1]!=-1) return dp[pos][n0][n1];
int up=limit?a[pos]:1;
ll ans=0;
for(int i=0;i<=up;i++)
{
if(lead)
{
if(i) ans+=dfs(pos-1,n0,n1+1,0,limit&&i==a[pos]);
else ans+=dfs(pos-1,0,0,1,limit&&i==a[pos]);
}
else
{
if(i) ans+=dfs(pos-1,n0,n1+1,0,limit&&i==a[pos]);
else ans+=dfs(pos-1,n0+1,n1,0,limit&&i==a[pos]);
}
}
if(!limit&&!lead) dp[pos][n0][n1]=ans;
return ans;
}
ll solve(ll x)
{
int pos=0;
while(x)
{
a[++pos]=x%2;
x/=2;
}
return dfs(pos,0,0,1,1);
}
int main()
{
ll l,r;
int t;
while(~scanf("%lld%lld",&l,&r)&&(l+r))
{
memset(dp,-1,sizeof(dp));
printf("%lld\n",solve(r)-solve(l-1));
}
}