Time limit 1000 ms

Memory limit 65536 kB

OS Windows

中文题意

给一个数n,设将n质因数分解后可以得到

\[n=\prod_{i=1}^{\omega(n)} a_i^{p_i}
\]

其中\(\omega(n)\)意思是n的不同质因子个数,\(a_i\)是n的质因子。

要求输出最小的\(p_i\)。

题解

看完题解感觉很妙啊——

Let's first factorize \(N\) using prime numbers not larger than \(N^{\frac{1}{5}}\). And let's denote \(M\) as the left part, all the prime factors of \(M\) are larger than \(N^{\frac{1}{5}}\). If \(M=1\) then we are done, otherwise M can only be \(P^2\), \(P^3\), \(P^4\) or \(P^2 \times Q^2\), here \(P\) and \(Q\) are prime numbers.

  1. If \(M^{\frac{1}{4}}\) is an integer, we can know that \(M=P^4\). Update answer using 4 and return.
  2. If \(M^{\frac{1}{3}}\) is an integer, we can know that \(M=P^3\). Update answer using 3 and return.
  3. If \(M^{\frac{1}{2}}\) is an integer, we can know that \(M=P^2\) or \(M=P^2 \times Q^2\). No matter which situation, we can always update answer using 2 and return.
  4. If (1)(2)(3) are false, we can know that answer=1.

Since there are just \(O(\frac{N^{\frac{1}{5}}}{log(N)})\) prime numbers, so the expected running time is \(O(\frac{TN^{\frac{1}{5}}}{log(N)})\).

官方题解导致我一开始没反应过来的地方在于

M can only be \(P^2\), \(P^3\), \(P^4\) or \(P^2 \times Q^2\),

不是only,\(M\)还可以是\(PQRS\)、\(P^2QR\)、\(PQR\)、\(P^3Q\)、\(P^2Q\),之类的,而这些的答案都是1,也就是题解里编号4所说的,不是前三种情况。

另外一个问题,如何判断\(\sqrt[4]M\)、\(\sqrt[3]M\)、\(\sqrt{M}\)是否是整数呢?我们可以求出\(\sqrt[4]M\)、\(\sqrt[3]M\)、\(\sqrt{M}\)向下取整后的结果,再乘回去,比如,看看是否存在\(\lfloor\sqrt{M}\rfloor^2==M\)。

还有,求\(\lfloor\sqrt[3]{M}\rfloor\)时,pow函数精度不太够,用pow(n,1.0/3.0)会WA,要二分法求。下面这段二分法的代码来自这个博客代码里的two函数,要是有整数解就返回整数解,否则返回负一,这倒是挺好。

还有……复杂度那个log哪里来的?和质数分布有关?

源代码

#include<stdio.h>
#include<math.h>
#include<algorithm>
int T;
long long n;
long long cnt=0;
long long prime[10000];
long long ans;
bool vis[10000]; void shai()//取变量名一次回到解放前。还别说,挺方便的,一目了然
{
for(int i=2;i<=10000;i++)
{
if(!vis[i]) prime[++cnt]=i;
for(int j=1;j<=cnt&&i*prime[j]<=10000;j++)
{
vis[i*prime[j]]=1;
if(i%prime[j]==0)
break;
}
}
} long long sancigeng(long long a)
{
long long l=1,r=(long long)pow(n*1.0, 1.0 / 3) + 1,mid;
while(l<=r){
mid=(l+r)>>1;
if(mid*mid*mid==n) return mid;
else if(mid*mid*mid>n) r=mid-1;
else l=mid+1;
}
return -1;
} int main()
{
shai();
//freopen("test.in","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
ans=0x7fffffff;
int pw,pr;
for(int i=1;i<=cnt;i++)
{
if(n%prime[i]==0)
{
pr=prime[i],pw=0;
while(n%pr==0)
{
n/=pr;
pw++;
}
if(pw<ans) ans=pw;
}
if(ans==1)
{
n=1;
break;
}
}
if(n==1)
{
printf("%lld\n",ans);
continue;
}
long long temp1=sqrt(n),temp2=sqrt(temp1),temp3=sancigeng(n);
if(temp2*temp2==temp1&&temp1*temp1==n)
{//algorithm里的max和min不支持long long和int混杂的参数,居然不会隐式类型转换
ans=std::min(ans,4LL);
}
else if(temp3>=0)
{
ans=std::min(ans,3LL);
}
else if(temp1*temp1==n)
{
ans=std::min(ans,2LL);
}
else ans=1;
printf("%lld\n",ans);
}
return 0;
}

HDU 6623 Minimal Power of Prime的更多相关文章

  1. HDU 6623"Minimal Power of Prime"(数学)

    传送门 •题意 给你一个大于 1 的正整数 n: 它可以分解成不同的质因子的幂的乘积的形式,问这些质因子的幂中,最小的幂是多少. •题解 定义 $ans$ 表示最终答案: ①如果 $ans \ge 5 ...

  2. HDU 6623 Minimal Power of Prime(数学)

    传送门 •题意 给你一个大于 1 的正整数 n: 它可以分解成不同的质因子的幂的乘积的形式,问这些质因子的幂中,最小的幂是多少. •题解 把[1,10000]内的素数筛出来,然后对于每个素$P$数遍历 ...

  3. HDU 6623 Minimal Power of Prime(思维)题解

    题意: 已知任意大于\(1\)的整数\(a = p_1^{q_1}p_2^{q_2} \cdots p_k^{q_k}\),现给出\(a \in [2,1e18]\),求\(min\{q_i\},q ...

  4. 2019杭电多校第四场hdu6623 Minimal Power of Prime

    Minimal Power of Prime 题目传送门 解题思路 先打\(N^\frac{1}{5}\)内的素数表,对于每一个n,先分解\(N^\frac{1}{5}\)范围内的素数,分解完后n变为 ...

  5. [2019杭电多校第四场][hdu6623]Minimal Power of Prime

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6623 题目大意为求一个数的唯一分解的最小幂次.即120=23*31*51则答案为1. 因为数字太大不能 ...

  6. 2019 Multi-University Training Contest 4 - 1010 - Minimal Power of Prime

    http://acm.hdu.edu.cn/showproblem.php?pid=6623 题意,给50000个1e18级别的数N,求它质因数分解里面的最小的指数(不算0) 比赛的时候给划了一个1e ...

  7. 2019HDU多校Minimal Power of Prime——分段讨论&&思维

    题目 将 $n$($1 < n \leq 10^{18}$)质因数分解,求质因数幂的最小值. 分析 直接质因数分解,不太行. 可以这样想,对小区间质因数分解,n变小了,再枚举答案. 打印1-10 ...

  8. 2019hdu多校 Minimal Power of Prime

    题目链接:Click here 题目大意:求一个数分解质因数后的最小幂指数 Solution: 首先,我们肯定是不能直接暴力求解的 我们先考虑筛出1e4范围以内的所有质数,把x所有这个范围内的质因子筛 ...

  9. 【HDOJ6623】Minimal Power of Prime(Powerful Number)

    题意:给定大整数n,求其质因数分解的最小质数幂 n<=1e18 思路:常规分解算法肯定不行 考虑答案大于1的情况只有3种:质数的完全平方,质数的完全立方,以及p^2*q^3,p,q>=1三 ...

随机推荐

  1. 第八周作业总结&第六次实验报告

    实验六 Java异常 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获方法. 实验要求 练习捕获异常.声明异常.抛出异常的方法.熟悉try和catch子句的使用. 掌握自定义异常类 ...

  2. spring boot @Transactional的一个小坑

    同一个类Service下,有两个函数 method_1和 method_2,且method_1内部调用了method_2,那么希望method_2内部意外时,数据库回滚,那么一定要在method_1上 ...

  3. P1177快速排序

    这是一个快速排序的模板题.拿到题后便写了quicksort(确定一个基准数,利用两个哨兵,把大的放右边,小的放左边,再递归实现排序),但是竟然TLE了60pts(???),于是翻看dalao们的题解, ...

  4. RabbitMq学习4-发布/订阅(Publish/Subscribe)

    一.发布/订阅 分发一个消息给多个消费者(consumers).这种模式被称为“发布/订阅”. 为了描述这种模式,我们将会构建一个简单的日志系统.它包括两个程序——第一个程序负责发送日志消息,第二个程 ...

  5. Spring KafkaTemplate 注解式实现 工厂模式

    实现注解式注入kafkaTemplate 生产者和消费者,简化配置文件 目录 消费者工厂 /** * 消费者工厂 */ @EnableKafka @Configuration public class ...

  6. Python数据基础类型-新建列表

    1,遍历列表 遍历列表的所有元素,对每个元素执行相同的操作.可使用for循环 magicians = ['alice','david','carolina'] for magician in magi ...

  7. Python 入门之数据类型之间的相互转换 以及 在编程中会遇到的数据类型的坑

    Python 入门之数据类型之间的相互转换 以及 在编程中会遇到的数据类型的坑 1.数据类型总结: 可变,不可变,有序,无序 (1)可变的数据类型:list dict set (2)不可变的数据类型: ...

  8. php配置伪静态如何将.htaccess文件转换 nginx伪静态文件

    php通常设置伪静态三种情况,.htaccess文件,nginx伪静态文件,Web.Config文件得形式,如何将三种伪静态应用到项目中呢, 1,.htaccess文件 实例 <IfModule ...

  9. [51Nod1623] 完美消除

    link $solution:$ 首先我们可以发现一个结论,对于一个数 $x$ ,它的最低修改次数为它每位与前去中是否都比此位上的数大,有则答案 $-1$ .因为若有小数则没有办法将其答案贡献变低. ...

  10. Redis---Redis与Memcached

    4.Redis与Memcached   两者都是非关系型内存键值,主要有以下不同: 数据类型   Memcached仅支持字符串类型,而Redis支持五种不同的数据类型,可以更灵活地解决问题. 数据持 ...