3164 质因数分解

3164 质因数分解

 

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 黄金 Gold
 查看运行结果
 
 
题目描述  Description

(多数据)给出t个数,求出它的质因子个数。

数据没坑,难度降低。

输入描述  Input Description

第一行 t

之后t行 数据

输出描述  Output Description

t行 分解后结果(质因子个数)

样例输入  Sample Input

2

11

6

样例输出  Sample Output

1

2

数据范围及提示  Data Size & Hint

(样例解释)11自己本身是一个质数,所以计入其中。

顺便提示:t<=100000。每个数小于long long unsigned 呵呵

分类标签 Tags 点此展开 
 
 
第一次,直接上唯一分解定理,结果WA+TLE ??
WA代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
#define ll unsigned long long
#define pir pair<ll,ll>
vector<pir> f;
inline const ll read(){
    register ll x=0;
    register char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x;
}
void deal(ll x){
    f.clear();
    ll m=floor(sqrt(x)+0.5);
    for(ll i=2;i<=m;i++){
        if(x%i) continue;
        ll q=0;
        while(x%i==0) q++,x/=i;
        f.push_back(make_pair(i,q)); 
    }
    if(x>1) f.push_back(make_pair(x,1));
    printf("%d\n",f.size());
}
int main(){
    ll T,n;
    T=read();
    while(T--) n=read(),deal(n);
    return 0;
}

 

暴力过掉
AC代码:
#include<iostream>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    int T;long long n;
    cin>>T;
    while(T--){
        cin>>n;
        int tot=0;
        for(int i=2;i<=n;i++){
            if(n==1) break;
            if(n%i==0){
                do{
                    n/=i;
                    tot++;
                }while(n%i==0);
            }
        }
        cout<<tot<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/shenben/p/5860333.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值