제출 #869456

#제출 시각아이디문제언어결과실행 시간메모리
869456sleepntsheep캥거루 (CEOI16_kangaroo)C++17
6 / 100
1 ms348 KiB
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,tune=native")


using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using ll = long long;
#define N 2005
const ll M = 1000000007;

template <int MOD=M>
struct Modular {
  int value;
  static const int MOD_value = MOD;

  Modular(long long v = 0) {
      if (v > MOD) value = v % MOD; else value = v; if (value < 0) value += MOD;
  }
  Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

  Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
  Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
  Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

  friend Modular mexp(Modular a, long long e) {
    Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

  Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator%(Modular const a, Modular const b) { return a.value % b.value; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
  friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
  friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};

ll n, cs, cf; 
Modular<M> dp[2][N][2][2];

int main()
{
    ShinLena;
    cin >> n >> cs >> cf;
    if (cs > cf) swap(cs, cf);
    dp[0][0][0][0] = 1;
    for (int I = 1, i = 1; i <= n; ++i, I ^= 1)
    {
        memset(dp[I], 0, sizeof *dp);

        if (i == cs)
        {
            for (int j = 1; j <= i; ++j)
            {
                dp[I][j][0][0] = dp[!I][j-1][0][0];
                dp[I][j][1][0] = dp[!I][j][0][0];
                dp[I][j][0][1] = dp[!I][j-1][0][1];
                dp[I][j][1][1] = dp[!I][j][0][1];
            }
        }
        else if (i == cf)
        {
            for (int j = 1; j <= i; ++j)
            {
                dp[I][j][0][0] = dp[!I][j-1][0][0];
                dp[I][j][1][0] = dp[!I][j-1][1][0];
                dp[I][j][0][1] = dp[!I][j][0][0];
                dp[I][j][1][1] = dp[!I][j][1][0];
            }
        }
        else if (i < cs)
        {
            for (int j = 1; j <= i; ++j)
            {
                dp[I][j][0][0] = ((1ll * dp[!I][j-1][0][0] * j % M)
                        + (1ll * dp[!I][j+1][0][0] * j % M)) % M;

                dp[I][j][1][0] = ((1ll * dp[!I][j][0][0])
                        + (1ll * dp[!I][j-1][1][0] * (j-1) % M)
                        + (1ll * dp[!I][j+1][1][0] * j % M)) % M;

                dp[I][j][0][1] = ((1ll * dp[!I][j][0][0])
                        + (1ll * dp[!I][j-1][0][1] * (j-1) % M)
                        + (1ll * dp[!I][j+1][0][1] * j % M)) % M;

                dp[I][j][1][1] = ((1ll * dp[!I][j][1][0])
                        + (1ll * dp[!I][j][0][1])
                        + (1ll * dp[!I][j-1][1][1] * (j-2) % M)
                        + (1ll * dp[!I][j+1][1][1])) % M;
            }
        }

        else if (i < cf)
        {
            for (int j = 1; j <= i; ++j)
            {
                dp[I][j][0][0] = ((1ll * dp[!I][j-1][0][0] * (j-1) % M)
                        + (1ll * dp[!I][j+1][0][0] * j % M)) % M;

                dp[I][j][1][0] = ((0)
                        + (1ll * dp[!I][j-1][1][0] * (j-1) % M)
                        + (1ll * dp[!I][j+1][1][0] * j % M)) % M;

                dp[I][j][0][1] = ((1ll * dp[!I][j][0][0])
                        + (1ll * dp[!I][j-1][0][1] * (j-2) % M)
                        + (1ll * dp[!I][j+1][0][1] * j % M)) % M;

                dp[I][j][1][1] = ((1ll * dp[!I][j][1][0])
                        + (0)
                        + (1ll * dp[!I][j-1][1][1] * (j-2) % M)
                        + (1ll * dp[!I][j+1][1][1])) % M;
            }
        }
        else
        {
            for (int j = 1; j <= i; ++j)
            {
                dp[I][j][0][0] = ((1ll * dp[!I][j-1][0][0] * (j-2) % M)
                        + (1ll * dp[!I][j+1][0][0] * j % M)) % M;

                dp[I][j][1][0] = ((0)
                        + (1ll * dp[!I][j-1][1][0] * (j-2) % M)
                        + (1ll * dp[!I][j+1][1][0] * j % M)) % M;

                dp[I][j][0][1] = ((0)
                        + (1ll * dp[!I][j-1][0][1] * (j-2) % M)
                        + (1ll * dp[!I][j+1][0][1] * j % M)) % M;

                dp[I][j][1][1] = ((0)
                        + (0)
                        + (1ll * dp[!I][j-1][1][1] * (j-2) % M)
                        + (1ll * dp[!I][j+1][1][1])) % M;
            }
        }
    }
    cout << (1ll * dp[n&1][1][0][0] + dp[n&1][1][1][0] + dp[n&1][1][0][1] + dp[n&1][1][1][1]) % M;
    return 0;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...