BZOJ1833或洛谷2602 [ZJOI2010]数字计数
BZOJ原题链接
洛谷原题链接
又是套记搜模板的时候。。
对\(0\sim 9\)单独统计。
定义\(f[pos][sum]\),即枚举到第\(pos\)位,前面枚举的所有位上是当前要统计的数的个数之和为\(sum\)。
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 13;
ll f[N][N];
int a[N], nw;
inline ll re()
{
ll x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
ll dfs(int pos, int s, int lm, int zero)
{
if (pos < 0)
return s;
if (!lm && !zero && f[pos][s] > -1)
return f[pos][s];
int i, k = lm ? a[pos] : 9;
ll S = 0;
for (i = 0; i <= k; i++)
S += dfs(pos - 1, s + ((!nw && !i && !zero) || (nw && i == nw)), lm && i == a[pos], zero && !i);
if (!lm && !zero)
return f[pos][s] = S;
return S;
}
ll calc(ll x)
{
int k = 0;
memset(f, -1, sizeof(f));
while (x > 0)
{
a[k++] = x % 10;
x /= 10;
}
return dfs(k - 1, 0, 1, 1);
}
int main()
{
ll x, y;
x = re();
y = re();
for (nw = 0; nw <= 9; nw++)
printf("%lld ", calc(y) - calc(x - 1));
return 0;
}
BZOJ1833或洛谷2602 [ZJOI2010]数字计数的更多相关文章
- 洛谷P2602 [ZJOI2010]数字计数 题解 数位DP
题目链接:https://www.luogu.com.cn/problem/P2602 题目大意: 计算区间 \([L,R]\) 范围内 \(0 \sim 9\) 各出现了多少次? 解题思路: 使用 ...
- 洛谷 P2602 [ZJOI2010]数字计数
洛谷 第一次找规律A了一道紫题,写篇博客纪念一下. 这题很明显是数位dp,但是身为蒟蒻我不会呀,于是就像分块打表水过去. 数据范围是\(10^{12}\),我就\(10^6\)一百万一百万的打表. 于 ...
- 洛谷P2602 [ZJOI2010]数字计数(数位dp)
数字计数 题目传送门 解题思路 用\(dp[i][j][k]\)来表示长度为\(i\)且以\(j\)为开头的数里\(k\)出现的次数. 则转移方程式为:\(dp[i][j][k] += \sum_{t ...
- [洛谷P2602][ZJOI2010]数字计数
题目大意:求区间$[l,r]$中数字$0\sim9$出现个数 题解:数位$DP$ 卡点:无 C++ Code: #include <cstdio> #include <iostrea ...
- 洛谷P2602 [ZJOI2010]数字计数 题解
题目描述 输入格式 输出格式 输入输出样例 输入样例 1 99 输出样例 9 20 20 20 20 20 20 20 20 20 说明/提示 数据规模与约定 分析 很裸的一道数位DP的板子 定义f[ ...
- 洛谷P2602 [ZJOI2010] 数字计数 (数位DP)
白嫖的一道省选题...... 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 usin ...
- 洛谷 P2606 [ZJOI2010]排列计数 解题报告
P2606 [ZJOI2010]排列计数 题目描述 称一个\(1,2,...,N\)的排列\(P_1,P_2...,P_n\)是\(Magic\)的,当且仅当对所以的\(2<=i<=N\) ...
- 洛谷P2606 [ZJOI2010]排列计数(组合数 dp)
题意 题目链接 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案 ...
- ●洛谷P2606 [ZJOI2010]排列计数
题链: https://www.luogu.org/problemnew/show/P2606题解: 组合数(DP),Lucas定理 首先应该容易看出,这个排列其实是一个小顶堆. 然后我们可以考虑dp ...
随机推荐
- hdu5505-GT and numbers-(贪心+gcd+唯一分解定理)
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- k8s 创建deployment流程
pod 创建流程https://blog.csdn.net/yan234280533/article/details/72567261 api server -> etcd -> cont ...
- java.net.NoRouteToHostException
1.之前一直默认的一个请求url,后来后台ip更换后,就报上述错误.网上好多方法说:关闭服务器端的防火墙,但试过没用. 问题有待重新测试解决
- CentOS7下解决yum install mysql-server没有可用包
# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm -- http://repo.mysql.com/mysq ...
- Ajax图片异步上传并回显
1.jsp页面 <td width="20%" class="pn-flabel pn-flabel-h"></td> <td w ...
- shell重定向命令执行顺序
重定向内容介绍 一条shell命令的执行包含三个文件描述符:标准输入(键盘等) stdin 0,标准正确输出(屏幕等) stdout 1,标准错误输出(屏幕等)stderr 2 通过重定向可以指定 ...
- Python_变量命名
Python的变量命名 变量的命名的原则一直都是我这种小白的头疼事,好几条,根本记不住...... 为了解决大家的头疼问题,今天想出来一个好办法,那就是:身边常备头疼片.......(哈哈哈,开玩笑的 ...
- 静态方法调用内部类时候的new 问题
package tool; /** * 静态方法调用内部类时候的new 问题 */ public class aa { // 静态方法 // 静态方法new 有问题 public static voi ...
- JAVA 基本数据结构--数组、链表、ArrayList、Linkedlist、hashmap、hashtab等
概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的实现. ...
- 179. Largest Number(INT, String)
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...