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));
}
}
本文介绍了一种求解Awqb-number的算法,Awqb-number是指十进制形式包含子串“13”且能被13整除的非负整数。文章详细解析了如何通过动态规划的方法,计算从1到给定整数n范围内满足条件的Awqb-number数量。
B-number&spm=1001.2101.3001.5002&articleId=82716469&d=1&t=3&u=4a9a062d7dba41aab5119cc4e59cf485)
2233

被折叠的 条评论
为什么被折叠?



