Three Chairs
Contest: Codeforces Round 846 (Div. 2)
Difficulty rating: 2300
Tags: bitmasks, brute force, combinatorics, data structures, dp, number theory, sortings

One day Kira found $$$n$$$ friends from Morioh and decided to gather them around a table to have a peaceful conversation. The height of friend $$$i$$$ is equal to $$$a_i$$$. It so happened that the height of each of the friends is unique.

Unfortunately, there were only $$$3$$$ chairs in Kira's house, and obviously, it will not be possible to seat all friends! So, Kira has to invite only $$$3$$$ of his friends.

But everything is not so simple! If the heights of the lowest and the tallest of the invited friends are not coprime, then the friends will play tricks on each other, which will greatly anger Kira.

Kira became interested, how many ways are there to choose $$$3$$$ of his friends so that they don't play tricks? Two ways are considered different if there is a friend invited in one way, but not in the other.

Formally, if Kira invites friends $$$i$$$, $$$j$$$, and $$$k$$$, then the following should be true: $$$\gcd(\min(a_i, a_j, a_k), \max(a_i, a_j, a_k)) = 1$$$, where $$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of the numbers $$$x$$$ and $$$y$$$.

Kira is not very strong in computer science, so he asks you to count the number of ways to invide friends.

Input format:
The first line contains the number $$$n$$$ ($$$3 \le n \le 3\cdot10^5$$$) — the number of Kira's friends.

The next line contains $$$n$$$ distinct integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 3\cdot10^5$$$) — heights of Kira's friends.

Output format:
In a single line output the number of ways to invite three friends.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, only one way is suitable: invite friends $$$1$$$, $$$2$$$, and $$$3$$$. Here $$$1 < 2 < 3$$$, and the numbers $$$1$$$ and $$$3$$$ are coprime.

Example input:
3
1 2 3

Example output:
1

Example input:
4
1 6 2 3

Example output:
3

Editorial:
Let's sort the array and process the triples $$$i, j, k$$$, assuming that $$$i < j < k$$$ and $$$a_i < a_j < a_k$$$. Now if $$$gcd(a_i, a_k) = 1$$$, then the number of ways to take the index $$$j$$$ is $$$k - i - 1$$$.
We will consider the answer to the problem for each $$$k$$$ from $$$1$$$ to $$$n$$$, assuming that $$$a_k$$$ is the maximum number in the triple. Now let $$$c$$$ be the number of numbers that are mutually prime with $$$a_k$$$ on the prefix from $$$1$$$ to $$$k - 1$$$, and $$$sum$$$ is the sum of their indices. Then you need to add $$$c\cdot i - sum - c$$$ to the answer.
It remains to find out the number of numbers that are mutually prime with $$$a_k$$$ and the sum of their indices. This can be done using the inclusion and exclusion method. Let $$$cnt_i$$$ be the number of numbers $$$a_j$$$ that are divisible by $$$i$$$, $$$s_i$$$ be the sum of the indices $$$j$$$ of numbers $$$a_j$$$ that are divisible by $$$i$$$. Let's look at the prime numbers $$$p_1, p_2, ..., p_m$$$ included in the factorization of the number $$$a_k$$$.
Then let $$$c$$$ initially be equal to the number of numbers on the prefix, and $$$sum$$$ to the sum of the indices on the prefix. Note that then we took into account the extra elements - numbers that are divisible by $$$p_1, p_2, ..., p_m$$$, since they will not be mutually simple with $$$a_k$$$, we subtract them from $$$c$$$ and $$$sum$$$. But having done this, we again took into account the extra elements that are multiples of the numbers of the form $$$p_i * p_j$$$, where $$$i \neq j$$$, add them back, etc. So we can iterate over the mask $$$mask$$$ of the primes $$$p_1, p_2, ..., p_m$$$. And depending on the parity of the bits in the mask, we will subtract or add elements that are multiples of $$$d$$$, where $$$d$$$ is the product of the primes included in $$$mask$$$. Having received $$$c$$$ and $$$sum$$$, we can recalculate the answer for the position $$$i$$$.
To move from position $$$i$$$ to position $$$i+1$$$, update the values of $$$cnt$$$ and $$$s$$$ by adding the element $$$a_{i-1}$$$ by iterating over the mask of the simple element $$$a_{i-1}$$$.