Problem Statement
Below is a simple ASCII art showing one specific rendering of the digits 0 through 9 on a seven-segment digital display. This particular "font" is the one used in this problem.
0 1 2 3 4 5 6 7 8 9 +-+ + + +-+ +-+ + + +-+ +-+ +-+ +-+ +-+ | | | | | | | | | | | | | | + + + + +-+ +-+ +-+ +-+ +-+ + + +-+ +-+ | | | | | | | | | | | | | +-+ + + +-+ +-+ + + +-+ +-+ + + +-+ +-+
Note that if the individual segments that form a digit touch at their endpoints, some of the digits enclose one or two areas completely: the digits 0, 6 and 9 enclose one area each, the digit 8 encloses two.
This generalizes naturally to arbitrary positive integers. For example, a digital representation of the number 1000 contains three enclosed areas, 8968 contains six, and 12345 contains none.
For a given value E we now consider the sequence of all positive integers that have exactly E enclosed areas in their digital representation. We will assign ordinal numbers to the elements of this sequence, starting with number 0 for the smallest such integer.
Given E and N, return the integer that will have the ordinal number N in the above sequence.
Definition
- Class:
- EnclosedArea
- Method:
- construct
- Parameters:
- int, int
- Returns:
- long
- Method signature:
- long construct(int E, int N)
- (be sure your method is public)
Notes
- For the constraints given below it is guaranteed that the return value is smaller than 10^18 (and therefore fits into a signed 64-bit integer).
Constraints
- E will be between 0 and 20, inclusive.
- N will be between 0 and 10^9, inclusive.
Examples
1
0
Returns: 6
We want the smallest positive integer with one enclosed area. That integer is 6. (Note that 0 is not a positive integer.)
7
1
Returns: 8088
Here we want the second smallest positive integer with 7 enclosed areas. (The smallest is 6888.)
7
8
Returns: 8898
The first eight integers in this sequence (i.e., those with ordinals 0-7) are 6888, 8088, 8688, 8808, 8868, 8880, 8886, and 8889. The next one is the one we seek.
0
123456789
Returns: 17125735114
Integers with no enclosed areas are simply integers written only using digits 1, 2, 3, 4, 5, and 7. There is a fairly obvious relationship between this sequence and integers written in base 6. Note that the return value does not fit into a 32-bit integer. Watch out for overflows!
4
123456
Returns: 805519
0
999999998
Returns: 243121245343
0
67135996
Returns: 7354542445
0
55
Returns: 132
1
1000000000
Returns: 31755339173
1
612881816
Returns: 17413516711
1
43
Returns: 129
2
999999997
Returns: 11973547951
2
550233048
Returns: 5139172027
2
49
Returns: 266
3
999999999
Returns: 5963032275
3
660954037
Returns: 3925027359
3
5
Returns: 108
4
999999999
Returns: 5117038107
4
707195253
Returns: 3596177097
4
3
Returns: 388
5
999999994
Returns: 5584756787
5
672808
Returns: 6269093
5
33
Returns: 3898
6
1000000000
Returns: 7056748784
6
777236230
Returns: 5816861414
6
69
Returns: 9988
7
999999993
Returns: 9748915058
7
985054900
Returns: 9639674380
7
40
Returns: 38868
8
999999999
Returns: 16910811060
8
247011136
Returns: 5786877186
8
30
Returns: 83888
9
999999998
Returns: 30355869818
9
474831280
Returns: 16604825860
9
30
Returns: 268888
10
999999993
Returns: 64886760309
10
602310884
Returns: 41018887805
10
47
Returns: 868889
11
999999998
Returns: 98909099050
11
645292743
Returns: 84168832288
11
43
Returns: 2888808
12
999999996
Returns: 268566668867
12
256215501
Returns: 86889661979
12
93
Returns: 8880808
13
999999993
Returns: 663979886989
13
134754254
Returns: 106800607868
13
42
Returns: 26888888
14
999999994
Returns: 1006809580800
14
389595110
Returns: 728618988698
14
74
Returns: 88088088
15
999999999
Returns: 3287888889458
15
545789266
Returns: 1888861607686
15
69
Returns: 289888888
16
999999997
Returns: 8087881480866
16
104258988
Returns: 1688888008973
16
83
Returns: 880880888
17
999999996
Returns: 18614880876888
17
995373686
Returns: 18601858786888
17
89
Returns: 3888868888
18
999999998
Returns: 60858698488869
18
234670574
Returns: 18688986900968
18
60
Returns: 8388888888
19
999999994
Returns: 89069888996860
19
830082728
Returns: 88809189888601
19
53
Returns: 18888898888
20
999999998
Returns: 386078880890898
20
724619712
Returns: 287889082889808
20
36
Returns: 78888888888
20
1000000000
Returns: 386078880891888
0
999918851
Returns: 243115421537