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】字符串格式化
- SQLMap安装步骤
SQLMap是利用Python语言写的,所以需要将Python这个语言环境给安装上 : 1.首先下载Python(这里Python版本为2.7.2,可以下载不同或高版本的) 2.然后在下载sqlmap ...
- Python学习日记day10------函数的命名空间、作用域与闭合函数
1,参数陷阱 如果默认参数的只是一个可变数据类型,那么每一次调用的时候,如果不传值就共用这个数据类型的资源. 2,三元运算 c=a if a>b else b#如果a>b返回a,否则,返回 ...
- Hibernate问题浅析
1.什么是SessionFactory?什么是Session?httpsession和hibernate的session的有什么区别? SessionFactory接口负责初始化Hiber ...
- ArcGIS Runtime SDK是什么?
如上图,Runtime SDK是什么东西?居然还有安卓.苹果手机.Mac.QT的版本? 是不是意味着ArcGIS的编辑数据和空间分析可以通过编程的方法在每个平台上满地跑了? 答案是:是,也不是. 1. ...
- P、NP、NP完全问题
如果一个算法的最差时间效率属于O(p(n)),则该算法可以在多项式的时间内对问题进行求解,其中p(n)是输入规模n的一个多项式函数. 可以在多项式时间内求解的问题是易解的.不能在多项式时间内求解的问题 ...
- python+selenium自动化测试_1
前言 回顾一下python+selenium基础,并整理相关知识点,分享给有需要,在前进道路上的朋友. print打印 #打印Hello World print("Hello World&q ...
- springBoot系列教程05:fastjson的集成、配置及使用
springBoot自带的json用着不太习惯,已习惯了fastJSON,下面介绍下fastjson的配置 1. pom引入 <dependency> <groupId>com ...
- what is cdecl?
cdecl这是一个Linux上功能很强大的一个小型程序,它最主要的功能就是能帮助我们解释一个很复杂的C语言声明. 例如,我写了这样一个C语言的一个声明:int *(*g[])(int , floa ...
- Git详解之七:自定义Git
自定义 Git 到目前为止,我阐述了 Git 基本的运作机制和使用方式,介绍了 Git 提供的许多工具来帮助你简单且有效地使用它. 在本章,我将会介绍 Git 的一些重要的配置方法和钩子机制以满足自定 ...