Skip to content

Commit 0dd6bf9

Browse files
committed
srm 632 div 2 500
1 parent 8c5c92c commit 0dd6bf9

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

PotentialGeometricSequence.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
using namespace std;
11+
typedef pair<int,int> pi;
12+
typedef vector<int> vi;
13+
typedef vector<vi> vvi;
14+
typedef vector<string> vs;
15+
typedef vector<vs> vvs;
16+
17+
class PotentialGeometricSequence {
18+
public:
19+
int numberOfSubsequences(vector <int> a)
20+
{
21+
int n = a.size();
22+
int s = 2*n-1;
23+
for(int l = 3; l <= n; l++){
24+
for(int i = 0; i+l-1 < n; i++){
25+
int j = i+l-1;
26+
int d = a[i+1] - a[i];
27+
int ok = 1;
28+
for(int t = i; t < j; t++)
29+
if(a[t+1]-a[t] != d) ok = 0;
30+
if(ok) s++;
31+
}
32+
}
33+
return s;
34+
}
35+
36+
// BEGIN CUT HERE
37+
public:
38+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
39+
private:
40+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
41+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
42+
void test_case_0() { int Arr0[] = {0,1,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(0, Arg1, numberOfSubsequences(Arg0)); }
43+
void test_case_1() { int Arr0[] = {1,2,4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 5; verify_case(1, Arg1, numberOfSubsequences(Arg0)); }
44+
void test_case_2() { int Arr0[] = {3,2,1,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; verify_case(2, Arg1, numberOfSubsequences(Arg0)); }
45+
void test_case_3() { int Arr0[] = {1,2,4,8,16}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 9; verify_case(3, Arg1, numberOfSubsequences(Arg0)); }
46+
void test_case_4() { int Arr0[] = {1,3,5,5,5,5,64,4,23,2,3,4,5,4,3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 37; verify_case(4, Arg1, numberOfSubsequences(Arg0)); }
47+
48+
// END CUT HERE
49+
50+
};
51+
52+
// BEGIN CUT HERE
53+
int main(){
54+
55+
PotentialGeometricSequence ___test;
56+
___test.run_test(-1);
57+
58+
}
59+
// END CUT HERE

PotentialGeometricSequence.txt

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
PROBLEM STATEMENT
2+
3+
We have a sequence of N positive integers: a[0] through a[N-1].
4+
You do not know these integers.
5+
All you know is the number of trailing zeros in their binary representations.
6+
You are given a vector <int> d with N elements.
7+
For each i, d[i] is the number of trailing zeros in the binary representation of a[i].
8+
9+
10+
11+
For example, suppose that a[0]=40.
12+
In binary, 40 is 101000 which ends in three zeros.
13+
Therefore, d[0] will be 3.
14+
15+
16+
17+
You like geometric sequences.
18+
(See the Notes section for a definition of a geometric sequence.)
19+
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).
20+
21+
22+
23+
More precisely:
24+
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?"
25+
26+
27+
28+
For example, suppose that d = {0,1,2,3,2}.
29+
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.
30+
For i=1 and j=4 the answer is negative: there is no geometric sequence with these numbers of trailing zeros in binary.
31+
32+
33+
34+
Compute and return the number of contiguous subsequences of a[0], a[1], ..., a[N-1] that can be geometric sequences.
35+
36+
37+
DEFINITION
38+
Class:PotentialGeometricSequence
39+
Method:numberOfSubsequences
40+
Parameters:vector <int>
41+
Returns:int
42+
Method signature:int numberOfSubsequences(vector <int> d)
43+
44+
45+
NOTES
46+
-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.
47+
48+
49+
CONSTRAINTS
50+
-N will be between 1 and 50, inclusive.
51+
-d will contain exactly N elements.
52+
-Each element of d will be between 0 and 100, inclusive.
53+
54+
55+
EXAMPLES
56+
57+
0)
58+
{0,1,2}
59+
60+
Returns: 6
61+
62+
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.
63+
64+
1)
65+
{1,2,4}
66+
67+
Returns: 5
68+
69+
All one-element and two-element subsequences are geometric. The entire sequence cannot be geometric.
70+
71+
2)
72+
{3,2,1,0}
73+
74+
Returns: 10
75+
76+
77+
78+
3)
79+
{1,2,4,8,16}
80+
81+
Returns: 9
82+
83+
84+
85+
4)
86+
{1,3,5,5,5,5,64,4,23,2,3,4,5,4,3}
87+
88+
Returns: 37
89+
90+

0 commit comments

Comments
 (0)