fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <tuple>
  5. #include <cstdint>
  6. #include <limits>
  7. #include <algorithm>
  8.  
  9. typedef std::tuple<std::uint64_t,std::uint64_t, std::uint64_t> Triple;
  10. typedef std::vector<Triple> RType;
  11. typedef std::vector<std::uint64_t> DType;
  12.  
  13. DType SepDigit(std::uint64_t N) {
  14. std::uint64_t V = 0;
  15. DType D;
  16. while (N != 0){
  17. V = N % 10;
  18. N /= 10;
  19. D.push_back(V);
  20.  
  21. }
  22. return D;
  23. }
  24.  
  25. bool Check(std::uint64_t N, std::uint64_t M) {
  26. DType A = SepDigit(N);
  27. DType B = SepDigit(M);
  28.  
  29. for (size_t i = 0; i < B.size(); i++){
  30. if (A[i] != B[i]) return false;
  31. }
  32. return true;
  33. }
  34.  
  35. RType MakeHoge(std::uint64_t N) {
  36.  
  37. std::uint64_t Root = 0;
  38. double V=0;
  39. RType R;
  40.  
  41. for (std::uint64_t i = 2; i < N; i++){//1は自明。
  42. // Root = std::round(std::pow(i,0.5));//sqrt
  43. Root = std::numeric_limits<std::uint64_t>::digits;
  44. for (std::uint64_t j = 2; j <= Root; j++){
  45. V = std::pow(i,1 / static_cast<double>(j));
  46. if (V == static_cast<std::uint64_t>(V)) {
  47. if (Check(i, static_cast<std::uint64_t>(V)) == true) {
  48. R.push_back(std::make_tuple(static_cast<std::uint64_t>(V), j, i));
  49. }
  50. }
  51. }
  52. }
  53. return R;
  54. }
  55.  
  56. bool Show(RType R, bool IsSort = false) {
  57. std::uint64_t N = 0;
  58. std::uint64_t M = 0;
  59. std::uint64_t V = 0;
  60. if (IsSort == true) std::sort(R.begin(), R.end());
  61. for (auto&o : R) {
  62. std::tie(N, M, V) = o;
  63. std::cout << N << '^' << M << " -> " << V << std::endl;
  64. }
  65. return true;
  66. }
  67.  
  68. int main() {
  69. std::uint64_t N = 300000;
  70. RType R;
  71.  
  72. R = MakeHoge(N);
  73. Show(R,true);
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 3.35s 3468KB
stdin
Standard input is empty
stdout
2^5 -> 32
2^9 -> 512
2^13 -> 8192
2^17 -> 131072
3^5 -> 243
3^9 -> 19683
4^5 -> 1024
5^2 -> 25
5^4 -> 625
6^2 -> 36
6^4 -> 1296
25^2 -> 625
76^2 -> 5776
376^2 -> 141376