fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. class Generator: public std::iterator<std::forward_iterator_tag, size_t> {
  8. public:
  9. Generator(size_t init): result(init) {
  10. while (init > 0) {
  11. array.push_back(init % 10);
  12. init /= 10;
  13. }
  14. std::reverse(array.begin(), array.end());
  15. }
  16.  
  17. Generator& operator++() {
  18. this->increment();
  19. this->convert();
  20. return *this;
  21. }
  22.  
  23. Generator& operator++(int) { Generator tmp(*this); ++tmp; return *this; }
  24.  
  25. size_t& operator*() { return result; }
  26. size_t const& operator*() const { return result; }
  27.  
  28. size_t* operator->() { return &result; }
  29. size_t const* operator->() const { return &result; }
  30.  
  31. bool operator==(Generator const& other) {
  32. return result == other.result;
  33. }
  34.  
  35. bool operator!=(Generator const& other) {
  36. return result != other.result;
  37. }
  38.  
  39. private:
  40. void convert() {
  41. result = 0;
  42. for (auto e: array) { result *= 10; result += e; }
  43. }
  44.  
  45. void increment() {
  46. typedef std::vector<uint8_t>::const_reverse_iterator It;
  47.  
  48. std::vector<uint8_t> r;
  49. bool carry = true;
  50.  
  51. for (It it = array.rbegin(), end = array.rend(); it != end; ++it) {
  52. uint8_t e = *it;
  53. if (carry) { e += 1; carry = false; }
  54. if (e > 6) { e = 4; carry = true; }
  55. r.push_back(e);
  56. }
  57.  
  58. if (carry) { r.push_back(4); }
  59.  
  60. array.assign(r.rbegin(), r.rend());
  61. }
  62.  
  63. size_t result;
  64.  
  65. std::vector<uint8_t> array;
  66. }; // class Generator
  67.  
  68. int main() {
  69. size_t i = 0;
  70. for (Generator it(4), end(666555); it != end; ++it) {
  71. i += *it;
  72. }
  73. std::cout << i << "\n";
  74. return 0;
  75. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
409632209