Ultraviolet A 993: Product of digits

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.