Ehab's REAL Number Theory Problem

题目描述

You are given an array $ a $ of length $ n $ that has a special condition: every element in this array has at most 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 $ if $ a $ can be obtained from $ b $ by deletion of several (possibly, zero or all) elements.

输入格式

The first line contains an integer $ n $ ( $ 1 \le n \le 10^5 $ ) — the length of $ a $ .

The second line contains $ n $ integers $ a_1 $ , $ a_2 $ , $ \ldots $ , $ a_{n} $ ( $ 1 \le a_i \le 10^6 $ ) — the elements of the array $ a $ .

输出格式

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

3
1 4 6

样例输出 #1

1

样例 #2

样例输入 #2

4
2 3 6 6

样例输出 #2

2

样例 #3

样例输入 #3

3
6 15 10

样例输出 #3

3

样例 #4

样例输入 #4

4
2 3 5 7

样例输出 #4

-1

提示

In the first sample, you can choose a subsequence $ [1] $ .

In the second sample, you can choose a subsequence $ [6, 6] $ .

In the third sample, you can choose a subsequence $ [6, 15, 10] $ .

In the fourth sample, there is no such subsequence.

至多7个因子是什么条件?仔细分析,其实是说明这个数至多有两个不同质因数。

要求一个序列的所有数相乘为完全平方。也就代表任意一个质因数的次数之和为偶数。那么在某一个数中,如果有一个质因数出现了偶数次,那他很没用,出现了和没出现一个样,我们就可以不理他。然后现在要求选出来的序列每种质因数出现偶数次,会想到一件事。如果有一个数的质因数是 \((u,v)\),那么就一定需要一个含有 \(u\) 的数和一个含有 \(v\) 的数,然后往后又要推。最后一定是一个环。所以考虑最小环构图。

构图很简单,如果存在一个数 \(a_i=u\times v\)( \(u,v\) 为质数),那么就可以从 \(u\) 向 \(v\) 连一条无向边。如果一个数次数为奇数的质因数只有一个,那就把质因数 \(p\) 和 \(1\) 连边。

现在到了跑最小环。最小环问题解法可以使用 Floyd,但是这里完全行不通。注意到这里的边是无权的,对于最小环,有一种 \(O(n^2)\) 的算法。钦定一个一定在最小环上的点 \(x\),用它跑出一棵 bfs 树。然后对于一条不在树上的边 \((u,v)\),设点 \(x\) 到点 \(y\) 的最短路为 \(d_y\),那么如果点 \(x\) 和点 \((u,v)\) 要同时在最小环上,最小环长为 \(d_u+d_v+1\)。

或许有人会问,如果点 \((u,v)\) 在 bfs 树上的 lca 不为点 \(x\),怎么办?其实没关系,因为等我枚举 \(x\) 枚举到他们的 lca 时,自然就会把那个环算入了。

\(O(n^2)\) 的复杂度仍然不能接受,但发现不存在两个数 \(x,y\ge 1000\),同时又有连边。这说明,最小环上一定有一个点 \(x\le1000\)。所以我们枚举在环上的点时只用枚举小于等于1000的质数就行了。

复杂度 \(O(\frac{n\sqrt{n}}{logn})\)

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=1e6+5;
int n,a[N],pri[M],hd[M],dp[M],q[M],l,r,ans=2000000000,e_num(1),pw[M];
vector<int>g[M];
struct edge{
int v,nxt,f;
}e[N<<1];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
void run(int x)
{
memset(dp,0,sizeof(dp));
dp[q[l=r=1]=x]=1;
for(int i=2;i<=e_num;i++)
e[i].f=0;
while(l<=r)
{
int k=q[l];
++l;
for(int i=hd[k];i;i=e[i].nxt)
{
if(!dp[e[i].v])
{
e[i^1].f=1;
dp[e[i].v]=dp[k]+1;
q[++r]=e[i].v;
}
else if(!e[i].f)
{
// printf("%d %d %d\n",x,k,e[i].v);
ans=min(ans,dp[k]+dp[e[i].v]-1);
}
}
}
}
int main()
{
for(int i=2;i<M;i++)
{
// printf("%d\n",i);
if(!pri[i])
{
for(int j=1;j*i<M;j++)
{
pri[j*i]=1;
if(j%i==0)
pw[j]=pw[j/i]^1;
if(!pw[j])
g[j*i].push_back(i);
}
for(int j=1;j*i<M;j++)
pw[j]=0;
}
}
// puts("hjhakioi");
// for(int i=1;i<M;i++)
// {
// printf("%d\n",i);
// for(int j=0;j<g[i].size();j++)
// printf("%d ",g[i][j]);
// putchar('\n');
// }
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
if(g[a[i]].size()==1)
{
add_edge(1,g[a[i]][0]);
add_edge(g[a[i]][0],1);
}
else if(g[a[i]].size())
{
add_edge(g[a[i]][0],g[a[i]][1]);
add_edge(g[a[i]][1],g[a[i]][0]);
}
else
{
printf("1");
return 0;
}
}
for(int i=1;i<=1000;i++)
run(i);
if(ans>n)
printf("-1");
else
printf("%d",ans);
}

[CF1325E] Ehab's REAL Number Theory Problem的更多相关文章

  1. [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环

    E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...

  2. 题解-Ehab's REAL Number Theory Problem

    Ehab's REAL Number Theory Problem 前置知识 质数 分解质因数 无向无权图最小环<讲> Ehab's REAL Number Theory Problem/ ...

  3. Number Theory Problem(The 2016 ACM-ICPC Asia China-Final Contest 找规律)

    题目: Mr. Panda is one of the top specialists on number theory all over the world. Now Mr. Panda is in ...

  4. Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  5. A. Number Theory Problem

    题目大意:计算小于2^n,且满足2^k-1并且是7的倍数的个数 思路:优先打表,数据不大,1e5,然后求个前n项和 #include<bits/stdc++.h> using namesp ...

  6. How to solve the SVDI SN Number Display Problem

    Yesterday we have learn how to find the SVDI Serial Number, today one of customer from UK look our a ...

  7. Codeforces 1088E Ehab and a component choosing problem

    Ehab and a component choosing problem 如果有多个连接件那么这几个连接件一定是一样大的, 所以我们先找到值最大的连通块这个肯定是分数的答案. dp[ i ]表示对于 ...

  8. 2016级算法第二次上机-F.ModricWang's Number Theory II

    891 ModricWang's Number Theory II 思路 使得序列的最大公约数不为1,就是大于等于2,就是找到一个大于等于2的数,它能够整除序列中的所有数. 考虑使得一个数d整除数组中 ...

  9. Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem

    E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...

  10. Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem

    D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...

随机推荐

  1. MySql Workbench 迁移工具 migration 提示缺少pyodbc 2.1.8 的解决方法

    想把公司的数据库转到MySQL,所以想装个MySQL测试,发现新版的MySQL(8.0.34)默认安装还是有不少问题, 一.譬如表.字段大小写的问题: lower_case_table_names=0 ...

  2. 轻松玩转70亿参数大模型!借助Walrus在AWS上部署Llama2

    Llama 2 是 Meta 的下一代开源大语言模型.它是一系列经过预训练和微调的模型,参数范围从 70 亿到 700 亿个.Meta Llama 2 可免费用于研究和商业用途并且提供了一系列具有不同 ...

  3. 【LaTeX】环境配置以及中文支持

    目录 网页环境 Overleaf 本地环境 TeX Live TeXstudio VSCode 安装 LaTeX Workshop 扩展 编译链配置 正向同步 反向同步 其他可选配置 中文支持 XeL ...

  4. 14.4K Star,一款外观漂亮、运行快速、动画细腻的开源免费UI组件库

    之前给大家推荐了很多后台模版,有读者希望推荐一些跟通用的好看组件,毕竟出了后台还有很多其他场景嘛.所以,今天继续给大家推荐一个广受好评的UI组件库:NextUI 主要特性 NextUI的主要目标是简化 ...

  5. P1113 杂务 (DAG拓扑排序--DP)

    这是一道拓扑排序的模板题 0 额. 所需的前置知识: 图论相关的基本概念 建图,存图 图的遍历 非常入门的DP 下面进入正文 1 引入 拓扑排序是一类用于处理 DAG(Directed acyclic ...

  6. 行行AI人才直播第17期:无界AI联合创始人马千里《AIGC,比生产力还多》

    谁也没有想到,短短几个月内,AI就在各行各业掀起了一阵狂风暴雨.ChatGPT.Stable Diffusion.Midjourney--AI已经变得十八般武艺样样精通,不仅能够高效率完成很多日常工作 ...

  7. 使用Skonsole自动生成Git提交信息

    使用Skonsole自动生成Git提交信息 随着LLM应用的普及,日常工作中的很多使用都可以使用LLM来完成,比如Git提交信息的生成. Skonsole是一个基于Semantic Kernel的命令 ...

  8. pythonapi接口怎么对接?

    ​ Python API接口对接是使用Python语言开发应用程序时,与外部API接口进行交互的一种方式.API(应用程序接口)是一种定义了程序或系统如何与另一个程序或系统进行交互的协议.通过使用Py ...

  9. 从驾考科目二到自动驾驶,聊聊GPU为什么对自动驾驶很重要

    "下一个项目,坡道起步." -- "考试不合格,请将车子开到起点,重新验证考试.你的扣分项是:起步时间超30秒:扣100分.行驶过程中车轮轧到边线:扣100分." ...

  10. JDK8升级JDK11最全实践干货来了

    1.前言 截至目前(2023年),Java8发布至今已有9年,2018年9月25日,Oracle发布了Java11,这是Java8之后的首个LTS版本.那么从JDK8到JDK11,到底带来了哪些特性呢 ...