This question is very simple. First, use N to break down the factor with a prime number of, (that is, a prime number of less than 10) (first, we need to make a special judgment that N is not 1 ), then merge the factors that can be merged (for example, 2*2 into 4). The number of digits of the result is reduced, and the size is certainly smaller. For specific implementation, see the code.
My problem-solving code is as follows:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <string> # include <algorithm> using namespace std; int c [12]; int T; int N; int main () {cin> T; while (T --) {cin> N; if (N = 1) {cout <1 <endl; continue;} memset (c, 0, sizeof (c); while (N! = 1) // decompose N {if (N % 2 = 0) {c [2] ++; N/= 2 ;} else if (N % 3 = 0) {c [3] ++; N/= 3;} else if (N % 5 = 0) {c [5] ++; N/= 5;} else if (N % 7 = 0) {c [7] ++; N/= 7 ;} else break;} if (N! = 1) {cout <-1 <endl; continue;} while (1) // merge the N factor {if (c [2]> = 3) {c [2]-= 3; c [8] ++;} // There are three two factors, merged into 8 else if (c [2]> = 2) {c [2]-= 2; c [4] ++;} // There are two 2, merged into 4 else if (c [3]> = 2) {c [3]-= 2; c [9] ++;} // There are two 3, merged into 9 else if (c [2]> = 1 & c [3]> = 1) {c [2] --; c [3] --; c [6] ++;} // There is a 2 and a 3, merged into 6 else break;} for (int I = 2; I <= 9; I ++) {// output result while (c [I]) {cout <I; c [I] -- ;}} cout <endl ;} return 0 ;}
The attached questions are as follows:
For a given non-negative integer number N, find the minimal natural Q such that the product of all digits of Q is equal N.
Input
The first line of input contains one positive integer number, which is the number of data sets. Each subsequent line contains one data set which consists of one non-negative integer number N (0N109 ).
Output
For each data set, write one line containing the corresponding natural number Q or '-1' if Q does not exist.
Sample Input
3
1
10
123456789
Sample Output
1
25
-1