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?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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?
Название |
---|
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.