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的倍数. ...
随机推荐
- iOS 计时器三种定时器的用法NSTimer、CADisplayLink、GCD
原文:http://www.cocoachina.com/ios/20160919/17595.html DEMO链接
- ASP.NET Cookie 概述
什么是 Cookie? Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递.Cookie 包含每次用户访问站点时 Web 应用程序都可以读取的信息. 例如,如果在用 ...
- ecsmart的开发经历
ecsmart是ecshop的产品之一,是2015年发布的一套系统.关于它的介绍请到官网去了解,也可以百度 “商之翼” “ecsmart” 1.ecsmart分别在pc.mobile.app三个方面都 ...
- lesson - 11 课程笔记
一.sed 作用: sed 是一种流编辑器,它是文本处理中非常重要的工具, 能够完美的配合正则表达式使用.处理时,把当前处理的行存储在临时缓冲区中, 称为“模式空间(pattern space)”, ...
- 轮播插件、原生js编写,弄懂这个,基本上各种轮播都可以自己写了
直接上代码了: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- 消息队列一:为什么需要消息队列(MQ)?
为什么会需要消息队列(MQ)? #################################################################################### ...
- sql sever[基本] ''增删改'' 随笔
结构语言分类 DDL(数据定义语言) create drop alter 创建删除以及修改数据库,表,存储过程,触发器,索引.... DML(数据操作语言) insert delete ...
- DBCC page 数据页 堆 底层数据分布大小计算
1.行的总大小: Row_Size = Fixed_Data_Size + Variable_Data_Size + Null_Bitmap + 4(4是指行标题开销) 开销定义: Fixed_Dat ...
- php 事务处理transaction
MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成 ...
- C语言的学习
一.文件的使用方式 r 只读 rb只读 r+ rb+(不带b的为已存在的文本文件,带b的为二进制文件(binary),带+号的为读写文件) w 只写 wb只写 a 追加 ab追加 二.说明 1 ...