按三进制枚举,理论上没有问题吧
我可不会告诉你样例太大我调不动
#include <iostream>
using namespace std;
const int power = 59049;
int n, tot, res[power];
int main()
{
ios::sync_with_stdio(0);
cin >> n;
for (int i = power - 1; i >= 0; --i)
{
int sum = 0, x;
x = i;
for (int j = 0; j < 10 && sum <= n; ++j)
{
sum += x % 3 + 1;
x /= 3;
}
if (sum == n) res[tot++] = i;
}
cout << tot << endl;
for (int i = 0; i < tot; ++i)
{
for (int j = 0; j < 10; ++j)
{
cout << res[i] % 3 + 1 << " ";
res[i] /= 3;
}
cout << endl;
}
return 0;
}