数位DP HDU3652
2024-10-15 07:49:43
B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5000 Accepted Submission(s): 2866
Problem Description
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.
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
Author
wqb0039
Source
题意:
计算N以内的数中含有13并且能被13整除的数的个数
计算N以内的数中含有13并且能被13整除的数的个数
代码:
/*
记忆化搜索+数位DP,不是很理解这一套路,dp[i][j][k],i表示位数,j表示余数,k=0表示没有13,k=1表示末尾是1,
k=2表示有13.一个数除以13可以是前几位除以13的余数连上后几位再除以13......
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int n;
int dp[][][];
int c[];
int dfs(int lne,int mod,int have,int lim) //lim代表是否为上限
{
if(lne<=) //没有位数了返回符合的情况
return mod==&have==;
if(!lim&&dp[lne][mod][have]!=-) //没有上限并且已被访问过
return dp[lne][mod][have];
int num=lim?c[lne]:; //假设该位是2,下一位是3,如果现在算到该位为1,那么下一位是能取到9的,
//如果该位为2,下一位只能取到3
int ans=;
for(int i=;i<=num;i++)
{
int nmod=(mod*+i)%; //看是否能整除13,而且由于是从原来数字最高位开始算,
//事实上这个过程就是一个除法过程
int nhave=have;
if(have==&&i==) nhave=; //末尾不是1,现在加入的是1
if(have==&&i!=&&i!=) nhave=; //末尾是1,现在加入的不是1
if(have==&&i==) nhave=; //末尾是1,现在加入的是3
ans+=dfs(lne-,nmod,nhave,lim&&i==num); //lim&&i==num,在最开始,取出的num是最高位,
//所以如果i比num小,那么i的下一位都可以到达9,而i==num了,最大能到达的就只有,c[len-1]
}
if(!lim)
dp[lne][mod][have]=ans; //dp只记录没有限的值
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(dp,-,sizeof(dp));
int cnt=;
while(n)
{
c[++cnt]=n%;
n/=;
}
printf("%d\n",dfs(cnt,,,));
}
return ;
}
最新文章
- Javascript之旅——第五站:说说那些所谓的包装类型
- Android之Inflate()方法用途
- NXP QN9020
- Div 自适应屏幕大小
- 用javascript预加载图片、css、js的方法研究
- C#。3.1 循环(叠加、穷举)
- 编写自己的单点登录(SSO)服务
- gcc 源代码分析-前端篇3
- [BZOJ1061] [Noi2008] 志愿者招募 (费用流)
- MySQL性能基准测试对比:5.7 VS 8.0
- datetime模块处理时间
- Python十讲 - 第二讲:变量和基础数据类型
- doc 常用命令
- [React] Asynchronously Load webpack Bundles through Code-splitting and React Suspense
- OOAD之面向对象设计原则
- 基于C#的机器学习--面部和动态检测-图像过滤器
- Installation Guide for Appium 1.6.3
- HDU 6194 string string string (后缀数组)
- 【usaco-Earthquake, 2001 Open】 0-1分数规划 &; 最优比率生成树
- 【python】抄写大神的百度贴吧代码