天天看點

Codeforces 834E The Bakery【枚舉+數位dp】

E. Ever-Hungry Krakozyabra

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

Codeforces 834E The Bakery【枚舉+數位dp】

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

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

題目連結:http://codeforces.com/contest/834/problem/E

官方題解:

Codeforces 834E The Bakery【枚舉+數位dp】

下面給出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.

Codeforces 834E The Bakery【枚舉+數位dp】

我的公衆号是IT老實人(IThonest),一個有故事的公衆号,歡迎大家來這裡讨論,共同進步,不斷學習才能不斷進步。掃下面的二維碼或者收藏下面的二維碼關注吧(長按下面的二維碼圖檔、并選擇識别圖中的二維碼),個人QQ和微信的二維碼也已給出,掃描下面👇的二維碼一起來讨論吧!!!

Codeforces 834E The Bakery【枚舉+數位dp】

歡迎大家關注我的Github,一些文章的備份和平常做的一些項目會存放在這裡。

繼續閱讀