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;
}