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.

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
input
1 10
output
9
input
40 57
output
17
input
157 165
output
9
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.


分析:看范围像是数位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;}



原创粉丝点击