zoukankan      html  css  js  c++  java
  • Codeforces 1294C Product of Three Numbers(质因子分解)

    传送门

    题意:

    给一个整数n,问是否存在3个不同的数(a,b,c,) (2≤a,b,c , a cdot b cdot c = 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
    ");
           return ;
        }
        else
        {
            printf("YES
    %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
    ",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
    ",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
    ",p);
                }
            }
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            get(n);
        }
        return 0;
    }
    
  • 相关阅读:
    windows加固方案
    redis集群
    tar命令
    nfs安装配置
    nginx php版本隐藏
    细谈select函数(C语言)
    linux 下各errno的意义(转)
    iperf交叉编译:
    主机和虚拟机不能ping通问题
    Linux中tcpdump的编译和使用
  • 原文地址:https://www.cnblogs.com/zzl_Alexander/p/12250595.html
Copyright © 2011-2022 走看看