C. Product of Three Numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Example
input
5
64
32
97
2
12345
output
YES
2 4 8
NO
NO
NO
YES
3 5 823
题意:
给你一个数字,要求你求出三个不相等且大于等于2的相乘为n的数。
思路:
如果暴力的三个循环那么肯定就超时了,毕竟109挺大的了,但是如果知道了两个数,那么第三个数就出来了,首先第一个数(也就是最小的数)不能大于103因为三个相乘如果最小的都大于103那么最后一定大于109,同理第二个数一定不大于105,这样遍历一下就行了,如果时间还要更短的话,可以在第一个循环后面加一个n % i != 0 continue。
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
int main() {
int t, n;
read(t);
while (t--) {
read(n);
bool flag = false;
int x = pow(n, 1.0 / 3);
int y = sqrt(n);
for (int i = 2; i <= x; i++) {
if (n % i != 0) continue;
for (int j = i + 1; j <= y; j++) {
if ((n / i) % j == 0 && (n / i / j) <= max(i, j)) break;
if ((n / i) % j == 0 && n / i / j >= 2 && n / i / j != i && n / i / j != j) {
printf("YES\n");
printf("%d %d %d\n", i, j, n / i / j);
flag = true;
break;
}
}
if (flag) break;
}
if (!flag) printf("NO\n");
}
return 0;
}