Codeforces 833C. 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.

Input

In the first and only string, the numbers L and R are given – the boundaries of the range (1 ≤ L ≤ R ≤ 1018).

Output

Output the sole number – the answer for the problem.

Examples

Note

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.

题意简述

对于一个数,将其的各个位置上的数,按从小到大排序,删去前导零,可以表示成另一个数。

现在给出 $l$ 和 $r$,问区间 $[l,r]$ 内的数所表示的数共有多少种。

分析

两个数被视为同一种,当且仅当两个数中 $1$ ~ $9$ 出现的次数都相同,如果我们把两个数都补齐到同样位数,那就相当于是 $0$ ~ $9$ 出现的次数都相同。

我们把位数限制在 $18$ 位,暴力搜一下,就会发现情况数只有四百多万种,也就是说我们可以枚举所有情况判断每种情况是否可以用给定区间内的数表示。

考场上写的是找出可以表示这种情况的、比 $r$ 小的最大的数,再和 $l$ 比较,结果 TLE 了。

实际上应该要把 $l$ 和 $r$ 一起放进去暴搜判断(想想其实也不难理解,可行性问题往往比最优化问题更容易解决),考虑一下是否顶上界和下界,如果上下界都没有限制了,就一定可以,其他情况考虑一下下一步是否还顶着界,能不顶就不顶,具体看代码吧。

这个搜索看似有分叉,实际上最多分叉一次,也就是说最坏复杂度不是指数级而是线性(相对于数的位数),实测用上记忆化搜索因为要清空数组反而更慢。

 

发表评论