:heavy_check_mark: test/aoj_ALDS1_10_A.test.cpp

Back to top page

Depends on

Code

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

#include <cstdint>
#include <cstdio>
#include <vector>

#include "utility/make/fix_point.cpp"

int main() {
  int n;
  scanf("%d", &n);

  std::vector<intmax_t> memo(n+1);
  memo[0] = memo[1] = 1;
  make_fix_point([&](auto f, int i) {
      intmax_t& res = memo[i];
      if (res > 0) return res;
      res = f(i-1) + f(i-2);
      return res;
  })(n);
  printf("%jd\n", memo[n]);
}

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

#include <cstdint>
#include <cstdio>
#include <vector>

#line 1 "utility/make/fix_point.cpp"
/**
 * @brief ラムダ式の再帰
 * @author えびちゃん
 */

#ifndef H_make_fix_point
#define H_make_fix_point

#include <utility>

template <typename Fn>
class fix_point: Fn {
public:
  explicit constexpr fix_point(Fn&& f) noexcept: Fn(std::forward<Fn>(f)) {}

  template <typename... Args>
  constexpr decltype(auto) operator ()(Args&&... args) const {
    return Fn::operator ()(*this, std::forward<Args>(args)...);
  }
};

template <typename Fn>
static inline constexpr decltype(auto) make_fix_point(Fn&& f) noexcept {
  return fix_point<Fn>{std::forward<Fn>(f)};
}

#endif  /* !defined(H_make_fix_point) */
#line 8 "test/aoj_ALDS1_10_A.test.cpp"

int main() {
  int n;
  scanf("%d", &n);

  std::vector<intmax_t> memo(n+1);
  memo[0] = memo[1] = 1;
  make_fix_point([&](auto f, int i) {
      intmax_t& res = memo[i];
      if (res > 0) return res;
      res = f(i-1) + f(i-2);
      return res;
  })(n);
  printf("%jd\n", memo[n]);
}

Back to top page