LightOJ 1140: How Many Zeroes? (数位DP)
当前数位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)的更多相关文章
- LightOJ 1140 How Many Zeroes? (数位DP)
题意:统计在给定区间内0的数量. 析:数位DP,dp[i][j] 表示前 i 位 有 j 个0,注意前导0. 代码如下: #pragma comment(linker, "/STACK:10 ...
- light oj 1140 - How Many Zeroes? 数位DP
思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...
- UVALive - 6575 Odd and Even Zeroes 数位dp+找规律
题目链接: http://acm.hust.edu.cn/vjudge/problem/48419 Odd and Even Zeroes Time Limit: 3000MS 问题描述 In mat ...
- LightOJ 1032 - Fast Bit Calculations 数位DP
http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...
- lightoj 1021 - Painful Bases(数位dp+状压)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...
- LightOJ 1140 How Many Zeroes
题意:写出一个给定区间的每个数,求出一共写了多少个零. 解法:数位DP,定义dp[len][flag][num]:len的定义为数位的长度,flag定义为前导0和没有前导0的两种状态,num定义为写的 ...
- lightoj 1140 - How Many Zeroes?(数位dp)
Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...
- LightOJ - 1140 统计0的数位 数位DP
注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...
- 数位dp(D - How Many Zeroes? LightOJ - 1140 )
题目链接:https://cn.vjudge.net/contest/278036#problem/D 题目大意:T组测试数据,每一次输入两个数,求的是在这个区间里面,有多少个0,比如说19203包括 ...
随机推荐
- 201621123080 《Java程序设计》第13周学习总结
201621123080 <Java程序设计>第13周学习总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能( ...
- Qt:实现子线程发送信号父线程切换图片
mainwindow.h中代码 #ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QMainWindow>#include " ...
- angular5 HttpInterceptor使用
HttpInterceptor接口是ng的http请求拦截器,当需要拦截http请求,可以实现该接口. 1.创建HttpInterceptor 的实现类,并使用@Injectable()注解 @Inj ...
- RSA非对称加密算法实现过程
RSA非对称加密算法实现过程 非对称加密算法有很多,RSA算法就是其中比较出名的算法之一,下面是具体实现过程 <?php /** */ class Rsa { /** * private key ...
- 使用 Python 编写登陆接口
# 使用 Python 编写登陆接口# Create Date: 2017.10.31 Tuesday# Author: Eric Zhao# -*- coding:utf-8 -*-'''编写登陆接 ...
- python中字符串的一些用法
一.字符串的拼接: a=‘123’ b=‘abc’ d=‘hello world’ 1.print(a+b) 2.print(a,b) 3. c=‘ ’.join((a ...
- 使用python3调用MyQR库生成动态二维码(附源代码)
可生成普通二维码.带图片的艺术二维码(黑白与彩色).动态二维码(黑白与彩色). GitHub:https://github.com/sylnsfar/qrcode 中文版:https://github ...
- JAVA基础篇—Servlet小结
一.get请求和post请求的区别: 1.get请求是通过url传递参数,post请求是通过请求体传递参数的 2.get请求最多允许传递255个字符,对长度有限制,所以数据比较大的时候我们使用post ...
- RMQ原理及实现
RMQ(Range Minimum/Maximum Query),区间最值查询问题,是指:对于长度为n的数列A,回答若干次询问RMQ(i,j),返回数列A中下标在区间[i,j]中的最小/大值. 这里介 ...
- 使用html+javascriptt实现的简易四则运算(初学JavaScript笔记)
今天第一天学javascript,做了个简易的四则运算,提供参考,效果图: html代码: <!DOCTYPE html> <html > <head > < ...