题面
C. Product of Three Numbers
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.
大意:给你一个整数n,是否存在三个大于等于2且互不相等的因数a,b,c 使abc=n;
如果存在,输出YES并输出任意一种可能,否则输出NO。
思路:先算出n的所有因数,然后三个for循环暴力找a,b,c;
#include<bits/stdc++.h>
using namespace std;
int b[100005];
int z;
void find(int a) //找因数的函数。
{
memset(b,0,sizeof(b)); //注意清0
z=0; //同上
for(int i=1;i<=sqrt(a);i++)
{
if(a%i==0)
{
b[z]=i;
z++;
int g=a/i;
b[z]=g;
z++;
}
}
}
int panduan(int a,int b,int c)
{
if(a>=2&&b>=2&&c>=2&&(a!=b&&a!=c&&b!=c))
return 1;
else return 0;
}
int main()
{
int t;
cin >>t;
while(t--)
{
int n;
cin >> n;
find(n);
int flag=0;
int aa,bb,cc;
int i,j,k;
for( i=0;i<z;i++) //暴力找abc
for( j=0;j<z;j++)
for( k=0;k<z;k++)
{
if(panduan(b[i],b[j],b[k])&&b[i]*b[j]*b[k]==n)
{
aa=i;
bb=j;
cc=k;
flag=1;
break;
}
}
if(flag==1)
{
cout << "YES" <<endl;
cout << b[aa] << " " <<b[bb] <<" " << b[cc] <<endl;
}
else if(flag==0)
{
cout << "NO" <<endl;
}
}
return 0;
}