Skip to content

Commit 7349706

Browse files
authored
Create 3404.Count-Special-Subsequences.cpp
1 parent e2c08fc commit 7349706

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using LL = long long;
2+
class Solution {
3+
public:
4+
LL getKey(int x, int y)
5+
{
6+
int g = gcd(x,y);
7+
x = x/g;
8+
y = y/g;
9+
return (LL)x*1000+y;
10+
}
11+
long long numberOfSubsequences(vector<int>& nums)
12+
{
13+
unordered_map<LL, vector<int>>Map;
14+
int n = nums.size();
15+
for (int i=0; i<n; i++)
16+
for (int j=i+2; j<n; j++)
17+
Map[getKey(nums[i], nums[j])].push_back(j);
18+
19+
for (auto& [k,v]:Map)
20+
sort(v.begin(), v.end());
21+
22+
LL ret = 0;
23+
for (int r=2; r<n; r++)
24+
for (int s=r+2; s<n; s++)
25+
{
26+
int key = getKey(nums[s], nums[r]);
27+
auto iter = lower_bound(Map[key].begin(), Map[key].end(), r-1);
28+
ret += iter - Map[key].begin();
29+
}
30+
return ret;
31+
}
32+
};

0 commit comments

Comments
 (0)