fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. #include <unordered_set>
  4. #include <random>
  5. #ifdef _MSC_VER
  6. # include <intrin.h>
  7. # define __builtin_popcount __popcnt
  8. #endif
  9.  
  10. //#include <atcoder/all>
  11. //using namespace atcoder;
  12. //typedef modint998244353 mint;
  13.  
  14. using namespace std;
  15.  
  16. typedef long long int lld;
  17. typedef long double ld;
  18. typedef pair<int, int> pii;
  19. typedef pair<lld, lld> pll;
  20. typedef vector<int> vi;
  21. typedef vector<lld> vl;
  22. typedef vector<ld> vld;
  23. typedef vector<char> vch;
  24. typedef vector<string> vs;
  25. typedef vector<bool> vb;
  26. typedef vector<double> vd;
  27. typedef vector<pii> vpii;
  28. typedef vector<pll> vpll;
  29. typedef vector<vi> vivi;
  30. typedef vector<vl> vlvl;
  31. typedef vector<vch> vcvc;
  32. typedef vector<vb> vbvb;
  33. typedef vector<vs> vsvs;
  34.  
  35. const int mod = 2012;
  36. const int inf = 1LL << 28;
  37. const int nom = 1000000;
  38.  
  39. int mul(int a, int k)
  40. {
  41. int tp = 1;
  42.  
  43. while (k--)
  44. tp *= a;
  45.  
  46. return tp;
  47. }
  48.  
  49. int main()
  50. {
  51. cin.tie(NULL), cout.tie(NULL);
  52. ios::sync_with_stdio(false);
  53.  
  54. int a, b, k;
  55. lld fin = 0;
  56.  
  57. cin >> a >> b >> k;
  58.  
  59. for (int i = a; i <= b; ++i)
  60. {
  61. int tp = i, tp2, mn;
  62. set<int> st;
  63.  
  64. mn = i;
  65. st.insert(i);
  66.  
  67. while (1)
  68. {
  69. tp2 = 0;
  70.  
  71. while (tp)
  72. {
  73. tp2 += mul(tp % 10, k);
  74. tp /= 10;
  75. }
  76.  
  77. if (st.find(tp2) != st.end())
  78. break;
  79. else
  80. st.insert(tp2);
  81.  
  82. mn = min(mn, tp2);
  83. tp = tp2;
  84. }
  85.  
  86. fin += mn;
  87. }
  88.  
  89. cout << fin;
  90.  
  91. cout << '\n';
  92.  
  93. return 0;
  94. }
Success #stdin #stdout 2.12s 5272KB
stdin
100000 400000 6
stdout
5169721292