Codeforces 1294C Product of Three Numbers(质因子分解)

传送门

题意:

给一个整数n,问是否存在3个不同的数 a , b , c , a,b,c, a,b,c, 2 ≤ a , b , c , a ⋅ b ⋅ c = n 2≤a,b,c , a \cdot b \cdot c = n 2a,b,c,abc=n,如果存在输出yes,并输出任一满足的三个数,否则输出no

思路:

将n进行质因子分解,然后进去讨论判断即可,具体看代码

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int MAXN=2e5+50;
const double pi=3.1415926536;
int t,n;
int a[1001],b[1001];
int cnt;
void get(int n){
    memset(a,0,sizeof(a));//存质因子
    memset(b,0,sizeof(b));//存对应质因子的个数
    cnt=0;
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            a[++cnt]=i;
            while(n%i==0){
                n/=i;
                b[cnt]++;
            }
        }
    }
    if(n>1){
        a[++cnt]=n;
        b[cnt]=1;
    }
    int flag=1;
    int ans=0;
    for(int i=1;i<=cnt;i++){
        ans+=b[i];
    }
    if((ans<6&&cnt==1)||(ans<4&&cnt==2)){//不满足的情况,凑不够三个不同的数
       printf("NO\n");
       return ;
    }
    else
    {
        printf("YES\n%d ",a[1]);//若能凑够,a[1]可以是第一个,然后后面的讨论,写的有的麻烦
        b[1]--;
        if(b[1]==0){
            printf("%d ",a[2]);
            b[2]--;
            int p=1;
            for(int i=2;i<=cnt;i++){
                while(b[i]--)p*=a[i];
            }
            printf("%d\n",p);//这里的p直接等于n/a[1]/a[2]即可,其实不用算了
        }
        else
        {
            if(b[1]>=2){
                printf("%d ",a[1]*a[1]);
                b[1]-=2;
                int p=1;
                for(int i=1;i<=cnt;i++){
                    while(b[i]--)p*=a[i];
                }
                printf("%d\n",p);
            } else{
                printf("%d ",a[1]*a[2]);
                b[2]--;
                int p=1;
                for(int i=2;i<=cnt;i++){
                    while(b[i]--)p*=a[i];
                }
                printf("%d\n",p);
            }
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        get(n);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值