CodeForces 1325E - Ehab's REAL Number Theory Problem【质因子+】

题意:

??给定一个数组 \(a\) ,数组中任意一个元素的因子数不超过 \(7\) ,找出一个最短的子序列,满足该子序列之积为完全平方数。输出其长度。
数据范围:\(1≤n≤10^5,1≤a_i≤10^6\)

分析:

??首先,对于数组中的每个元素,如果其因子中包含有一个完全平方数,那么可以把该完全平方数除去,不影响最后的结果。
??然后,可以发现,当一个数的因子个数 \(\leq 7\) 时,其包含的质因子个数 \(\leq 2\)。(如果有3个质因子,那么至少有 \(8\) 个因子)当我们把每个数因子中的完全平方数除去后,每个数回变成 \(1,p,pq\)(\(p,q\) 均为质数)中的一种。接下来,建一个图,图中每个顶点为一个质数和 \(1\),边为数组中元素化简后得到的数字。那么图的意义是什么呢?假设我们从点 \(p\) 经过一条边走到点 \(q\),那么我们就可以得到 \(p*q\)。要想最后得到一个完全平方数,那么必然会有一个点走两次,即整条路径是一个环。要使路径最短,就是要求一个最小环的长度。
??我们可以直接对每个点用一遍 \(bfs\) 来找到最小环,复杂度为:\(O(N*M)\),显然不行。但其实可以发现,对于两个都大于 \(\sqrt{a_{max}}\)的点,它们之间是不可能连边的,各自只能和更小的数的点连边,所以对于这些点,不用以它们为源点进行 \(bfs\)。

代码:

参考博客

CodeForces 1325E - Ehab's REAL Number Theory Problem【质因子+】

原文地址:https://www.cnblogs.com/1024-xzx/p/12507534.html

时间: 2024-07-29 05:34:35

CodeForces 1325E - Ehab's REAL Number Theory Problem【质因子+】的相关文章

A. Number Theory Problem

题目大意:计算小于2^n,且满足2^k-1并且是7的倍数的个数 思路:优先打表,数据不大,1e5,然后求个前n项和 #include<bits/stdc++.h> using namespace std; const int N=1E5+1; int arr[N]; void inint(int x){ int t=1; for(int i=1;i<=x;i++){ t<<=1; t%=7; if(t==1) arr[i]=1; } for(int i=1;i<=x;i

Codeforces Round #601 (Div. 2)E(寻找质因子,DP)

先分解质因数,对于当前a[i],假设当前的质因数为x,这个位置要满足能被k整除,有两个可能,要么是它向后一个转移x%k个,要么是后一个向它转移k-x%k个. 对于每一个a[i]满足后,因为只会对下一个位置产生影响,所以下一个位置a[i+1]算上a[i]产生的影响,之后又是一个新的子问题(禁止套娃). 对于每一个质因数x,对所有所有位置i求min(a[i],x-a[i])的和,取所有质因子i的求和的最小值作为答案. 1 #define HAVE_STRUCT_TIMESPEC 2 #include

hdu-2685I won&#39;t tell you this is about number theory(数论)

题目链接: I won't tell you this is about number theory Problem Description To think of a beautiful problem description is so hard for me that let's just drop them off. :)Given four integers a,m,n,k,and S = gcd(a^m-1,a^n-1)%k,calculate the S. Input The fi

Codeforces Round #563 (Div. 2) D、Ehab and the Expected XOR Problem

D. Ehab and the Expected XOR Problem Given two integers n and x, construct an array that satisfies the following conditions: for any element ai in the array, 1≤ai<2^n there is no non-empty subsegment with bitwise XOR equal to 0 or x, its length l sho

Acdream 1114 Number theory 莫比乌斯反演

http://acdream.info/problem?pid=1114 题目大意,给你一个序列a,求出这个序列中互质数的有多少对.其中所有的整数的都小于等于222222. f(d) 为 gcd 恰好为 d 的数的对数, F(d) 为 gcd 为 d 的倍数的对数, μ(d) 表示莫比乌斯函数 F(d) = ∑ f(n) 其中( n % d == 0 ) 莫比乌斯反演一下就可以得到, f(d) = ∑ μ(n / d) * F(n) 其中( n % d == 0) 所以我们最后所要的答案就是 f

Number Theory(数论-对数)

X - Number Theory Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Factorial of an integer is defined by the following function f(0) = 1 f(n) = f(n - 1) * n, if(n > 0) So, factorial of 5 is 120. B

Codeforces Round #196 (Div. 2) B. Routine Problem

screen 尺寸为a:b video 尺寸为 c:d 如果a == c 则 面积比为 cd/ab=ad/cb (ad < cb) 如果b == d 则 面积比为 cd/ab=cb/ad  (cb < ad) 如果不相等时 如果a/b > c/d,则ad/bd > cb/db 则(ad > cb) screen尺寸可为 ad:bd, video的尺寸可为 cb:db 面积比为:cb*db/ad*bd = cb/ad (ad > cb) 如果a/b < c/d,则a

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 article and ask us, If i have problem of the SN number display , how can I sort it out? Please do not worry,  here we would tell you the detail steps h

2013年北京师范大学新生程序设计竞赛网络赛--D. Number theory(模拟取余)

D. Number theory Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 34055 Font Size:  +   - 数学不仅是简单而且是美的.数学很有趣,但是数学中也有很多难题,比如哥德巴赫猜想.各种欧拉定理.拉格朗日中值定理.费马定理等.今天小若遇