B-number( 数位 DP )

这篇博客探讨了如何计算在1到n之间包含连续数字'13'并且能被13整除的整数,即WQB数的数量。文章通过动态规划的方法给出了解决方案,利用了数字对模运算的性质,实现了从高位到低位的遍历计算,并避免了重复计算。示例代码展示了如何高效地计算满足条件的WQB数的个数。

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

B-number

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

给定的数据要能够被 13 整除且其中存在连续的 13
对于一个数来说最后取模等同于对每一位进行取模,举个栗子!
21554%13 == (((2*10+1)%13)*10+5)%13…

f [ pos ][ mul ][ pre ][ st ] ;pos 当前的位数,mul 当前数字取模后的余数,pre 上一位的数字,st 是否存在连续的 13

AC代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N=30;
int n;
ll p[N];
ll f[N][20][15][2];

ll dp(int pos,int mul,int pre,int st,int flag)
{
	if(pos==-1) return mul==0&&st;
	if(flag&&f[pos][mul][pre][st]!=-1) return f[pos][mul][pre][st];
	ll ans=0;
	int up=flag?9:p[pos];
	for(int i=0;i<=up;i++)
	{
		ans+=dp(pos-1,(mul*10+i)%13,i,(pre==1&&i==3)||st,flag||i<up);
	}
	if(flag) f[pos][mul][pre][st]=ans;
	return ans;
} 

ll slove(ll x)
{
	int pos=0;
	while(x)
	{
		p[pos++]=x%10;
		x/=10;
	}
	return dp(pos-1,0,0,0,0);
}

int main()
{
	while(cin>>n)
	{
		memset(f,-1,sizeof f);
		cout<<slove(n)<<endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在人间负债^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值