loj 1032 数位dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1032
思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 pos处,前一位为pre, 当前有have个满足条件的状态。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; long long dp[][][];
int n, digit[]; long long dfs(int pos, int pre, int have, int doing)
{
if (pos == -) {
return have;
}
if (!doing && dp[pos][pre][have] != -) {
return dp[pos][pre][have];
}
int end = doing ? digit[pos] : ;
long long ans = ;
for (int i = ; i <= end; i++) {
int nhave = have;
if (pre == && i == ) {
nhave++;
}
ans += dfs(pos - , i, nhave, i == end && doing);
}
if (!doing) {
dp[pos][pre][have] = ans;
}
return ans;
} long long Solve(int n)
{
int pos = ;
while (n) {
digit[pos] = n % ;
n /= ;
pos++;
}
return dfs(pos - , , , );
} int main()
{
memset(dp, -, sizeof(dp));
int _case, t = ;
scanf("%d", &_case);
while (_case--) {
scanf("%d", &n);
printf("Case %d: %lld\n", t++, Solve(n));
}
return ;
}
loj 1032 数位dp的更多相关文章
- light oj 1032(数位DP)
求一段区间中,每个十进制数所对应的二进制数中连续的1的个数之和. 设dp[i][0]代表长度为i的二进制数,首位为0,所含有的连续的1的个数之和. dp[i][1]代表长度为i的二进制数,首位为1,所 ...
- LightOJ - 1032 数位DP
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
- LightOJ 1032 - Fast Bit Calculations 数位DP
http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...
- Light OJ 1032 - Fast Bit Calculations(数位DP)
题目大意: 一个数字把他看成二进制数字,数字里又会一些相邻的1,问从0到n至间所有相邻1的总和是多少? 分解成2进制数字,然后数位DP就行了. ======================== ...
- 2018.09.07 loj#10166 数字游戏(数位dp)
传送门 数位dp板子题. f[i][mod]" role="presentation" style="position: relative;"> ...
- lightoj 1032 - Fast Bit Calculations(数位dp)
A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true&quo ...
- [转]数位dp小记
转载自:http://blog.csdn.net/guognib/article/details/25472879 参考: http://www.cnblogs.com/jffifa/archive/ ...
- 数位dp 的简单入门
时间紧张,就不讲那么详细了. 之前一直被深搜代码误解,以为数位dp 其实就是记忆化深搜...(虽说爆搜确实很舒服而且还好想) 但是后来发现数位dp 的标准格式其实是 预处理 + dp ...... 数 ...
- 数位DP 计划
通常的数位dp可以写成如下形式: [cpp] view plain copy int dfs(int i, int s, bool e) { if (i==-1) return s==target_s ...
随机推荐
- keepalived+haproxy构建高可用负载均衡
一.环境介绍 我用的是centos6.7,内核版本为2.6.32-573.el6.x86_64,keepalived版本为keepalived-1.2.22,haproxy版本为haproxy-1.6 ...
- iOS 在UITableViewCell中加入自定义view时view的frame设定注意
由于需要重用同一个布局,于是在cellForRowAtIndexPath中把自定义view加在了cell上,我是这样设定view的frame的 var screenFrame = UIScreen.m ...
- 【转】Java-----jar反编译修改重新打包
原文链接:http://blog.csdn.net/hekewangzi/article/details/44676797 一.使用反编译工具JD-GUI(JD-GUI相关操作见Java-----反编 ...
- VC++ LoadLibrary失败,错误126(找不到指定的模块)
在VS中调用一个资源模块dll,LoadLibrary返回值为NULL,没有加载成功.GetLastError后原因为"找不到指定的模块"!代码如下: HINSTANCE hIns ...
- code vs 1506 传话
codevs 1506 传话(时间限制: 1 s 空间限制: 128000 KB) 题目描述 Description 一个朋友网络,如果a认识b,那么如果a第一次收到某个消息,那么会把这个消息传给b, ...
- 【leetcode】Maximal Rectangle (hard)★
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 【python】SQLAlchemy
来源:廖雪峰 对比:[python]在python中调用mysql 注意连接数据库方式和数据操作方式! 今天发现了个处理数据库的好东西:SQLAlchemy 一般python处理mysql之类的数据库 ...
- August 7th 2016, Week 33rd Sunday
Knowing yourself is the height of wisdom. 了解自己就是大智慧. Two-day holiday, even I didn't have enought tim ...
- 项目差异class文件提取-->上线用
package fileReader; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStre ...
- SQL Server多表多列更新
student表: lag表: 要求将student表stu_id列为1的stu_nick列和stu_phont列的数据更新为lag表的lag_nick列和lag_phone列. SQL语句: upd ...