UVALive - 5110 Square-Free Numbers ( 唯一分解定理

题目描述

You all know about factorization of an integer. Here we want you to factor a number into as few factors
as possible. That is easy, you say, just have the number itself, and that will be the smallest number of
factors i.e. 1.
But wait, I haven’t finished — each of the factors that you find must be square-free. A square-free
number, however you factor it, won’t have any factor that is a perfect square. Of course, you can never
include 1 as a factor.

输入

The first line of input is the number of test cases T.
The next T lines each have an integer N.

输出

For each testcase, output the smallest number of square-free factors.
Constraints:
• T ≤ 104
• 2 ≤ N ≤ 106
Explanation:
6 can be factored as just 6 (further factorable as 2 × 3 only, and hence square free), a single factor.
8 has to be factored as 2 × 2 × 2 so that all factors are square-free.

样例

Sample Input
2
6
8
Sample Output
1
3

题意

唯一分接定理 求的是质因子最的幂 QAQ

AC代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))
#define ls st<<1
#define rs st<<1|1

const int MAXN = 1e6+10;
int prime[MAXN];
void getPrime()
{
    memset(prime, 0, sizeof(prime));
    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;
    cin >> T;
    int k = 0;
    while(T--) {
        LL n;
        cin >> n;
        int ans = 0;
        for(int i = 1; i < prime[0] && prime[i] <= n; i++) {
            if(n%prime[i]==0) {
                int kk = 0;
                while(n%prime[i]==0) {
                    n /= prime[i];
                    kk++;
                }
                ans = max(kk,ans);
            }
        }
        cout << ans << endl;
    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值