Problem Statement
There are some cities and some roads connecting them together. The road network has the topology of a perfect binary tree (see below for a picture), in which the cities are nodes and the roads are edges.
You are given the
The picture below shows how the road network looks like when treeHeight = 2.
We want to send some cars into the road network. Each car will be traveling from its starting city to its destination city without visiting the same city twice. (Note that the route of each car is uniquely determined by its starting and its destination city.) It is possible for the starting city to be equal to the destination city, in that case the car only visits that single city.
Our goal is to send out the cars in such a way that each city will be visited by exactly one car. Compute and return the smallest number of cars we need in order to do so.
Definition
- Class:
- TrafficCongestionDivTwo
- Method:
- theMinCars
- Parameters:
- int
- Returns:
- long
- Method signature:
- long theMinCars(int treeHeight)
- (be sure your method is public)
Notes
- The answer will always fit into a 64-bit signed integer data type.
Constraints
- treeHeight will be between 0 and 60, inclusive.
Examples
0
Returns: 1
1
Returns: 1
In this case, one car can visit all the cities.
2
Returns: 3
Here is one way to visit all cities exactly once by three cars:
3
Returns: 5
4
Returns: 11
5
Returns: 21
6
Returns: 43
7
Returns: 85
8
Returns: 171
9
Returns: 341
10
Returns: 683
11
Returns: 1365
12
Returns: 2731
13
Returns: 5461
14
Returns: 10923
15
Returns: 21845
16
Returns: 43691
17
Returns: 87381
18
Returns: 174763
19
Returns: 349525
20
Returns: 699051
21
Returns: 1398101
22
Returns: 2796203
23
Returns: 5592405
24
Returns: 11184811
25
Returns: 22369621
26
Returns: 44739243
27
Returns: 89478485
28
Returns: 178956971
29
Returns: 357913941
30
Returns: 715827883
31
Returns: 1431655765
32
Returns: 2863311531
33
Returns: 5726623061
34
Returns: 11453246123
35
Returns: 22906492245
36
Returns: 45812984491
37
Returns: 91625968981
38
Returns: 183251937963
39
Returns: 366503875925
40
Returns: 733007751851
41
Returns: 1466015503701
42
Returns: 2932031007403
43
Returns: 5864062014805
44
Returns: 11728124029611
45
Returns: 23456248059221
46
Returns: 46912496118443
47
Returns: 93824992236885
48
Returns: 187649984473771
49
Returns: 375299968947541
50
Returns: 750599937895083
51
Returns: 1501199875790165
52
Returns: 3002399751580331
53
Returns: 6004799503160661
54
Returns: 12009599006321323
55
Returns: 24019198012642645
56
Returns: 48038396025285291
57
Returns: 96076792050570581
58
Returns: 192153584101141163
59
Returns: 384307168202282325
60
Returns: 768614336404564651