数位DP HDU3652
B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5000 Accepted Submission(s): 2866
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.
计算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 ;
}
数位DP HDU3652的更多相关文章
- [暑假集训--数位dp]hdu3652 B-number
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- ...
- HDU3652 B-number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3652 B-number Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- 【HDU3652】B-number 数位DP
B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose de ...
- hdu3652(数位dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意:求1~n含有13且能被13整除的数的个数. 分析:数位dp,dp数组加一维来维护到pos位 ...
- HDU3652(数位dp)
A - B-number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descri ...
- hdu3652 B-number 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意就是求区间内能被13整除并且包含”13“的数字的个数 感觉是比较中等的数位DP题目 我用的记 ...
- 【Hdu3652】B-number(数位DP)
Description 题目大意:求小于n是13的倍数且含有'13'的数的个数. (1 <= n <= 1000000000) Solution 数位DP,题目需要包含13,且被13整除, ...
- hdu3652 数位dp记忆化搜索
从未见过的船新版本数位dp,,省去了预处理过程,直接进行计算 #include<bits/stdc++.h> using namespace std; #define ll long lo ...
- HDU3652 B-number(数位DP)题解
思路: 这里的状态分为3种,无13和末尾的1,无13且末尾为1,有13,然后DFS 等我搞清楚数位DP就来更新Orz 代码: #include<iostream> #include< ...
随机推荐
- zoj 3471(状态压缩)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 dp[state]表示当前状态为state时的所能获得的最大值 ...
- 映射一对多双向关联关系 cascade、inverse、属性
当类与类之间建立了关联,就可以方便的从一个对象导航到另一个对象.或者通过集合导航到一组对象.例如: 对于给定的Emp对象,如果想获得与它关联的Dept对象,只要调用如下方法 Dept dept=emp ...
- cdoj1324暴力分块
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> ...
- felx项目属性(二)
order flex-grow flex-shrink flex-basis flex align-self 1.1 order css order属性规定了弹性容器中的可伸缩项目在布局时的顺序.元素 ...
- background为圆角的表框,dp转Px,Px转dp
圆角边框<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="ht ...
- BroadCast,BroadCastReceiver
效果是:一个MainActivity动态注册一个BroadcastReceiver,BActivity发送一个标准广播,MainActivity接收到广播后,将广播中的消息显示在MainActivit ...
- hdu3448 01背包+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound f ...
- 运用Python成为黑客
1.Fuzz测试: 漏洞挖掘有三种方法:白盒代码审计.灰盒逆向工程.黑盒测试.其中黑盒的Fuzz测试是效率最高的一种,能够快速验证大量潜在的安全威胁. Fuzz测试,也叫做"模糊测试&quo ...
- shell-bash学习03 别名、日期、函数
别名 使用alias 创建 alias new_command='command sequence' 保存 echo 'alias cmd="command seq"' >& ...
- codeforces 519E A and B and Lecture Rooms LCA倍增
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...