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.
Input
The first line of input is the number of test cases T.
The next T lines each have an integer N.
Output
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

#include<bits/stdc++.h>
using namespace std;

const int MAX = 1e6+10;
const int INF = 0x3fffffff;
int a[60];
int sz=0;
int f(int n){
    sz = 0;
    memset(a,0,sizeof(a));
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            while(n%i==0){
                a[sz]++;
                n/=i;
            }
            ++sz;
        }
    }
    if(n>1) a[sz++]=1;
    sort(a,a+sz);
    return a[sz-1];
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        scanf("%d",&n);
        long long ans = f(n);
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值