当前数位DP还不理解的点:

1:出口用i==0的方式

2:如何省略状态d(就是枚举下一个数的那个状态。当然枚举还是要的,怎么把空间省了)

总结:

1:此类DP,考虑转移的时候,应当同时考虑查询时候的情况。

2:考虑x在第i位之后,能遍历多少数字,其答案为(x%10i-1+1)

3:这里的记忆化搜索不太一样喔,出口一定要写在递归里,不然,查询状态下差到出口就会出错了~

类型:

数位DP

题意:

求[A,B]区间内的所有数,写下来之后,0的个数。(a,b 为 unsigned int)

思路:

我的笨拙暴力状态:

dp[i][d][okPre] 表示d开头的i位数,(okPre表示计算前导0的情况下,反之~),的0的个数。

那么。

dp[i][d][含] = dp[i-1][0~9(num[i-1])][含] + 10i-1(x%10i-1+1) * (d==0);

dp[i][d][不含] = dp[i-1][1~9(num[i-1])][含] + dp[i-1][0][d==0?不含:含] ;

出口:

dp[1][1~9][含] =dp[1][1~9][不含] = 0;

dp[1][0][不含] = dp[1][0][含] = 1;

当时确定出口的时候,不含的0应该是0还是1呢?不好确定,感觉是1,最后是通过试验确定的。

还是没有理解别人代码中 用 i==0 做出口 是怎么实现的。

我的代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; long long nowx;
long long dp[][][];
int num[]; long long dfs(int i, int d, bool okPreZero, bool isQuery) {
//printf("(%d,%d,%s,%s)\n", i, d, okPreZero?"T":"F", isQuery?"T":"F");
long long &nowdp = dp[i][d][okPreZero];
if (!isQuery && ~nowdp) return nowdp;
if (i == ) {
if (d!=) return nowdp = ;
else if (okPreZero) {
return nowdp = ;
} else {
return nowdp = ;
}
}
long long ans = ;
int end = isQuery?num[i-]:;
for (int j = ; j <= end; j++) {
if (okPreZero) {
ans += dfs(i-,j,true,isQuery && j==end);
} else {
if (d == && j == ) {
ans += dfs(i-,j,false,isQuery && j==end);
} else {
ans += dfs(i-, j, true, isQuery && j==end);
}
}
}
long long ten = ;
for (int j = ; j < i-; j++) ten*=;
if (d== && okPreZero) ans += (isQuery?((nowx%ten)+):(ten));
if (!isQuery) nowdp = ans;
return ans;
} long long cal(long long x) {
nowx = x;
if (x == -) return ;
if (x == ) return ;
int len = ;
while (x) {
num[++len] = x%;
x/=;
}
return dfs(len+, , false, true);
} int Nmain() {
long long a;
memset(dp, -, sizeof(dp));
while (cin>>a) {
cout<<"---"<<cal(a)<<endl;
}
return ;
} int main() {
int t;
cin>>t;
int cas = ;
memset(dp, -, sizeof(dp));
while (t--) {
long long m, n;
cin>>m>>n;
cout<<"Case "<<cas++<<": "<<cal(n)-cal(m-)<<endl;
}
return ;
}

不理解的代码:

#include<cstdio>
#include<cstring>
#include<cmath>
typedef long long LL;
LL dp[][][];
int bit[],len;
LL a,b;
LL dfs(int pos,int v,int flag,int limit)
{
if (pos<=) return flag?v:;
if (!limit&&dp[pos][v][flag]!=-) return dp[pos][v][flag];
int end=(limit?bit[pos]:);
LL re=;
for (int i=;i<=end;i++)
{
int tmp;
if (flag&&(i==)) tmp=;else tmp=;
re+=dfs(pos-,v+tmp,flag||i,limit&&(end==i));
}
if (!limit) dp[pos][v][flag]=re;
return re;
} LL solve(LL n)
{ if (n==-) return -;
if (n==) return ;
len=;
while (n)
{ bit[++len]=n%;
n/=;
}
return dfs(len,,,);
}
int main()
{ memset(dp,,sizeof(dp));
int cas,i=;
scanf("%d",&cas);
while (cas--)
{scanf("%lld%lld",&a,&b);
printf("Case %d: %lld\n",++i,solve(b)-solve(a-));
}
return ;
}

LightOJ 1140: How Many Zeroes? (数位DP)的更多相关文章

  1. LightOJ 1140 How Many Zeroes? (数位DP)

    题意:统计在给定区间内0的数量. 析:数位DP,dp[i][j] 表示前 i 位 有 j 个0,注意前导0. 代码如下: #pragma comment(linker, "/STACK:10 ...

  2. light oj 1140 - How Many Zeroes? 数位DP

    思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...

  3. UVALive - 6575 Odd and Even Zeroes 数位dp+找规律

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48419 Odd and Even Zeroes Time Limit: 3000MS 问题描述 In mat ...

  4. LightOJ 1032 - Fast Bit Calculations 数位DP

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

  5. lightoj 1021 - Painful Bases(数位dp+状压)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...

  6. LightOJ 1140 How Many Zeroes

    题意:写出一个给定区间的每个数,求出一共写了多少个零. 解法:数位DP,定义dp[len][flag][num]:len的定义为数位的长度,flag定义为前导0和没有前导0的两种状态,num定义为写的 ...

  7. lightoj 1140 - How Many Zeroes?(数位dp)

    Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...

  8. LightOJ - 1140 统计0的数位 数位DP

    注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...

  9. 数位dp(D - How Many Zeroes? LightOJ - 1140 )

    题目链接:https://cn.vjudge.net/contest/278036#problem/D 题目大意:T组测试数据,每一次输入两个数,求的是在这个区间里面,有多少个0,比如说19203包括 ...

随机推荐

  1. numpy学习(一)

    numpy数据类型 # numpy创建对象,numpy创建的对象是n阶矩阵,类似python中列表的嵌套 nd = np.array([[1,2,3,4,5],[2,3,4,6,5]])nd 结果: ...

  2. RSA与AES实现数据加密传输

    RSA.AES简介 RSA:非对称加密,需要提前生成两个密钥(一对的),通过其中一个密钥加密后的数据,只有另一个密钥能解密.通常这两个密钥中有一个会暴漏出来,即对外公开的,这个密钥称为“公钥”,反之另 ...

  3. cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

    参考  http://blog.csdn.net/mazicwong/article/details/54946952 1.到https://curl.haxx.se/ca/cacert.pem复制下 ...

  4. DC 课程内容

  5. python可视化动态图表: 关于pyecharts的sankey桑基图绘制

    最近因工作原因,需要处理一些数据,顺便学习一下动态图表的绘制.本质是使具有源头的流动信息能够准确找到其上下级关系和流向. 数据来源是csv文件 导入成为dataframe之后,列为其车辆的各部件供应商 ...

  6. iOS 中的视图函数 init initwithnib viewDidLoad viewWillAppear的总结

    我要总结的函数主要是这几个: UIView *view-如果view还没有被初始化的话,getter方法会先调用[self loadView],如果getter或者setter方法被重写了,子类中的g ...

  7. poj 1017 装箱子问题 贪心算法

    题意:有1*1到6*6的的东西,需要用6*6的箱子将它们装起来.问:至少需要多少个6*6箱子 思路: 一个瓶子怎么装东西最多?先装石头,在装沙子,然后装水. 同样放在本题就是先装6*6然后5*5... ...

  8. BZOJ 4057: [Cerc2012]Kingdoms

    状压DP #include<cstdio> #include<cstring> using namespace std; int F[1200005],A[25][25],st ...

  9. UVa 11651 Krypton Number System DP + 矩阵快速幂

    题意: 有一个\(base(2 \leq base \leq 6)\)进制系统,这里面的数都是整数,不含前导0,相邻两个数字不相同. 而且每个数字有一个得分\(score(1 \leq score \ ...

  10. Python类元编程

    类元编程是指在运行时创建或定制类.在Python中,类是一等对象,因此任何时候都可以使用函数创建新类,而无需用class关键字.类装饰器也是函数,不过能够审查.修改,甚至把被装饰的类替换成其他类.元类 ...