fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cstdint>
  6. typedef std::vector<double> DType;
  7. typedef std::vector<DType> RType;
  8.  
  9. bool Adder(DType& D, std::uint64_t R) {
  10. if (D.size() == 0)return false;
  11. D[0]++;
  12. bool C = false;
  13. for (std::size_t i = 0; i < D.size(); i++) {
  14. if (C == true) {
  15. D[i]++;
  16. C = false;
  17. }
  18.  
  19. if (D[i] >= R-1) {
  20. D[i] = 0;
  21. C = true;
  22. }else {
  23. break;
  24. }
  25. }
  26.  
  27. return C;
  28. }
  29.  
  30.  
  31. double Hypot3(double X, double Y, double Z) {//C++17 have default this.that is more better
  32. return sqrt(X*X + Y*Y + Z*Z);
  33. }
  34. RType SelectFor3D(const std::uint64_t& Rad) {
  35. DType D{ 0,1,1 };
  36. RType R;
  37. while (!Adder(D,Rad)){
  38. // std::cout << D[0] << ',' << D[1] << ',' << D[2] << '\r';
  39. double V = Hypot3(D[0], D[1], D[2]);
  40. if (V == std::floor(V)) {
  41. R.push_back(D);
  42. }
  43. }
  44. return R;
  45. }
  46.  
  47. RType SelectFor2D(RType& D) {
  48. RType R;
  49. bool F = false;
  50. for (auto& o : D) {
  51. std::sort(o.begin(), o.end());
  52. do {
  53. double V = std::hypot(o[0], o[1]);
  54. if (V != std::floor(V)) {
  55. F = true;
  56. break;
  57. }
  58. } while (std::next_permutation(o.begin(),o.end()));
  59. if (F == true) {
  60. R.push_back(o);
  61. }
  62. }
  63. return R;
  64. }
  65. bool Show(RType& R) {
  66. for (auto& oo : R) {
  67. for (auto& o : oo) {
  68. std::cout << o << ',';
  69. }
  70. std::cout << std::endl;
  71. }
  72. return true;
  73. }
  74. RType MakeHoge(const std::uint64_t& Rad) {
  75. RType R;
  76. std::cout << "start 篩 by 3D" << std::endl;
  77. R = SelectFor3D(Rad);
  78. Show(R);
  79. std::cout << "start 篩 by 2D" << std::endl;
  80. R = SelectFor2D(R);
  81.  
  82. return R;
  83.  
  84. }
  85.  
  86. int main()
  87. {
  88. RType R;
  89.  
  90. R = MakeHoge(10001);
  91. Show(R);
  92.  
  93. return 0;
  94. }
  95.  
Time limit exceeded #stdin #stdout 5s 6176KB
stdin
Standard input is empty
stdout
start 篩 by 3D