HDU 2019 Multi-University Training Contest 4 杭电多校联合训练赛 第四场 1010 Minimal Power of Prime (6623)
--------------------------------------------分割线-------------------------------------------------------------------------
跪求用米勒拉宾的大佬们告诉我怎么过题!
在线等!急!
-----------------------------------------分割线----------------------------------------------------------------------------
Problem Description
You are given a positive integer n > 1. Consider all the different prime divisors of n. Each of them is included in the expansion n into prime factors in some degree. Required to find among the indicators of these powers is minimal.
Input
The first line of the input file is given a positive integer T ≤ 50000, number of positive integers n in the file. In the next T line sets these numbers themselves. It is guaranteed that each of them does not exceed 10^18.
Output
For each positive integer n from an input file output in a separate line a minimum degree of occurrence of a prime in the decomposition of n into simple factors.
Sample Input
5
2
12
108
36
65536
Sample Output
1
1
2
2
16
------------------------------------------------------分割线---------------------------------------------------------------
题意
给定一个LL范围的n,将n分解质因数n=p1x1+p2x2+……pnxn,求x数组的最小值。
思路
先贴上原题解
对于一个1e18以内的数,我们先用N1/5(5e3左右)范围内的素数对齐进行质因数分解,并记录最小值,还未分解的数记为m。如果此时的m为1,说明质因数分解完成,输出已求出的最小值即可。此时m内可分解出来的质因数§必定大于5e3,我们易知,这个质因数最大幂次只能为4,所以我们分别考虑m是否能开四次方根(m=p4),立方根(m=p3),平方根(m=p2或m=p2*Q2),如果可以开t次方根,只需讲5e3以内的最小值在和t取最小值即是答案。如果能开根号,即说明m本身就是一个素数,答案即为1。
坑点
请先看图:
就算我知道了题解,我还是TLE到绝望。其实还有WA。
首先是怎么开根号,我第一次想到的是用math库自带的pow()函数,但是我忘记了pow()函数的精度问题,WA到自闭。然后就改成二分去判断能不能开某次方根。实际上sqrt(sqrt(n))也可以用来开四次方根,也是可以AC的。
然后再说一下二分,因为二分里面是要判断mid的t次方是否等于m的。所以mid的右端点不能太大(更不能太小),所以需要稍微计算一下右端点的极限值,避免炸LL。
还有就是诡异的TLE,素数筛最大只能筛1e4,2e4就会TLE了,我计算了一下1e18的五次方根是3981多一点,所以我只筛了5e3的素数就够用了。
-----------------------------------------------------分割线----------------------------------------------------------------
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=5000;
int prime[MAXN+5];
bool fun(ll x,int times)
{
ll mid;
ll low=1;
ll top;
if(times==2)
{
top=1000000005;
}
else if(times==3)
{
top=1000005;
}
else if(times==4)
{
top=32000;
}
while (low<=top)
{
mid=(low+top)>>1;
ll tmp=1;
for(int i=0; i<times; i++)
{
tmp*=mid;
}
if (tmp>x)
{
top=mid-1;
}
else if(tmp<x)
{
low=mid+1;
}
else
{
return true;
}
}
return false;
}
void getPrime()
{
for(int i=2; i<=MAXN; i++)
{
if(!prime[i])
prime[++prime[0]]=i;
for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
{
prime[prime[j]*i]=1;
if(i%prime[j]==0)
break;
}
}
}
int main()
{
getPrime();
int T;
scanf("%d",&T);
while(T--)
{
ll n;
scanf("%lld",&n);
int ans=0x3f3f3f3f;
for(int i=1; i<=prime[0]&&n!=1&&prime[i]<=n; i++)
{
int num=0;
while(n%prime[i]==0&&n!=1)
{
n/=prime[i];
num++;
}
if(num!=0)
ans=min(ans,num);
}
if(n==1)
{
printf("%d\n",ans);
continue;
}
if(fun(n,4)==true)
{
printf("%d\n",min(ans,4));
continue;
}
if(fun(n,3)==true)
{
printf("%d\n",min(ans,3));
continue;
}
if(fun(n,2)==true)
{
printf("%d\n",min(ans,2));
continue;
}
printf("1\n");
}
return 0;
}