fork download
  1. // Вычисление за O(1) !!!!
  2. // На самом деле это просто "магия шаблонов" и constexpr
  3. // Задача решается на этапе компиляции компилятором
  4. // В рантайме просто выводится результат
  5.  
  6. #include <iostream>
  7. #include <array>
  8. #include <utility>
  9.  
  10. using namespace std;
  11.  
  12. template<size_t N>
  13. struct helper
  14. {
  15. static constexpr size_t other = 2 * (helper<N-1>::other + helper<N-1>::end_2);
  16. static constexpr size_t end_2 = helper<N-1>::other / 2 + helper<N-1>::end_2;
  17. };
  18.  
  19. template<>
  20. struct helper<1>
  21. {
  22. static constexpr size_t other = 2;
  23. static constexpr size_t end_2 = 1;
  24. };
  25.  
  26.  
  27. template <size_t N>
  28. struct task126
  29. {
  30. static constexpr size_t Value = helper<N>::other + helper<N>::end_2;
  31. };
  32.  
  33. void print_values(index_sequence<>)
  34. {
  35. }
  36.  
  37. template <size_t H, size_t ...Rest> constexpr void print_values(index_sequence<H, Rest...>)
  38. {
  39. cout << H << endl;
  40. print_values(index_sequence<Rest...>());
  41. }
  42.  
  43. template <size_t ...N> constexpr void solve(index_sequence<N...>)
  44. {
  45. print_values(index_sequence<task126<N+1>::Value...>());
  46. }
  47.  
  48.  
  49. int main() {
  50. solve(make_index_sequence<30>());
  51. return 0;
  52. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
3
8
21
55
144
377
987
2584
6765
17711
46368
121393
317811
832040
2178309
5702887
14930352
39088169
102334155
267914296
701408733
1836311903
4807526976
12586269025
32951280099
86267571272
225851433717
591286729879
1548008755920
4052739537881