A factorization problem.
A straight-forward solution would be dividing the input by the numbers from 9 down to 2 and build the output. If after these divisions the input has value other than 1, then it can’t be solved (there are factors larger than 9).
C++ implementation:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef unsigned long long int64; int n; int64 q, d; int main() { int nCase; scanf("%d", &nCase); while (nCase--) { // in scanf("%d", &n); if (n == 0) { printf("10\n"); continue; } else if (n < 10) { printf("%d\n", n); continue; } d = (q = 0) + 1; // solve for (int i = 9; i > 1; --i) while (n % i == 0) { n /= i; q += i * d; d *= 10; } // out if (n == 1) printf("%lld\n", q); else printf("-1\n"); } return 0; }