<p>Given an integer array <code>nums</code>, return <em>the number of all the <strong>arithmetic subsequences</strong> of</em> <code>nums</code>.</p>

<p>A sequence of numbers is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p>

<ul>
	<li>For example, <code>[1, 3, 5, 7, 9]</code>, <code>[7, 7, 7, 7]</code>, and <code>[3, -1, -5, -9]</code> are arithmetic sequences.</li>
	<li>For example, <code>[1, 1, 2, 5, 7]</code> is not an arithmetic sequence.</li>
</ul>

<p>A <strong>subsequence</strong> of an array is a sequence that can be formed by removing some elements (possibly none) of the array.</p>

<ul>
	<li>For example, <code>[2,5,10]</code> is a subsequence of <code>[1,2,1,<strong><u>2</u></strong>,4,1,<u><strong>5</strong></u>,<u><strong>10</strong></u>]</code>.</li>
</ul>

<p>The test cases are generated so that the answer fits in <strong>32-bit</strong> integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,4,6,8,10]
<strong>Output:</strong> 7
<strong>Explanation:</strong> All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [7,7,7,7,7]
<strong>Output:</strong> 16
<strong>Explanation:</strong> Any subsequence of this array is arithmetic.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1&nbsp; &lt;= nums.length &lt;= 1000</code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        int n = nums.size();
        int res = 0;
        std::vector<int> cnts;
        cnts.reserve(nums.size());
        std::unordered_map<long, int> seen;
        for (int b = n-2; b >= 1; --b) {
            seen[nums[b + 1]]++;
            long nb = nums[b];
            for (int a = 0; a < b; ++a) {
                long na = nums[a];
                long d = nb - na;
                if (!seen.count(nb + d)) continue;
                if (d == 0) {
                    res += (1l << seen[nb]) - 1;
                    continue;
                }
                if (!seen.count(nb + d + d)) {
                    res += seen[nb + d];
                    continue;
                }
                cnts.clear();
                for (int c = b+1; c < n; ++c) {
                    long nc = nums[c];
                    long mod = (nc - na) % d;
                    long div = (nc - na) / d;
                    if (mod != 0) continue;
                    div -= 2;
                    if (div < 0) continue;
                    if (div > cnts.size()) continue;
                    if (div == cnts.size()) cnts.push_back(0);
                    if (div == 0)
                        cnts[0]++;
                    else
                        cnts[div] += cnts[div-1];
                }
                if (cnts.empty()) continue;
                for (int c : cnts) res += c;
            }
        }

        if (false) {
            std::unordered_map<int, int> cnts;
            for (int n : nums) {
                cnts[n]++;
            }
            for (auto& [k,v] : cnts) {
                if (v <= 2) continue;
                res += (1l<<v) - 1 - v - (long)v * (v-1) / 2;
            }
        }
        return res;
    }
};
13731