D. Number into Sequence(唯一分解定理)

传送门

题意:给一个n,求a1*a2....ak=n; 要求k最大,并且a[i+1]/a[i]==0;
分析:对n进行分解,假设个数最多(k)的素因子为 pi,那么
     输出序列为 (k-1)个pi,和n/(pow(pi,k-1));
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 1e5+5;
bool is_prime(ll n){
    if(n==2) return false;
    ll len=sqrt(n);
    for(ll i=2;i<=len;i++)
        if(n%i==0) return true;
    return false;
}
int main()
{
    int t=read();
    while (t--)
    {
        ll n;
        scanf("%lld",&n);
        ll f=n;

        if(!is_prime(n)) printf("1\n%lld\n",n);
        else
        {
            int pi[500]={0},k=0;
            for(int i=2;i<=sqrt(n);i++){
                while (n%i==0){
                    n/=i;
                    pi[++k]=i;
                }
            }
            if(n>1) pi[++k]=n;
            int ans=-INF;
            int ff=pi[1],pos=-1;
            for(int i=2;i<=k;i++){
                int x=1;
                while (pi[i]==ff&&i<=k) i++,x++;
                if(ans<x) ans=x,pos=i-1;
                ff=pi[i];
            }
            if(ans==1) {printf("1\n%lld\n",f);continue;}
            printf("%d\n",ans);
            for(int i=1;i<=ans-1;i++)
                printf("%d ",pi[pos]);
            printf("%lld\n",f/(ll)pow(pi[pos],ans-1));
        }
    }

    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值