fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. void fun() {
  6. ios_base::sync_with_stdio(0);
  7. cin.tie(0);
  8. cout.tie(0);
  9. }
  10.  
  11. int const M = 1e6;
  12. int a[M];
  13. bool used[M];
  14. int main() {
  15. fun();
  16. int n; cin >> n;
  17. while (n--) {
  18. int p; cin >> p;
  19. int s = ceil(sqrt(p));
  20. int w = s * s;//4
  21. p -= 1;//3
  22. bool v = 1;
  23. for (int i = p ; i >= 0; --i) {
  24. // 4-2 3 2
  25. if (((w - i) <= p) &&(!used[ w-i])) {
  26. a[i] = w-i;
  27. used[w-i] = 1;
  28. }
  29. else {
  30. s--;
  31. if (s == -1) {
  32. v = 0;
  33. break;
  34. }
  35. w = (s ) * (s);
  36. i++;
  37. }
  38. }
  39. if (v) {
  40. for (int i = 0; i <= p; ++i)cout << a[i] << ' ';
  41. cout << endl;
  42. }
  43. else cout << -1 << endl;
  44. for (int i = 0; i <= p; ++i) {
  45. a[i] = 0;
  46. used[i] = 0;
  47. }
  48.  
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5804KB
stdin
3
3
4
7
stdout
1 0 2 
0 3 2 1 
1 0 2 6 5 4 3