[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<=10^6\)
- \(a_i\) 最多只有7个因数
题目要求在这个数列找到一个最短的子数列,子数列的所有的数相乘是一个完全平方数。
题解:
- 这个题对于 \(x^{3}\) 应该等价于 \(x\) ,其实就是可以除去 \(a_i\)中的所有的平方项,显而易见,这个并不影响答案。
- 因为 \(a_i\) 最多只有7个因素,由素数唯一分解定理可得每一个元素最多 2 个质因子。
- 所以删去平方项之后,每一个元素就会要么是两个素因子相乘,要么就是一个素因子。
- 此时,我们把每一个素因子当初一个节点,如果一个素数是由哪两个素因子相乘,那就可以连一条边,如果只有一个元素,把1也当作素因子看做一个节点。
- 这样就变成一个图论问题,求无向无权图的最小环。
- 直接暴力每一个点都是起点,因为每一个元素至少有一个素因子是小于1000,所以这个时候我们遍历从1到1000每一个值都是起点,就可以遍历到每一个元素,在这里求最小环即可。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e6+10;
struct node{
int v,nxt;
node(int v=0,int nxt=0):v(v),nxt(nxt){}
}e[maxn];
int head[maxn],cnt,isp[maxn],v[maxn],m,f,ans,a[maxn],num;
void init(){//只需要求1000以内的素数即可
cnt=f=num=0,ans=inf;
memset(head,-1,sizeof(head));
for(int i=2;i<1000;i++){
if(!v[i]){
isp[m++]=i;
v[i]=i;
}
for(int j=0;j<m;j++){
if(v[i]<isp[j]||i*isp[j]>=1000) break;
v[i*isp[j]]=isp[j];
}
}
}
void add(int u,int v){
a[++num]=u,a[++num]=v;//求出所有的素数
e[cnt]=node(v,head[u]);
head[u]=cnt++;
e[cnt]=node(u,head[v]);
head[v]=cnt++;
}
void judge(int x){
int div[5],tot=0;
for(int i=0;i<m;i++){
if(x%isp[i]==0){
while(x%(isp[i]*isp[i])==0) x/=isp[i]*isp[i];
if(x%isp[i]==0) div[++tot]=isp[i],x/=isp[i];
}
}
if(tot==0&&x==1) {f=1;return ;}
if(x>1) div[++tot]=x;
if(tot==1) add(1,div[1]);
else add(div[1],div[2]);
}
int d[maxn];
typedef pair<int,int>pii;
void bfs(int s){
for(int i=1;i<=num;i++) d[a[i]]=inf;//只需要初始化在图中的素数
d[s]=0;queue<pii>que;
que.push(pii(s,0));
while(!que.empty()){
pii u=que.front();que.pop();
for(int i=head[u.first];~i;i=e[i].nxt){
int v=e[i].v;
if(v==u.second) continue;
if(d[v]==inf){
d[v]=d[u.first]+1;
que.push(pii(v,u.first));
}
else ans=min(ans,d[u.first]+d[v]+1);
}
}
}
int main(){
init();int n;
scanf("%d",&n);
for(int i=1,x;i<=n;i++){
scanf("%d",&x);judge(x);
}
sort(a+1,a+1+num);
num=unique(a+1,a+1+num)-a-1;
if(f) {printf("1\n");return 0;}
bfs(1);
for(int i=0;i<m;i++) bfs(isp[i]);
if(ans==inf) printf("-1\n");
else printf("%d\n",ans);
return 0;
}
[E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环的更多相关文章
- 题解-Ehab's REAL Number Theory Problem
Ehab's REAL Number Theory Problem 前置知识 质数 分解质因数 无向无权图最小环<讲> Ehab's REAL Number Theory Problem/ ...
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- https://vjudge.net/contest/321565#problem/C 超时代码
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> ...
- http://codeforces.com/contest/610/problem/D
D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/612/problem/D
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- http://codeforces.com/contest/536/problem/B
B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/535/problem/C
C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- http://codeforces.com/contest/838/problem/A
A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- http://codeforces.com/contest/402/problem/E
E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- pgsql中的事务隔离
pgsql中的事务隔离级别 前言 事物隔离级别 在各个级别上被禁止出现的现象是 脏读 不可重复读 幻读 序列化异常 读已提交隔离级别 可重复读隔离级别 可序列化隔离级别 摘录 pgsql中的事务隔离级 ...
- transaction 用tx事务 测试时 报错:Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc]
Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/sc ...
- 使用ffprobe 查询wav文件信息
使用ffprobe 查询wav文件信息 安装 安装过程和ffmepg相同不在赘述 不带参数查询文件信息 ffprobe ZH_biaobei_标准合成_甜美女声_楠楠_5_5_5_6_1_4047db ...
- AJ学IOS(30)UI之Quartz2D画图片画文字
回头看了看自己写的博客,AJ决定以后更改风格 本意是想大家看效果直接拷贝代码能用,注释齐全也方便学习,但是发现这样对新手学习特别困难 以后风格基本是–>看标题–>看目录–>看图片–& ...
- Android Them+SharedPreferences 修改程序所有view字体颜色、大小和页面背景
有这么一个需求,可以对页面的样式进行选择,然后根据选择改变程序所有字体颜色和页面背景.同时下一次启动程序,当前设置依然有效. 根据需求,我们需要一种快速,方便,有效的方式来实现需求,然后可以通过And ...
- Redis学习三:Redis高可用之哨兵模式
申明 本文章首发自本人公众号:壹枝花算不算浪漫,如若转载请标明来源! 感兴趣的小伙伴可关注个人公众号:壹枝花算不算浪漫 22.jpg 前言 Redis 的 Sentinel 系统用于管理多个 Redi ...
- 深度解密 Go 语言之 sync.Pool
最近在工作中碰到了 GC 的问题:项目中大量重复地创建许多对象,造成 GC 的工作量巨大,CPU 频繁掉底.准备使用 sync.Pool 来缓存对象,减轻 GC 的消耗.为了用起来更顺畅,我特地研究了 ...
- python脚本如何同时运行多个
当我们想一次运行多个py脚本的时候你想到了什么应用场景了吗?当你想同时并行的处理一些对象时你有什么好方法吗?下面我就简单的总结一些这方面的小技巧,方便大家根据情况灵活处理. 1 用一个py脚本运行多个 ...
- Java去除ArrayList集合中重复字符串的案例
ArrayList去除集合中的字符串重复值 分析: A:创建集合对象 B:添加多个字符串元素 C:创建新集合 D:遍历旧集合,获取得到每一个元素 E:拿着个元素到新集合去找,看有没有 有:不进去 没有 ...
- 新建MapReduce项目
添加各种jar包 /usr/local/hadoop/share/hadoop/.. 这几个文件夹下的jar包以及它们子目录lib下的所有jar包 将/usr/local/hadoop/etc/had ...