Codeforces 834E The Bakery【枚举+数位dp】
E. Ever-Hungry Krakozyabra

Recently, a wild Krakozyabra appeared at Jelly Castle. It is, truth to be said, always eager to have something for dinner.
Its favorite meal is natural numbers (typically served with honey sauce), or, to be more precise, the zeros in their corresponding decimal representations. As for other digits, Krakozyabra dislikes them; moreover, they often cause it indigestion! So, as a necessary precaution, Krakozyabra prefers to sort the digits of a number in non-descending order before proceeding to feast. Then, the leading zeros of the resulting number are eaten and the remaining part is discarded as an inedible tail.
For example, if Krakozyabra is to have the number 57040 for dinner, its inedible tail would be the number 457.
Slastyona is not really fond of the idea of Krakozyabra living in her castle. Hovewer, her natural hospitality prevents her from leaving her guest without food. Slastyona has a range of natural numbers from L to R, which she is going to feed the guest with. Help her determine how many distinct inedible tails are going to be discarded by Krakozyabra by the end of the dinner.
In the first and only string, the numbers L and R are given – the boundaries of the range (1 ≤ L ≤ R ≤ 1018).
Output the sole number – the answer for the problem.
1 10
9
40 57
17
157 165
9
In the first sample case, the inedible tails are the numbers from 1 to 9. Note that 10 and 1 have the same inedible tail – the number 1.
In the second sample case, each number has a unique inedible tail, except for the pair 45, 54. The answer to this sample case is going to be (57 - 40 + 1) - 1 = 17.
题目链接:http://codeforces.com/contest/834/problem/E
官方题解:
下面给出AC代码:
#include <iostream>
#include <cstring>
#include <climits> const int N = ; using LL = int64_t; int a[N], b[N], c[], cc[]; bool check(int i, int need, bool gt, bool lt)
{
if (gt && lt) {
return need <= N - i;
}
if (i == N || need > N - i) {
return false;
}
for (int d = gt ? : a[i]; d <= (lt ? : b[i]); ++ d) {
cc[d] ++;
if (cc[d] <= c[d] && check(i + , need - !!d, gt || a[i] < d, lt || d < b[i])) {
return true;
}
cc[d] --;
}
return false;
} int search(int d, int used)
{
if (d == ) {
memset(cc, , sizeof(cc));
return check(, used, false, false);
}
int result = ;
for (c[d] = ; used + c[d] <= N - ; ++ c[d]) {
result += search(d + , used + c[d]);
}
return result;
} int main()
{
#ifdef LOCAL_JUDGE
freopen("C.in", "r", stdin);
#endif
LL l, r;
while (std::cin >> l >> r) {
l --, r ++;
for (int i = N - ; i >= ; -- i) {
a[i] = l % , b[i] = r % ;
l /= , r /= ;
}
c[] = INT_MAX;
printf("%d\n", search(, ));
}
}
Codeforces 834E The Bakery【枚举+数位dp】的更多相关文章
- CF1277A. Happy Birthday, Polycarp! 题解 枚举/数位DP
题目链接:http://codeforces.com/contest/1277/problem/A 题目大意: 求区间 \([1,n]\) 范围内有多少只包含一个数字的数. 比如:\(1,77,777 ...
- Codeforces 1290F - Making Shapes(数位 dp)
Codeforces 题面传送门 & 洛谷题面传送门 数位 dp 好题. 首先,由于是凸包,一但向量集合确定,凸包的形态肯定就已经确定了.考虑什么样的向量集合能够组成符合条件的凸包,我们假设第 ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- BZOJ 1799 - [AHOI2009]self 同类分布 - 枚举 数位DP
Description 找出$[L, R]$ 区间内有多少数, 各位数字和 能整除原数 Solution 枚举每个可能的数字和, 进行数位DP即可 , 水爆 Code #include<cstd ...
- CodeForces 55D Beautiful numbers(数位dp+数学)
题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道 ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- Codeforces Gym 100418J Lucky tickets 数位DP
Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- CodeForces 55D Beautiful numbers(数位dp)
数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...
- CodeForces 628 D Magic Numbers 数位DP
Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...
随机推荐
- 【python】局部变量和全局变量
- C++ 头文件系列(set)
简介 头文件包含set.multiset两个类模版,这里要描述的概念与map非常相似,甚至连成员函数都几乎一样,所以这篇随笔会很短. set set如果翻译成中文应该是集合的意思,这里更确切的说是唯一 ...
- Linux根目录详解-转自鸟哥的私房菜
转自:http://myhat.blog.51cto.com/391263/107931/ *根目录(/)的意义与内容: 根目录是整个系统最重要的一个目录,因为不但所有的目录都是由根目录衍生出来的 ...
- Java订单号生成,唯一订单号(日均千万级别不重复)
Java订单号生成,唯一订单号 相信大家都可以搜索到很多的订单的生成方式,不懂的直接百度.. 1.订单号需要具备以下几个特点. 1.1 全站唯一性. 1.2 最好可读性. 1.3 随机性,不能重复,同 ...
- Rstudio( bioconductor)下载太慢,用国内镜像
在Rstudio中,下载软件install.packages()和 bioconductor软件下载命令 source("http://bioconductor.org/biocLite.R ...
- angularjs 怎么获取鼠标焦点 鼠标移入显示浮动的div提示框
首先,我们要清楚几个基础的知识,angular的两个鼠标移入移出的指令------ng-mouseover(鼠标移入)ng-mouseleave(鼠标移出)--------还有就是window.eve ...
- volatile关键字是如何起作用的?
关键字volatile是Java虚拟机提供的最轻量级的同步机制,但是在平时的项目里面,遇到需要多线程的时候更多地使用的是synchronized关键字来进行同步.个人而言,更多的原因是对volatil ...
- 基础环境之Docker入门
随着Docker技术的不断成熟,越来越多的企业开始考虑使用Docker.Docker有很多的优势,本文主要讲述了Docker的五个最重要优势,即持续集成.版本控制.可移植性.隔离性和安全性. 有了Do ...
- JDBC详解系列(三)之建立连接(DriverManager.getConnection)
在JDBC详解系列(一)之流程中,我将数据库的连接分解成了六个步骤. JDBC流程: 第一步:加载Driver类,注册数据库驱动: 第二步:通过DriverManager,使用url,用户名和密码 ...
- lbp特征提取(等价模式)
LBP等价模式 考察LBP算子的定义可知,一个LBP算子可以产生多种二进制模式(p个采样点)如:3x3邻域有p=8个采样点,则可得到2^8=256种二进制模式:5x5邻域有p=24个采样点,则可得到2 ...