fork(4) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int cnt[111];
  5. bool check(long long n, int k) {
  6. for(int i = 0; i < k; ++i)
  7. cnt[i] = 0;
  8.  
  9. long long u = n;
  10. while (u) {
  11. cnt[u % 10]++;
  12. u /= 10;
  13. }
  14.  
  15. for(int pos = k-1; pos >= 0; --pos)
  16. if (cnt[pos] != n % 10) return false;
  17. else n /= 10;
  18. return true;
  19. }
  20.  
  21. int main() {
  22. int ntest; cin >> ntest;
  23. while (ntest--) {
  24. int k; cin >> k;
  25. long long lb = 1; for(int turn = 0; turn < k-1; ++turn) lb *= 10;
  26. long long ub = 1; for(int turn = 0; turn < k; ++turn) ub *= 10; --ub;
  27.  
  28. bool ok = false;
  29. for(long long x = lb; x <= ub; ++x) {
  30. if (check(x, k)) {
  31. ok = true;
  32. cout << x << ' ';
  33. }
  34. }
  35. if (!ok) cout << -1 << endl;
  36. else cout << endl;
  37. }
  38. }
  39.  
Time limit exceeded #stdin #stdout 5s 3300KB
stdin
10
1
2
3
4
5
6
7
8
9
10
stdout
-1
-1
-1
1210 2020 
21200 
-1
3211000