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代码:
1 #include <iostream> 2 #include <cstring> 3 #include <climits> 4 5 const int N = 19; 6 7 using LL = int64_t; 8 9 int a[N], b[N], c[10], cc[10]; 10 11 bool check(int i, int need, bool gt, bool lt) 12 { 13 if (gt && lt) { 14 return need <= N - i; 15 } 16 if (i == N || need > N - i) { 17 return false; 18 } 19 for (int d = gt ? 0 : a[i]; d <= (lt ? 9 : b[i]); ++ d) { 20 cc[d] ++; 21 if (cc[d] <= c[d] && check(i + 1, need - !!d, gt || a[i] < d, lt || d < b[i])) { 22 return true; 23 } 24 cc[d] --; 25 } 26 return false; 27 } 28 29 int search(int d, int used) 30 { 31 if (d == 10) { 32 memset(cc, 0, sizeof(cc)); 33 return check(0, used, false, false); 34 } 35 int result = 0; 36 for (c[d] = 0; used + c[d] <= N - 1; ++ c[d]) { 37 result += search(d + 1, used + c[d]); 38 } 39 return result; 40 } 41 42 int main() 43 { 44 #ifdef LOCAL_JUDGE 45 freopen("C.in", "r", stdin); 46 #endif 47 LL l, r; 48 while (std::cin >> l >> r) { 49 l --, r ++; 50 for (int i = N - 1; i >= 0; -- i) { 51 a[i] = l % 10, b[i] = r % 10; 52 l /= 10, r /= 10; 53 } 54 c[0] = INT_MAX; 55 printf("%d\n", search(1, 0)); 56 } 57 }
作 者:Angel_Kitty
出 处:https://www.cnblogs.com/ECJTUACM-873284962/
关于作者:阿里云ACE,目前主要研究方向是Web安全漏洞以及反序列化。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
欢迎大家关注我的微信公众号IT老实人(IThonest),如果您觉得文章对您有很大的帮助,您可以考虑赏博主一杯咖啡以资鼓励,您的肯定将是我最大的动力。thx.

我的公众号是IT老实人(IThonest),一个有故事的公众号,欢迎大家来这里讨论,共同进步,不断学习才能不断进步。扫下面的二维码或者收藏下面的二维码关注吧(长按下面的二维码图片、并选择识别图中的二维码),个人QQ和微信的二维码也已给出,扫描下面👇的二维码一起来讨论吧!!!

欢迎大家关注我的Github,一些文章的备份和平常做的一些项目会存放在这里。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述