:heavy_check_mark: test/aoj_1180.test.cpp

Back to top page

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1180&lang=jp"

#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <string>

#include "algorithm/tortoise_and_hare.cpp"

class generator {
  int M_a, M_l;

public:
  generator() = default;
  generator(int a, int l): M_a(a), M_l(l) {}

  int peek() const { return M_a; }
  void pop() {
    char buf[8];
    snprintf(buf, sizeof buf, "%0*d", M_l, M_a);
    std::string s = buf;
    std::sort(s.begin(), s.end());
    std::string t(s.rbegin(), s.rend());
    M_a = std::stoi(t) - std::stoi(s);
  }
};

int testcase_ends() {
  int a0, l;
  scanf("%d %d", &a0, &l);
  if (a0 == 0 && l == 0) return 1;

  generator g(a0, l);
  auto [mu, lambda] = detect_cycle(g);
  for (int i = 0; i < mu+lambda; ++i) g.pop();

  printf("%jd %d %jd\n", mu, g.peek(), lambda);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

#line 1 "test/aoj_1180.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1180&lang=jp"

#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <string>

#line 1 "algorithm/tortoise_and_hare.cpp"



/**
 * @brief 周期検出
 * @author えびちゃん
 */

#line 10 "algorithm/tortoise_and_hare.cpp"
#include <utility>

template <typename Generator>
std::pair<intmax_t, intmax_t> detect_cycle(Generator g) {
  Generator tortoise = g;
  Generator hare = g;
  do {
    tortoise.pop();
    hare.pop(), hare.pop();
  } while (tortoise.peek() != hare.peek());

  tortoise = g;
  intmax_t mu = 0;
  while (tortoise.peek() != hare.peek()) {
    ++mu;
    tortoise.pop();
    hare.pop();
  }

  intmax_t lambda = 0;
  hare = tortoise;
  do {
    ++lambda;
    hare.pop();
  } while (tortoise.peek() != hare.peek());

  return {mu, lambda};
}


#line 9 "test/aoj_1180.test.cpp"

class generator {
  int M_a, M_l;

public:
  generator() = default;
  generator(int a, int l): M_a(a), M_l(l) {}

  int peek() const { return M_a; }
  void pop() {
    char buf[8];
    snprintf(buf, sizeof buf, "%0*d", M_l, M_a);
    std::string s = buf;
    std::sort(s.begin(), s.end());
    std::string t(s.rbegin(), s.rend());
    M_a = std::stoi(t) - std::stoi(s);
  }
};

int testcase_ends() {
  int a0, l;
  scanf("%d %d", &a0, &l);
  if (a0 == 0 && l == 0) return 1;

  generator g(a0, l);
  auto [mu, lambda] = detect_cycle(g);
  for (int i = 0; i < mu+lambda; ++i) g.pop();

  printf("%jd %d %jd\n", mu, g.peek(), lambda);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

Back to top page