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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.