1325E - Ehab’s REAL Number Theory Problem[最小环]
time limit per test | memory limit per test | input | output |
---|---|---|---|
3 seconds | 256 megabytes | standard input | standard output |
Description:
You are given an array a a a of length n n n that has a special condition: every element in this array has at most 7 7 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.
A sequence a is a subsequence of an array b b b if a can be obtained from b b b by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer n ( 1 ≤ n ≤ 1 0 5 ) n (1≤n≤10^5) n(1≤n≤105) — the length of a a a.
The second line contains n n n integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 1 0 6 ) a_1, a_2, …, a_n (1≤a_i≤10^6) a1,a2,…,an(1≤ai≤106) — the elements of the array a a a.
Output
Output the length of the shortest non-empty subsequence of a product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there’s no such subsequence, print “ − 1 -1 −1”.
One Example input
3
1 4 6
One Example output
1
Two Example input
4
2 3 6 6
Two Example output
2
Three Example input
3
6 15 10
Three Example output
3
Four Example input
4
2 3 5 7
Four Example output
-1
分析:
题意:
n
n
n个数,问若要得到
k
k
k个数的乘积是完全平方数,至少需要取几个数
做法:
有两种情况比较好理解
第一种:有一个数是
1
1
1,那么肯定最少是只取
1
1
1
第二种:有重复的数出现,那么最少肯定是
2
2
2
那么第三种情况比较要思考
根据题意可知每个数的因数不超过
7
7
7,也就是说他不同的质因数应该不超过
2
2
2个
原因是
2
2
=
4
,
2
3
=
8
2^2 = 4, 2^3 = 8
22=4,23=8,也就是说如果不同质因数超过两个,那么至少有
8
8
8个因数
又因为要找的是完全平方数,那么将每个次因数的(次幂
m
o
d
2
\mod 2
mod2) 不会影响结果
也就是说最后记录的是每个数质因数次幂为奇数的数
然后可以将同一个不同的质因数连一条无向边,最后可以形成一个图
可以发现如果可以围成一个最小的环,那么环上的那些质因数就是我们要取的数的质因数
例如第二个样例:
6
,
10
,
15
6, 10, 15
6,10,15
6
6
6剩下的因子是
2
,
3
2,3
2,3
10
10
10剩下的因子是
2
,
5
2,5
2,5
15
15
15剩下的因子是
3
,
5
3,5
3,5
同一个数的因子连一条直线,如下图
可以发现最少需要三个数
至于最小环
b
f
s
bfs
bfs的时候,其实应该是
l
e
n
=
d
e
p
[
u
]
+
d
e
p
[
v
]
+
1
−
L
C
A
(
u
,
v
)
len = dep[u] + dep[v] + 1 - LCA(u,v)
len=dep[u]+dep[v]+1−LCA(u,v)
但是这个数据量比较小,然后遍历了每个点
就是说
L
C
A
(
v
,
u
)
LCA(v,u)
LCA(v,u)都会遍历到
0
0
0的情况,求得最小环
这个是看一个博客看到的 传送门,一开始也一直很迷惑
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
const int inf = 0x3f3f3f3f;
vector<int> v1;
vector<int> e[maxn];
bool vis[maxn];
int dep[maxn];
int ans;
void solve(int x) {
int tmp = 1;
for(int i = 2; i * i <= x; ++i) {
int cnt = 0;
while(x % i == 0) x /= i, cnt += 1;
if(cnt & 1) tmp *= i;
}
if(x) tmp *= x;
v1.push_back(tmp);
}
void bfs(int x) {
queue<int> q;
while(!q.empty()) q.pop();
memset(dep, inf, sizeof(dep));
memset(vis, false, sizeof(vis));
q.push(x);
dep[x] = 0, vis[x] = true;
while(!q.empty()) {
int u = q.front(); q.pop();
vis[u] = false;
for(auto v : e[u]) {
if(dep[v] > dep[u] + 1) {
dep[v] = dep[u] + 1;
q.push(v); vis[v] = true;
}else if(vis[v]){ // +
ans = min(ans, dep[u] + dep[v] + 1);
}
}
}
}
void solve2() {
for(auto i : v1) {
int p = 1, q = 1;
for(int j = 2; j * j <= i; ++j) {
if(i % j) continue;
p = j;
if(i % (i / j) != 1) q = i / j;
break;
}
if(p == 1) p = i;
e[p].push_back(q), e[q].push_back(p);
}
ans = inf;
for(int i = 1; i < 1000; ++i) {
if(e[i].empty()) continue;
bfs(i);
}
if(ans == inf) puts("-1");
else printf("%d\n", ans);
}
int main() {
int n, x;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &x); solve(x);
}
sort(v1.begin(), v1.end());
int k = unique(v1.begin(), v1.end()) - v1.begin();
if(v1[0] == 1) puts("1");
else if(k < n) puts("2");
else solve2();
return 0;
}