結果

問題 No.936 Are
ユーザー noshi91noshi91
提出日時 2019-11-29 23:37:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 3,000 ms
コード長 8,739 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 93,240 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-13 07:37:14
合計ジャッジ時間 3,117 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 92 ms
4,380 KB
testcase_02 AC 92 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 27 ms
4,376 KB
testcase_14 AC 76 ms
4,380 KB
testcase_15 AC 31 ms
4,380 KB
testcase_16 AC 56 ms
4,376 KB
testcase_17 AC 49 ms
4,380 KB
testcase_18 AC 22 ms
4,380 KB
testcase_19 AC 87 ms
4,376 KB
testcase_20 AC 89 ms
4,376 KB
testcase_21 AC 80 ms
4,376 KB
testcase_22 AC 49 ms
4,376 KB
testcase_23 AC 93 ms
4,376 KB
testcase_24 AC 93 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define NDEBUG
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

namespace n91 {

using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  if (a < b) {
    return b - a;
  } else {
    return a - b;
  }
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a) {
    a = b;
  }
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b) {
    a = b;
  }
}
template <class F> class fix_point : private F {
public:
  explicit constexpr fix_point(F &&f) : F(std::forward<F>(f)) {}

  template <class... Args>
  constexpr decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
template <class F> constexpr decltype(auto) make_fix(F &&f) {
  return fix_point<F>(std::forward<F>(f));
}
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}

} // namespace n91
#include <cstdint>

namespace n91 {

constexpr std::uint_fast64_t totient(std::uint_fast64_t x) noexcept {
  using u64 = std::uint_fast64_t;
  u64 ret = x;
  for (u64 i = static_cast<u64>(2); i * i <= x; ++i) {
    if (x % i == static_cast<u64>(0)) {
      ret -= ret / i;
      x /= i;
      while (x % i == static_cast<u64>(0)) {
        x /= i;
      }
    }
  }
  if (x != static_cast<u64>(1)) {
    ret -= ret / x;
  }
  return ret;
}

template <std::uint_fast64_t Modulus,
          std::uint_fast64_t InverseExp =
              totient(Modulus) - static_cast<std::uint_fast64_t>(1)>
class modint {
  using u64 = std::uint_fast64_t;

  static_assert(Modulus < static_cast<u64>(1) << static_cast<u64>(32),
                "Modulus must be less than 2**32");

  u64 a;

  constexpr modint &negate() noexcept {
    if (a != static_cast<u64>(0)) {
      a = Modulus - a;
    }
    return *this;
  }

public:
  constexpr modint(const u64 x = static_cast<u64>(0)) noexcept
      : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+() const noexcept { return modint(*this); }
  constexpr modint operator-() const noexcept { return modint(*this).negate(); }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = InverseExp;
    while (exp) {
      if (exp % static_cast<u64>(2) != static_cast<u64>(0)) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= static_cast<u64>(2);
    }
    return *this;
  }
  constexpr bool operator==(const modint rhs) const noexcept {
    return a == rhs.a;
  }
  constexpr bool operator!=(const modint rhs) const noexcept {
    return a != rhs.a;
  }
};

template <class T, std::uint_fast64_t v> class modint_constant {
public:
  static constexpr T value = static_cast<T>(v);

  using value_type = T;
};
template <class T, std::uint_fast64_t v>
constexpr T modint_constant<T, v>::value;

} // namespace n91

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>

namespace n91 {

void main_() {
  /*
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //*/
  using mint = modint<1000000007>;
  const auto get = [](const usize l1, const usize r1, const usize l2,
                      const usize r2) {
    return l1 * 125 + r1 * 25 + l2 * 5 + r2;
  };
  const auto rev = [](const usize i) { return i / 25 + i % 25 * 25; };
  const usize size = 5 * 5 * 5 * 5;

  std::vector<std::vector<usize>> g(size);
  auto rg = g;
  for (const usize l1 : rep(0, 5)) {
    for (const usize r1 : rep(0, 5)) {
      for (const usize l2 : rep(0, 5)) {
        for (const usize r2 : rep(0, 5)) {
          if ((l1 == 0 && r1 == 0) || (l2 == 0 && r2 == 0)) {
            continue;
          }
          auto &v = g[get(l1, r1, l2, r2)];
          if (l2 != 0) {
            if (l1 != 0)
              v.push_back(get(l1, r1, (l2 + l1) % 5, r2));
            if (r1 != 0)
              v.push_back(get(l1, r1, (l2 + r1) % 5, r2));
          }
          if (r2 != 0) {
            if (l1 != 0)
              v.push_back(get(l1, r1, l2, (r2 + l1) % 5));
            if (r1 != 0)
              v.push_back(get(l1, r1, l2, (r2 + r1) % 5));
          }
          for (const usize l3 : rep(0, 5)) {
            for (const usize r3 : rep(0, 5)) {
              if ((l3 != 0 || r3 != 0) &&
                  ((l1 + r1) == (l3 + r3) || (l1 + r1) % 5 == (l3 + r3)) &&
                  std::set<usize>({l1, r1}) != std::set<usize>({l3, r3})) {
                v.push_back(get(l3, r3, l2, r2));
              }
            }
          }
        }
      }
    }
  }

  std::vector<int> lotwin(size, 0);

  for (const usize i : rep(0, size)) {
    for (const auto e : g[i]) {
      if (e % 25 == 0) {
        lotwin[i] = 1;
        break;
      }
    }
  }

  for (const usize i : rep(0, size)) {
    bool exist_win = false;
    bool exist_even = false;
    for (const auto e_ : g[rev(i)]) {
      const usize e = rev(e_);
      if (e / 25 == 0) {
        exist_win = true;
      }
      if (lotwin[e] == 0) {
        exist_even = true;
      }
    }
    if (exist_win) {
      for (const auto e_ : g[rev(i)]) {
        const usize e = rev(e_);
        if (e / 25 == 0) {
          rg[i].push_back(e);
        }
      }
    } else if (exist_even) {
      for (const auto e_ : g[rev(i)]) {
        const usize e = rev(e_);
        if (lotwin[e] == 0) {
          rg[i].push_back(e);
        }
      }
    } else {
      for (const auto e_ : g[rev(i)]) {
        const usize e = rev(e_);
        rg[i].push_back(e);
      }
    }
  }

  bool is_lot = scan<u32>() == 0;
  const usize k = scan<usize>();
  std::vector<mint> dp(size, 0);
  {
    const usize l1 = scan<usize>();
    const usize r1 = scan<usize>();
    const usize l2 = scan<usize>();
    const usize r2 = scan<usize>();
    dp[get(l1, r1, l2, r2)] = 1;
  }
  mint ans = 0;
  for (const usize loop : rep(0, k)) {
    std::vector<mint> nx(size, 0);
    if (is_lot) {
      for (const usize i : rep(0, size)) {
        for (const auto e : g[i]) {
          nx[e] += dp[i];
        }
      }
      for (const usize i : rep(0, 25)) {
        ans += nx[i * 25];
      }
    } else {
      for (const usize i : rep(0, size)) {
        for (const auto e : rg[i]) {
          nx[e] += dp[i];
        }
      }
    }
    is_lot = !is_lot;
    dp = std::move(nx);
  }
  std::cout << ans.value() << std::endl;
}

} // namespace n91

int main() {
  n91::main_();
  return 0;
}
0