• HDU 6623"Minimal Power of Prime"(数学)


    传送门

    •题意

      给你一个大于 1 的正整数 n;

      它可以分解成不同的质因子的幂的乘积的形式,问这些质因子的幂中,最小的幂是多少。

    •题解

      定义 $ans$ 表示最终答案;

      ①如果 $ans ge 5$:

        那么,肯定有 $n=p^{ans} , p le sqrt[{ans}]{n}$,也就是说 $ p le 10^{frac{18}{5}}$;

      所以,我们可以提前预处理出 $[1,10000]$ 内的素数,筛出 $n$ 中属于 $[1,10000]$ 内的质因子;

      如果在这个过程中出现 $n=1$ 或者 $ans=1$,那么直接返回 $ans$ 即可;

      如果筛完 $[1,10000]$ 内的素数后,$n > 1$,那么,就有如下情况:

        (1)存在质数 p,满足 p > 10000 并且 n 只能分解出一个 p,此时应输出 1;

        (2)存在质数 p,q,满足 p > 10000 , q > 10000,有 $n = p^2$ 或 $n = p^2 cdot q^2$,对于这种情况,$n$ 肯定是个完全平方数;

        (3)存在质数 p,满足 p > 10000,并且有 $n=p^3$,这种情况下,$n$ 肯定是个立方数;

        (4)存在质数 p,满足 p > 10000,并且有 $n=p^4$;

      如果情况(1)成立,那么,情况(2)(3)(4)肯定不成立,但是情况(1)可能不好直接判断;

      那么,我们可以先判断情况(4)(2)(3)是否成立,如果不成立,那么(1)肯定成立;

      疑惑(1):如果 $(sqrt[4]{n})^4=n$,那为什么一定有 $sqrt[4]{n}$ 为素数呢?

        定义 $x=sqrt[4]{n}$,那么有 $x le 10^{frac{18}{4}}$;

        如果 $x$ 为合数,那么势必存在某个大于 1 因子 f,$f le sqrt{x} < 10^4$;

        但,来到此步的条件是 $n$ 中所有属于 $[1,10000]$ 内的质因子已被筛走,所以,是不存满足条件的 $f$ 的;

           所以说,$x$ 一定是个素数;

      疑惑(2):为什么要先判断情况(4)再判断情况(2):

        因为满足情况(4)肯定满足情况(2),但是此时满足情况(2)的因子就不是质因子了;

    •Code

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define ll long long
     4 #define mem(a,b) memset(a,b,sizeof(a))
     5 const int N=1e4;
     6 
     7 ll n;
     8 int cnt;
     9 int prime[N];
    10 bool isPrime[N+50];
    11 
    12 void Prime()
    13 {
    14     cnt=0;
    15     mem(isPrime,true);
    16     isPrime[1]=false;
    17 
    18     for(int i=2;i <= N;++i)
    19     {
    20         if(isPrime[i])
    21             prime[++cnt]=i;
    22 
    23         int x;
    24         for(int j=1;j <= cnt && (x=i*prime[j]) <= N;++j)
    25         {
    26             isPrime[x]=false;
    27 
    28             if(i%prime[j] == 0)
    29                 break;
    30         }
    31     }
    32 }
    33 bool Calc(ll x)
    34 {
    35     int l=1,r=(int)1e6+1;
    36     while(r-l > 1)
    37     {
    38         ll mid=l+((r-l)>>1);
    39         if(mid*mid*mid > x)
    40             r=mid;
    41         else
    42             l=mid;
    43 
    44         if(mid*mid*mid == x)
    45             return true;
    46     }
    47     return false;
    48 }
    49 int Solve()
    50 {
    51     int ans=100;
    52     for(int i=1;i <= cnt;++i)
    53     {
    54         int k=0;
    55         while(n%prime[i] == 0)
    56         {
    57             k++;
    58             n /= prime[i];
    59         }
    60         if(k)
    61             ans=min(ans,k);
    62 
    63         if(n == 1 || ans == 1)
    64             return ans;
    65     }
    66 
    67     ll x=sqrt(sqrt(n));
    68     ll y=sqrt(n);
    69 
    70     if(x*x*x*x == n)
    71         ans=min(ans,4);
    72     else if(y*y == n)
    73         ans=min(ans,2);
    74     else if(Calc(n))
    75         ans=min(ans,3);
    76     else
    77         ans=1;
    78 
    79     return ans;
    80 }
    81 int main()
    82 {
    83     Prime();
    84 
    85     int T;
    86     scanf("%d",&T);
    87     while(T--)
    88     {
    89         scanf("%lld",&n);
    90         printf("%d
    ",Solve());
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    python3 -- 堆(heapq)
    二叉堆的实现(最大堆)
    解决layui layer.load() Ajax提交时,只在结束闪一下
    两个for循环嵌套,写成一个列表生成式
    time模块,计算当前时间和时间差
    私有化&property
    面向对象魔术方法之__str__()方法
    面向对象魔术方法-__del__()方法
    面向对象之魔术方法__init__()和__new__()
    Django的自定义标签
  • 原文地址:https://www.cnblogs.com/violet-acmer/p/11708166.html
Copyright © 2020-2023  润新知