Codeforces Round #426 (Div. 1) C. Ever-Hungry Krakozyabra(搜索)
来源:互联网 发布:od跳过网络验证 编辑:程序博客网 时间:2024/06/01 09:41
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.
分析:看范围像是数位dp,但是推一下就会发现不好做,我们可以贪心地用最小表示dp出小于某个值的所有方案数,但是我们不能直接拿R的方案数直接减L的,考虑直接枚举答案,这是一个组合问题,设a[i]表示数字i在组合中出现了多少次,那么sigma(a[i]) <= 18 且a[i] >= 0,这是线性方程整数解问题,
答案最多C(27,9)个,不到10^7,那么我们枚举所有可能的组合然后判断可不可行就可以了,判断可不可行也可以用常规数位dp的那种思路,设两个限制分别表示有没有超过上界和下界,然后直接递归判定,中间最多开一次叉,所以这个判定是线性的.
#include <bits/stdc++.h>#define MOD 1000000007using namespace std;typedef long long ll;int a[20],b[20],f[10],pos;ll l,r,ans;bool check(int pos,bool Ll,bool Lr){ if(!Ll && !Lr) return true; if(pos == -1) return true; int up = Lr ? b[pos] : 9; int down = Ll ? a[pos] : 0; if(up < down) return false; bool flag; for(int i = down;i <= up;i++) if(f[i]) { f[i]--; flag = check(pos-1,Ll && i == a[pos],Lr && i == b[pos]); f[i]++; if(flag) return true; } return false;}void dfs(int k,int tot){ if(k == 9) { f[k] = tot; if(check(pos-1,1,1)) ans++; return; } for(int i = 0;i <= tot;i++) { f[k] = i; dfs(k+1,tot - i); }}int main(){ cin.sync_with_stdio(false); cin>>l>>r; pos = 0; while(l) { a[pos++] = l % 10; l /= 10; } pos = 0; while(r) { b[pos++] = r % 10; r /= 10; } dfs(0,pos); cout<<ans<<endl;}
- Codeforces Round #426 (Div. 1) C. Ever-Hungry Krakozyabra(搜索)
- Codeforces Round #426 (Div. 2) E-Ever-Hungry Krakozyabra (爆搜)
- Codeforces Round #197 (Div. 2) C. Xenia and Weights - 搜索
- Codeforces Round #247 (Div. 2)(C)记忆化搜索
- Codeforces Round #428 (Div. 2)-搜索&期望&细节-C. Journey
- Codeforces Round #426 (Div. 2) C
- Codeforces Round #426 (Div. 2) C
- Codeforces Round #426 (Div. 2) C
- 【dp】Codeforces Round #110 (Div. 1) C
- Codeforces Round #142 (Div. 1) C. Triangles
- Codeforces Round #230 (Div. 1)B,C
- Codeforces Round #259 (Div. 1)C题
- Codeforces Round #228 (Div. 1) C 贪心
- Codeforces Round #284 (Div. 1) C
- Codeforces Round #286 (Div. 1) C、D
- Codeforces Round #302 (Div. 1) C
- Codeforces Round #333 (Div. 1) C
- Codeforces Round #336 (Div. 1) C. Marbles
- Linux 定时任务cron配置
- ThinkPHP URL中去除index.php
- linux 下的nc使用
- 《Machine learning for OpenCV》 Windows下的Anaconda+OpenCV的环境配置
- 基于Java+SparkStreaming整合kafka编程
- Codeforces Round #426 (Div. 1) C. Ever-Hungry Krakozyabra(搜索)
- 初来乍到
- Kendo控件封装
- 转盘抽奖特效
- 矩阵分析与应用(二)——内积与范数
- zoj2588(tarjan求割边)
- Codeforces 834D The Bakery DP(线段树维护)
- html5学习笔记
- 解决 Button 设置 background 之后点击动画效果消失的问题