Ehab's REAL Number Theory Problem

前置知识

质数

分解质因数

无向无权图最小环<讲>


Ehab's REAL Number Theory Problem/onCF

给 \(n\) 个数 \(a_i\)(\(a_i\) 的因数个数不超过 \(7\)),求最少选出多少个数,使得乘积为完全平方。无解输出 \(-1\)。

数据范围:\(1\le n\le 10^5\),\(1\le a_i\le 10^6\)。


没想到一场普通的 \(\texttt{CF}\) 比赛能出出这么毒瘤好的题!


很明显,把每个 \(a_i\) 的平方因子除尽后对答案没有影响,所以可以把每个 \(a_i\) 的平方因子除尽。

如果某个 \(a_i\) 的质因子除尽后为 \(1\),直接选它便解决了问题。

然后剩下的质因子的幂次肯定为 \(1\)

而且最多只有两个质因子,因为如果 \(a_i\) 有三个质因子,按照约数个数定理,\(d(a_i)=(1+1)^3=8>7\),矛盾

最后问题简化为,选最少的数,使乘积包含的质因子幂次都为 2(可以自己想为什么不需要选幂次为 \(4\))。

过程:

\[18=2\times 3^2\to 2
\]

有一个极其巧妙的方法是建一个图,节点是质数,然后把每个数转换为它的两个质因子之间的一条边,求最小环

如果某个数 \(a_i\) 质因数个数为 \(1\)(不存在为 \(0\) 的,因为已经满足方案),把 \(1\) 也看做质数节点,连 \(1\) 和 \(a_i\)。

根据环的性质,每个点的度为 \(2\),所以边对应的数的乘积每个质因子幂次都为 \(2\)

边没有长度,是无向边,所以问题又简化为了求无向无权图最小环

过程:

\(a_i\):2 3 6 15

\[2\to 1\times 2,3\to 1\times 3,6\to 2\times 3,15\to 3\times 5
\]

最小环为 \((1,2,3)\)。


无向无权图最小环使不得 \(\texttt{Floyd}\)!这里的点数最大约是 \(78500\),\(\Theta(n^3)\) 能跑到射手座去了。

可以枚举起点,然后 \(\texttt{Bfs}\),因为问题特殊,所以可以有很大优化。

因为 \(1\le a_i\le 10^6\),所以每个 \(a_i\) 对应的边不可能连接两个 \(>1000\) 的质数。

所以如果有环,那么环必然有一个起点对应的质数 \(\in[1,1000]\)。

所以可以枚举这个起点 \(s\),然后 \(\texttt{Bfs}\)。

设 \(dep_x\) 表示节点 \(x\) 的深度,所以 \(dep_s=0\)。每次 \(Bfs\) 前清空。

然后沿着队列顶的点 \(x\) 连的边走如果走到一个 \(dep\) 未赋值的节点 \(to\),就令 \(dep_{to}=dep_x+1\)。

如果走到一个已经遍历过的点,那么说明这里有一个环,令 \(ans=\min\{ans,dep_{to}+dep_x+1\}\)。

\(\texttt{Bfs}\) 过程中可以走重复的点,不能走重复的边。

这里有一个问题:如何知道这个环是否以 \(s\) 为其中一个起点呢?

答案是不需要知道,无论 \(s\) 在不在环上都直接 \(ans=\min\{ans,dep_{to}+dep_x+1\}\)。

因为如果 \(s\) 不在环上,\(dep_{to}+dep_x+1\) 肯定比 \(s\) 在环上大(别忘了每个 \(s\) 都要枚举过去的啊!)。

时间复杂度 \(\Theta(n \sqrt n)\)。

过程:

只展示 \(s=1\) 的 \(\texttt{Bfs}\) 过程:


代码实现的时候,可以把质数离散化一下。

\(\texttt{code}\)

#include <bits/stdc++.h>
using namespace std; //&Start
#define inf 0x3f3f3f3f
#define re register
#define il inline
typedef long long lng;
typedef vector<int> veci; //&Data
#define N 100000
#define MX 1000000
#define P 78500--->1000000内质数数量
int n,a[N+10]; //&Prime--->筛质数
bitset<MX+10> np;
int p[P+10],ip[MX+10],pcnt,S;
il void Prime(){
np[1]=true,ip[1]=p[++pcnt]=S=1;
for(re int i=2;i<=MX;i++){
if(!np[i]) p[++pcnt]=i,ip[i]=pcnt,S+=(i<=999);
for(re int j=1;j<=pcnt&&i*p[j]<=MX;j++)
np[i*p[j]]=1;
}
} //&Graph
veci e[P+10];
int E=1,to[(N<<1)+10];//---->同网络流思想,使互为反边的两条边通过^1可得
il void add(re int x,re int y){ //加双向边
e[x].push_back(++E),to[E]=y;
e[y].push_back(++E),to[E]=x;
}
il void Add(re int x){ // 把数转换为边
re int dcnt=0,div[4];
for(re int j=2;j<=pcnt&&p[j]*p[j]<=x;j++)
if(x%p[j]==0){
while(x%(p[j]*p[j])==0) x/=(p[j]*p[j]);
if(x%p[j]==0) div[++dcnt]=j,x/=p[j];
}
if(x>1) div[++dcnt]=ip[x],x=1;
if(dcnt==0) puts("1"),exit(0);
else if(dcnt==1) add(1,div[1]);
else add(div[1],div[2]);
}
int sz=inf,q[P+10][2],dep[P+10];
il void Bfs(re int s){ //以s为起点Bfs
fill(dep+1,dep+P+1,inf);
re int qcnt=0;
q[++qcnt][1]=s,dep[s]=0;
for(re int ft=1;ft<=qcnt;ft++){
re int x=q[ft][1],f=q[ft][0];
for(re int i:e[x])if(i!=(f^1)){
if(dep[to[i]]==inf){
dep[to[i]]=dep[x]+1;
q[++qcnt][1]=to[i];
q[qcnt][0]=i;
} else sz=min(sz,dep[x]+dep[to[i]]+1); //找到环
}
}
} //&Main
int main(){
Prime();
scanf("%d",&n);
for(re int i=1;i<=n;i++)
scanf("%d",a+i),Add(a[i]);
for(re int i=1;i<=S;i++) Bfs(i); //枚举起点
if(sz==inf) puts("-1");
else printf("%d\n",sz);
return 0;
}

祝大家学习愉快!

题解-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. 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 ...

  3. 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 ...

  4. A. Number Theory Problem

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

  5. 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 题意: 给出一个 ...

  6. 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 ...

  7. 【BZOJ4026】dC Loves Number Theory 分解质因数+主席树

    [BZOJ4026]dC Loves Number Theory Description  dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源.    给 ...

  8. 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 ...

  9. Codeforces 1088E Ehab and a component choosing problem

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

随机推荐

  1. day92:flask:flask简介&基本运行&路由&HTTP请求和响应

    目录 1.Flask简介 2.关于使用flask之前的准备 3.flask的基本运行 4.flask加载配置 5.传递路由参数(没有限定类型) 6.传递路由参数(通过路由转换器限定路由参数的类型) 7 ...

  2. 如何统计Ceph的RBD真实使用容量

    前言 ceph的rbd一直有个问题就是无法清楚的知道这个分配的空间里面到底使用了多少,这个在Jewel里面提供了一个新的接口去查询,对于老版本来说可能同样有这个需求,本篇将详细介绍如何解决这个问题 查 ...

  3. Ceph Bluestore首测

    Bluestore 作为 Ceph Jewel 版本推出的一个重大的更新,提供了一种之前没有的存储形式,一直以来ceph的存储方式一直是以filestore的方式存储的,也就是对象是以文件方式存储在o ...

  4. Android Support v4\v7\v13和AndroidX理解【转载】

    为什么要用support库呢? 因为在低版本Android平台上开发一个APP时,想使用高版本才有的功能,此时就需要使用Support来支持兼容. 1. android-support-v4 comp ...

  5. shell脚本快速入门----正则表达式

    一. "." 符号 (一个英文句号) 用于匹配换行符之外的任意一个字符 如 root 可用r..t来匹配 二. "*"符号 重复匹配前一个字符 如ab abc ...

  6. tp5 统一返回json格式

    控制器调用 public function json(){ if (request()->isPost()) { return jsonData(1,'转换成功',数据(可不填)); } } 公 ...

  7. 算法基础——KMP字符串匹配

    原题链接 题目: 给定一个模式串S,以及一个模板串P,所有字符串中只包含大小写英文字母以及阿拉伯数字. 模板串P在模式串S中多次作为子串出现. 求出模板串P在模式串S中所有出现的位置的起始下标. 输入 ...

  8. python3 Redis未授权检测脚本

    `import sys import getopt import socket def get_target(): opts, args = getopt.getopt(sys.argv[1:], ' ...

  9. Matlab 画图1

    plot函数 plot最简单的是plot(x,y),其中,x,y是一组数据 如果要画出\(y=x^2\)的图像 在Command Window中输入 x =[1 2 3]; y =[4 5 6]; p ...

  10. redis 压测与乐观锁

    单线程没有出现并发问题. 链接太多爆炸了 把连接改到50,没有问题 改回1000: emmm159,看来相当一部分拒绝了 并且8180-10000到头了 cpu爆炸了 观察下这种程度的并发用乐观锁 一 ...