[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 ...
随机推荐
- ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 以及 同步的集合类 Hashtable 和 Vector Collections.synchronizedMap 和 Collections.synchronizedList 区别缺点
ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 DougLea的 util.concurrent 包除了包含许多其他有用的并发构造块之外,还包含 ...
- CVE-2020-1938:Apache-Tomcat-Ajp漏洞-复现
0x00 漏洞简介 Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的Web服务器运行. Apache Tomcat服务器存在文件包含 ...
- OSI 七层模型以及TCP/IP模型
OSI 七层模型 定义 OSI(Open System Interconnection)即开放式系统互联通信参考模型.该模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系,一 ...
- Python的炫技操作:条件语句的七种写法
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Python极客社区 PS:如有需要Python学习资料的小伙伴可以 ...
- Jar包一键重启的Shell脚本及新服务器部署的一些经验
原文首发于博客园,作者:后青春期的Keats:地址:https://www.cnblogs.com/keatsCoder/ 转载请注明,谢谢! 前言 最近公司为客户重新部署了一套新环境,由我来完成了基 ...
- 深度学习之文本分类模型-前馈神经网络(Feed-Forward Neural Networks)
目录 DAN(Deep Average Network) Fasttext fasttext文本分类 fasttext的n-gram模型 Doc2vec DAN(Deep Average Networ ...
- 13.create-react-app 构建的项目使用代理 proxy
1. 正常运行 npm run eject 2. create-react-app 的版本在低于 2.0 的时候可以在 package.json 增加 proxy 配置, 配置成如下: "p ...
- python 获取的json字符串取值
获取到的json字符串,然后对其取值 {u'result': {u'10.10.10.100': {u'status': u'OK', u'msg': u"{'listen': {'': s ...
- EasyPoi 导入导出Excel时使用GroupName的踩坑解决过程
一.开发功能介绍: 简单的一个excel导入功能 二.Excel导入模板(大致模板没写全): 姓名 性别 生日 客户分类 联系人姓名 联系人部门 备注 材料 综合 采购 张三 男 1994/05/25 ...
- C#线程学习笔记
本笔记摘抄自:https://www.cnblogs.com/zhili/archive/2012/07/18/Thread.html,记录一下学习,方便后面资料查找 一.线程的介绍 进程(Proce ...