Product of Three Numbers(CF-1294C)

Problem Description

You are given one integer number n. Find three distinct integers a,b,c such that 2≤a,b,c and a⋅b⋅c=n or say that it is impossible to do it.

If there are several answers, you can print any.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤100) — the number of test cases.

The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2≤n≤109).

Output

For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a⋅b⋅c for some distinct integers a,b,c such that 2≤a,b,c.

Otherwise, print "YES" and any possible such representation.

Examples

Input

5
64
32
97
2
12345

Output

YES
2 4 8
NO
NO
NO
YES
3 5 823

題意:t 組數據,每組給出一個數 n,問 n 是否能分解成 a*b*c 的形式,要求 a、b、c 大於等於 2 並且不相等,若存在,輸出任意一個方案

思路:

將 n 分解成 a*b*c 的形式,那麼 a、b、c 這三個數一定是 n 的因子,因此首先將 n 進行因子分解,並統計其素因子個數,然後判斷是否有三個不一樣的因子

當 n 存在一個素因子 x 時:最簡單的方案是 x * (x*x) * (x*x*x),也即 n 中至少存在 6 個以上的 x 時,才存在合法方案

當 n 存在兩個素因子 x1、x2 時:最簡單的方案是 (x1*x2)*x1*x2、(x1*x1)*x1*x2、(x2*x2)*x1*x2,也即 n 中的 x1、x2 個數和至少達到 4 個時,才存在合法方案

當 n 存在三個及以上的素因子時,一定存在合法方案

而輸出合法方案是一個問題,可以發現,如果有了 a、b,那麼 c 就不需要進行暴力枚舉,直接利用 c=n/(a*b) 即可計算得出

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;

map<LL, int> num;   //記錄素因子個數
LL factor[N], cntF; //記錄素因子
void getFactor(LL n) {
    cntF = 0;
    num.clear();
    for (LL i = 2; i <= n / i; i++) {
        if (n % i == 0) //記錄素因子
            factor[cntF++] = i;
        while (n % i == 0) {
            n /= i;
            num[i]++; //記錄素因子個數
        }
    }
    if (n > 1) {
        factor[cntF++] = n;
        num[n]++;
    }
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        LL n;
        scanf("%lld", &n);
        getFactor(n);
        if (cntF == 1) {
            if (num[factor[0]] >= 6) {
                LL a = factor[0];
                LL b = factor[0] * factor[0];
                LL c = n / (a * b);
                printf("YES\n");
                printf("%lld %lld %lld\n", a, b, c);
            } 
            else
                printf("NO\n");
        } 
        else if (cntF == 2) {
            if (num[factor[0]] + num[factor[1]] >= 4) {
                LL a = factor[0];
                LL b = factor[1];
                LL c = n / (a * b);
                printf("YES\n");
                printf("%lld %lld %lld\n", a, b, c);
            } 
            else
                printf("NO\n");
        } 
        else {
            LL a = factor[0];
            LL b = factor[1];
            LL c = n / (a * b);
            printf("YES\n");
            printf("%lld %lld %lld\n", a, b, c);
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章