题目链接: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的更多相关文章

  1. light oj 1032(数位DP)

    求一段区间中,每个十进制数所对应的二进制数中连续的1的个数之和. 设dp[i][0]代表长度为i的二进制数,首位为0,所含有的连续的1的个数之和. dp[i][1]代表长度为i的二进制数,首位为1,所 ...

  2. LightOJ - 1032 数位DP

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  3. LightOJ 1032 - Fast Bit Calculations 数位DP

    http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...

  4. Light OJ 1032 - Fast Bit Calculations(数位DP)

    题目大意: 一个数字把他看成二进制数字,数字里又会一些相邻的1,问从0到n至间所有相邻1的总和是多少?   分解成2进制数字,然后数位DP就行了.   ======================== ...

  5. 2018.09.07 loj#10166 数字游戏(数位dp)

    传送门 数位dp板子题. f[i][mod]" role="presentation" style="position: relative;"> ...

  6. 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 ...

  7. [转]数位dp小记

    转载自:http://blog.csdn.net/guognib/article/details/25472879 参考: http://www.cnblogs.com/jffifa/archive/ ...

  8. 数位dp 的简单入门

    时间紧张,就不讲那么详细了. 之前一直被深搜代码误解,以为数位dp 其实就是记忆化深搜...(虽说爆搜确实很舒服而且还好想) 但是后来发现数位dp 的标准格式其实是 预处理 + dp ...... 数 ...

  9. 数位DP 计划

    通常的数位dp可以写成如下形式: [cpp] view plain copy int dfs(int i, int s, bool e) { if (i==-1) return s==target_s ...

随机推荐

  1. Selenium WebDriver 处理table

    首先,html table是由 table 元素以及一个或多个 tr.th 或 td 元素组成. for example: 这是一个简单的html table: 源码如下: <html> ...

  2. progressBar走马灯设置

    初始值Visible = false; 让progressBar1出现时: progressBar1.Visible = true; progressBar1.Style = ProgressBarS ...

  3. Match:Blue Jeans(POJ 3080)

    DNA序列 题目大意:给你m串字符串,要你找最长的相同的连续字串 这题暴力kmp即可,注意要按字典序排序,同时,是len<3才输出no significant commonalities #in ...

  4. vs2013显示行号

    随便打开一个项目,可以看到代码框内并没有显示行号 选择“工具”-“选项”,打开后界面如下 选择文本编辑器,找到下图中的“行号”并勾选 行号可以显示了 5 这样我们就完成了任务

  5. Memcache使用

    //需要下载memcache 服务 然后 在命令里面 安装和启动服务 //引用 Memcached.ClientLibrary.dllpublic class MemcacheHelper { pub ...

  6. 【leetcode】Subsets (Medium) ☆

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  7. xcode报错,svn : is not a workingCopy

    解决方案: 1.点击对应的targets 2.选择build phases 3.在红框处有一个run script选项(截图中已经删除了.),点击下拉按钮,看看是否是与svn有关的东西, 如果是,删除 ...

  8. net 页面跳转

    前台: < a href="xx.html" target="_blank"> 后台: Response.Redirect("XXX.as ...

  9. 解决SQL Server的cannot resolve the collation conflict问题

    当没有牵涉到两个不同的数据库时,出现以上错误.   Cannot resolve the collation conflict between "Chinese_PRC_CI_AS" ...

  10. !对c++类的理解

    c++的类可以分为两类,一种是entity的类(i.e.,实体类),一种是function的类(i.e.,功能类). 对于构造entity的类,包括这种entity的属性已经它本身具备的功能: 而fu ...