What can be the approach to solve this problem? I am not able to understand that when any array can have its lcm equal to its product?
# | User | Rating |
---|---|---|
1 | tourist | 3856 |
2 | jiangly | 3747 |
3 | orzdevinwang | 3706 |
4 | jqdai0815 | 3682 |
5 | ksun48 | 3591 |
6 | gamegame | 3477 |
7 | Benq | 3468 |
8 | Radewoosh | 3462 |
9 | ecnerwala | 3451 |
10 | heuristica | 3431 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | -is-this-fft- | 162 |
3 | Dominater069 | 160 |
4 | Um_nik | 158 |
5 | atcoder_official | 157 |
6 | Qingyu | 155 |
7 | djm03178 | 151 |
7 | adamant | 151 |
9 | luogu_official | 150 |
10 | awoo | 147 |
What can be the approach to solve this problem? I am not able to understand that when any array can have its lcm equal to its product?
Name |
---|
it just means that in the subarray you choose , gcd of each two element should be 1 .
and about the approach , you can use two pointers + sieve to solve it :)
Can you prove why gcd of every pair should be 1?
Basic formula: lcm(a, b) = (a * b) / gcd(a, b)
If gcd(a, b) = 1, lcm(a, b) = a * b
Hope it's clear now :D
What if n>2?
I saw this problem a long time ago and gave it a shot today. Check my submission as I think there was a lot of unnecessary stuff in the Editorial.
Solution
Let's X = p[1]^a[1] * ... * p[m]^a[m], Y = p[1]^b[1] * ... * p[m]^b[m]:
For array X[1], ..., X[n] we have:
And X[1] * ... * X[n] = p[1]^(a[1][1] + ... + a[1][n]) * ... * p[m]^(a[m][1] + ... + a[m][n]).
We are looking for sequence, where LCM(x[1], ..., x[n]) = X[1] * ... * X[n].
So for every i in [1..m] max(a[i][1], ..., a[i][n]) == (a[i][1] + ... + a[i][n])
It is true only when there is exactly one a[i][j] >= 0 and for every k != j a[i][k] == 0 (all a[i][] are non-negative).
You can see that for any k1 != k2 GCD(X[k1], X[k2]) == 1, because min(a[i][k1], a[i][k2]) == 1 for every i in [1..m].
In fact you don't need calculate gcd of any array elements. GCD(x, y) > 1, if they have at least one common prime divisor.
So you can do 'two pointers' method (as IHaveInt mentioned earlier) and found for each i in [1..n] max length of subarray [j..i] that there is no two elements which are divided by the same prime number in that subarray.