(数位dp)B-number

本文介绍了一种求解Awqb-number的算法,Awqb-number是指十进制形式包含子串“13”且能被13整除的非负整数。文章详细解析了如何通过动态规划的方法,计算从1到给定整数n范围内满足条件的Awqb-number数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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));

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值