forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPotentialGeometricSequence.txt
90 lines (48 loc) · 2.34 KB
/
PotentialGeometricSequence.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
PROBLEM STATEMENT
We have a sequence of N positive integers: a[0] through a[N-1].
You do not know these integers.
All you know is the number of trailing zeros in their binary representations.
You are given a vector <int> d with N elements.
For each i, d[i] is the number of trailing zeros in the binary representation of a[i].
For example, suppose that a[0]=40.
In binary, 40 is 101000 which ends in three zeros.
Therefore, d[0] will be 3.
You like geometric sequences.
(See the Notes section for a definition of a geometric sequence.)
You would like to count all non-empty contiguous subsequences of the sequence a[0], a[1], ..., a[N-1] that can be geometric sequences (given the information you have in d).
More precisely:
For each pair (i,j) such that 0 <= i <= j <= N-1, we ask the following question: "Given the values d[i] through d[j], is it possible that the values a[i] through a[j] form a geometric sequence?"
For example, suppose that d = {0,1,2,3,2}.
For i=0 and j=3 the answer is positive: it is possible that the values a[0] through a[3] are {1,2,4,8} which is a geometric sequence.
For i=1 and j=4 the answer is negative: there is no geometric sequence with these numbers of trailing zeros in binary.
Compute and return the number of contiguous subsequences of a[0], a[1], ..., a[N-1] that can be geometric sequences.
DEFINITION
Class:PotentialGeometricSequence
Method:numberOfSubsequences
Parameters:vector <int>
Returns:int
Method signature:int numberOfSubsequences(vector <int> d)
NOTES
-A geometric sequence is any sequence g[0], g[1], ..., g[k-1] such that there is a real number q (the quotient) with the property that for each valid i, g[i+1] = g[i]*q. For example, {1,2,4,8} is a geometric sequence with q=2, {7,7,7} is a geometric sequence with q=1, and {18,6,2} is a geometric sequence with q=1/3.
CONSTRAINTS
-N will be between 1 and 50, inclusive.
-d will contain exactly N elements.
-Each element of d will be between 0 and 100, inclusive.
EXAMPLES
0)
{0,1,2}
Returns: 6
One possibility is that a[0]=3, a[1]=6, and a[2]=12. In this case, all contiguous subsequences of this sequence are geometric.
1)
{1,2,4}
Returns: 5
All one-element and two-element subsequences are geometric. The entire sequence cannot be geometric.
2)
{3,2,1,0}
Returns: 10
3)
{1,2,4,8,16}
Returns: 9
4)
{1,3,5,5,5,5,64,4,23,2,3,4,5,4,3}
Returns: 37