【HDOJ 3652】B-number
给一整数n 找<=n的整数中能被13整除且含有13的 数位dp 记忆化!
。
一入记忆化深似海。
。。再也不想用递推了。。。发现真的非常好想 仅仅要保证满足条件把未选高位(即能任意挑数的)记录下来 不断搜索递归即可
代码例如以下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[10][13][3];
int digit[10];
/*
hs =
0 无13
1 无13 前位为1
2 出现13
mod表示高位取余后的结果 遍历完后mod == 0说名能被13整除 按位取余
*/
int dfs(int pos,int mod,int hs,bool high)
{
if(pos == -1) return hs == 2 && !mod;
if(!high && ~dp[pos][mod][hs]) return dp[pos][mod][hs];
int i,en,ans = 0,nhs,nmd;
en = high? digit[pos]: 9;
for(i = 0; i <= en; ++i)
{
nmd = (mod*10+i)%13;
nhs = hs;
if(nhs == 1 && i == 3) nhs = 2;
else if(nhs != 2) nhs = (i == 1)? 1: 0;
ans += dfs(pos-1,nmd,nhs,high && i == en);
}
if(!high) dp[pos][mod][hs] = ans;
return ans;
}
int Solve(int x)
{
int len = 0;
while(x)
{
digit[len++] = x%10;
x /= 10;
}
return dfs(len-1,0,0,1);
}
int main()
{
memset(dp,-1,sizeof(dp));
int n;
while(~scanf("%d",&n)) printf("%d\n",Solve(n));
return 0;
}
【HDOJ 3652】B-number的更多相关文章
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...
- 【HDOJ 5379】 Mahjong tree
[HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共同拥有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根 ...
- 【HDOJ 2089】不要62
[HDOJ 2089]不要62 第一个数位dp的题 做的老困难了...只是好歹是做出来了 迈出了第一步.. 对大牛来说这样的题都是小case ps:新上一个记忆化方法 一些绕弯的题里用dfs好想些 代 ...
- 【HDOJ 5371】 Hotaru's problem
[HDOJ 5371] Hotaru's problem Manacher算法+穷举/set Manacher算法一好文:http://blog.csdn.net/yzl_rex/article/de ...
- 【HDOJ 5399】Too Simple
pid=5399">[HDOJ 5399]Too Simple 函数映射问题 给出m函数 里面有0~m个函数未知(-1) 问要求最后1~n分别相应仍映射1~n 有几种函数写法(已给定的 ...
- 【HDOJ 5419】 Victor and Toys (排列组合)
[HDOJ 5419] Victor and Toys n个玩具 m个区间 每一个玩具有一个beauty值 问任选三个区间 三区间的MINleft~MAXright的和的期望值 预处理一个数组 存放每 ...
- 【HDOJ 2255】奔小康赚大钱(KM算法)
[HDOJ 2255]奔小康赚大钱(KM算法) 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 【HDOJ 1009】 CRB and String
[HDOJ 1009] CRB and String 每组两个串s t 仅仅由小写字母组成 问从s能不能变成t 改变的操作为选一个字符 在后面加上一个与所选字符不同的字符 这样的操作能够做无数次 问能 ...
- 【HDOJ 2063】过山车
[HDOJ 2063]过山车 二分图最大匹配模板题 1女对n男 问匹配最大对数 代码例如以下: #include <iostream> #include <cstdlib> # ...
随机推荐
- ubuntu14.04 configure: error: xml2-config not found. Please check your libxml2 installation错误解决
今天在ubuntu14.04上安装php7时 执行:./configure命令时 一直报configure: error: xml2-config not found. Please check yo ...
- 嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av13570243/?from=search&seid=15873837810484552531 中的15-23讲
#coding=gbk#嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av13570243/?from=search&seid=1587383 ...
- fstream,sstream的学习记录
fstream: #include<iostream> #include<fstream> using namespace std; int main(){ ofstream ...
- URI跟URL的区别
关于URL和URI的区别,个人见解. 初学java,最近被一个概念搞得头晕脑胀,就是url和uri的概念和区别,网上查了一大通,发现各种回答眼花缭乱,有百科直接粘贴的,有胡说八道的,有故意绕来绕 ...
- zoj 2724 Windows Message Queue
Windows Message Queue Time Limit: 2 Seconds Memory Limit: 65536 KB Message queue is the basic f ...
- 生产环境下lnmp的权限说明
https://www.cnblogs.com/zrp2013/p/4183546.html 有关权限说明:-rwxrw-r‐-1 root root 1213 Feb 2 09:39 50.html ...
- 【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle
http://acm.hdu.edu.cn/showproblem.php?pid=3986 [题意] 给定一个有重边的无向图,T=20,n<=1000,m<=5000 删去一条边,使得1 ...
- visual studio用"查找替换"来删掉源代码中所有//方式的纯注释和空行
visual studio用"查找替换"来删掉源代码中所有//方式的纯注释和空行 注意:包括/// <summary>这样的XML注释也都删掉了. 步骤1/2(删除注释 ...
- Free Web Application Firewall相关资料
http://www.freewaf.org/solution/#1 http://baike.soso.com/v60659982.htm
- 一个APP的由来
之前在站酷.UI中国.优设等网站看过不少的APP教程.规范等一些东西.自认为有些规范讲的内容过于繁琐,对于像我这样的大多数设计师来说看着看着就懵逼了....