Problem L. Little Difference
Input file: little.in Time limit: 3 seconds
Output file: little.out Memory limit: 512 megabytes
Little Lidia likes playing with numbers. Today she has a positive integer n, and she wants to decompose
it to the product of positive integers.
Because Lidia is little, she likes to play with numbers with little difference. So, all numbers in
decomposition should differ by at most one. And of course, the product of all numbers in the
decomposition must be equal to n. She considers two decompositions the same if and only if they
have the same number of integers and there is a permutation that transforms the first one to the second
one.
Write a program that finds all decompositions, which little Lidia can play with today.
Input
The only line of the input contains a single integer n (1 ≤ n ≤ 1018).
Output
In first line output the number of decompositions of n, or −1 if this number is infinite. If number
of decompositions is finite, print all of them one per line. In each line first print number ki of
elements in decomposition. Then print ki
integers in this decomposition in any order. Don’t forget
that decompositions which are different only in order of elements are considered the same.
Examples
little.in little.out
12
3
1 12
3 2 3 2
2 4 3
1
-1
In the second example 1 can be represented as product of any number of ones.
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const LL N = 1e6 + 10;
LL n, len = 0, s[N];
vector<LL> ans[N];
inline void solve(LL x) {
if (n % x || x == 1) return;
LL m = n;
LL l = 0;
while (m % x == 0) {
m /= x;
s[++l] = x;
}
while (m % (x + 1) == 0) {
m /= (x + 1);
s[++l] = x + 1;
}
if (m == 1) {
ans[len].push_back(l);
for (LL i = 1; i <= l; i++) ans[len].push_back(s[i]);
ans[len][0] = l;
len++;
}
}
int f(LL x) {
while (x % 2 == 0) x /= 2;
if (x == 1) return 1;
return 0;
}
int main() {
// freopen("little.in", "r", stdin);
// freopen("little.out", "w", stdout);
scanf("%lld", &n);
if (f(n))
printf("-1\n");
else {
solve(n);
LL m = LL(sqrt(n));
solve(m);
LL r = 1000006;
for (LL i = 2; i <= r; i++) {
if (i == m || i == n) continue;
solve(i);
}
printf("%lld\n", len);
for (LL i = 0; i < len; i++) {
printf("%lld", ans[i][0]);
for (LL j = 1; j <= ans[i][0]; j++) {
printf(" %lld", ans[i][j]);
}
printf("\n");
}
}
}