結果

問題 No.936 Are
ユーザー ei1333333ei1333333
提出日時 2019-11-29 23:26:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 6,902 bytes
コンパイル時間 2,795 ms
コンパイル使用メモリ 233,676 KB
実行使用メモリ 800,168 KB
最終ジャッジ日時 2024-05-01 01:19:38
合計ジャッジ時間 8,725 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,756 KB
testcase_01 MLE -
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;
// const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

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

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< mod >;


int main() {
  int N, K;
  cin >> N >> K;
  int a, b, c, d;
  cin >> a >> b >> c >> d;


  auto dp = make_v< int >(K, 5, 5, 5, 5, 2);
  fill_v(dp, -1);


  auto tap = [](int a, int b, int c, int d) {
    vector< tuple< int, int, int, int > > uku;
    if(a && c) uku.emplace_back(a, b, (a + c) % 5, d);
    if(b && c) uku.emplace_back(a, b, (b + c) % 5, d);
    if(a && d) uku.emplace_back(a, b, c, (a + d) % 5);
    if(b && d) uku.emplace_back(a, b, c, (b + d) % 5);

    for(int i = 0; i < 5; i++) {
      for(int j = 0; j < 5; j++) {
        bool ok = false;
        if(i + j == a + b) ok = true;
        if(i + j == (a + b) % 5) ok = true;
        if(i == a && j == b) ok = false;
        if(i == b && j == a) ok = false;
        if(ok) uku.emplace_back(i, j, c, d);
      }
    }
    return uku;
  };

  vector< tuple< int, int, int, int > > beet;

  auto rec = MFP([&](auto rec, int a, int b, int c, int d, int turn, bool which) -> int {
    if((a + b) == 0) return 0;
    if((c + d) == 0) return 1;
    if(turn >= K) return 0;
    if(~dp[turn][a][b][c][d][which]) return dp[turn][a][b][c][d][which];
    int ret = 0;

    if(which) {

      vector< tuple< int, int, int, int > > uku;
      if(a && c) uku.emplace_back((a + c) % 5, b, c, d);
      if(a && d) uku.emplace_back((a + d) % 5, b, c, d);
      if(b && c) uku.emplace_back(a, (b + c) % 5, c, d);
      if(b && d) uku.emplace_back(a, (b + d) % 5, c, d);

      for(int i = 0; i < 5; i++) {
        for(int j = 0; j < 5; j++) {
          bool ok = false;
          if(i + j == c + d) ok = true;
          if(i + j == (c + d) % 5) ok = true;
          if(i == c && j == d) ok = false;
          if(i == d && j == c) ok = false;
          if(ok) uku.emplace_back(a, b, i, j);
        }
      }

      for(auto &p : uku) {
        int r, t, y, u;
        tie(r, t, y, u) = p;
        if((y + u) == 0) continue;
        if((r + t) == 0) return 0;
      }

      bool exist = false;
      for(auto &p : uku) {
        int r, t, y, u;
        tie(r, t, y, u) = p;
        if((y + u) == 0) continue;


        bool ok = true;
        {
          auto get = tap(r, t, y, u);
          for(auto &q : get) {
            int aa, bb, cc, dd;
            tie(aa, bb, cc, dd) = q;
            if((cc + dd) == 0) ok = false;
          }
        }
        if(!ok) continue;


        exist = true;
        beet.emplace_back(p);
        ret += rec(r, t, y, u, turn + 1, which ^ 1);
        beet.pop_back();
        ret %= mod;
      }

      if(!exist) {
        for(auto &p : uku) {
          int r, t, y, u;
          tie(r, t, y, u) = p;
          if((y + u) == 0) continue;
          beet.emplace_back(p);
          ret += rec(r, t, y, u, turn + 1, which ^ 1);
          beet.pop_back();
          ret %= mod;
          exist = true;
        }
      }

    } else {
      auto get = tap(a, b, c, d);
      for(auto &p : get) {
        int r, t, y, u;
        tie(r, t, y, u) = p;
        if((r + t) == 0) continue;
        beet.emplace_back(p);
        ret += rec(r, t, y, u, turn + 1, which ^ 1);
        beet.pop_back();
        ret %= mod;
      }
    }
    return dp[turn][a][b][c][d][which] = ret;
  });
  cout << rec(a, b, c, d, 0, N) << endl;
}
0