Submission #891961

#TimeUsernameProblemLanguageResultExecution timeMemory
891961josanneo22Kangaroo (CEOI16_kangaroo)C++17
100 / 100
6 ms476 KiB
#include<bits/stdc++.h> using namespace std; using i64 = long long; char buf[1 << 23], * p1 = buf, * p2 = buf; #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) template<typename T> inline void re(T& x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * (1 << 1) + x * (1 << 3) + (ch - 48); ch = getchar(); } x *= f; } template<typename x, typename... y>void re(x& a, y&... b) { re(a); re(b...); } template<typename T> inline void ps(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) ps(x / 10); putchar(x % 10 + '0'); } template<typename x, typename... y>void ps(x& a, y&... b) { ps(a); putchar(' '); ps(b...); } #define sp putchar(' ') #define nl putchar('\n') template<class T> constexpr T power(T a, i64 b) { T res = 1; for (; b; b /= 2, a *= a) { if (b % 2) { res *= a; } } return res; } constexpr i64 mul(i64 a, i64 b, i64 p) { i64 res = a * b - i64(1.L * a * b / p) * p; res %= p; if (res < 0) { res += p; } return res; } template<i64 P> struct MLong { i64 x; constexpr MLong() : x{} {} constexpr MLong(i64 x) : x{ norm(x % getMod()) } {} static i64 Mod; constexpr static i64 getMod() { if (P > 0) { return P; } else { return Mod; } } constexpr static void setMod(i64 Mod_) { Mod = Mod_; } constexpr i64 norm(i64 x) const { if (x < 0) { x += getMod(); } if (x >= getMod()) { x -= getMod(); } return x; } constexpr i64 val() const { return x; } explicit constexpr operator i64() const { return x; } constexpr MLong operator-() const { MLong res; res.x = norm(getMod() - x); return res; } constexpr MLong inv() const { assert(x != 0); return power(*this, getMod() - 2); } constexpr MLong& operator*=(MLong rhs)& { x = mul(x, rhs.x, getMod()); return *this; } constexpr MLong& operator+=(MLong rhs)& { x = norm(x + rhs.x); return *this; } constexpr MLong& operator-=(MLong rhs)& { x = norm(x - rhs.x); return *this; } constexpr MLong& operator/=(MLong rhs)& { return *this *= rhs.inv(); } friend constexpr MLong operator*(MLong lhs, MLong rhs) { MLong res = lhs; res *= rhs; return res; } friend constexpr MLong operator+(MLong lhs, MLong rhs) { MLong res = lhs; res += rhs; return res; } friend constexpr MLong operator-(MLong lhs, MLong rhs) { MLong res = lhs; res -= rhs; return res; } friend constexpr MLong operator/(MLong lhs, MLong rhs) { MLong res = lhs; res /= rhs; return res; } friend constexpr std::istream& operator>>(std::istream& is, MLong& a) { i64 v; is >> v; a = MLong(v); return is; } friend constexpr std::ostream& operator<<(std::ostream& os, const MLong& a) { return os << a.val(); } friend constexpr bool operator==(MLong lhs, MLong rhs) { return lhs.val() == rhs.val(); } friend constexpr bool operator!=(MLong lhs, MLong rhs) { return lhs.val() != rhs.val(); } }; template<> i64 MLong<0LL>::Mod = i64(1E18) + 9; template<int P> struct MInt { int x; constexpr MInt() : x{} {} constexpr MInt(i64 x) : x{ norm(x % getMod()) } {} static int Mod; constexpr static int getMod() { if (P > 0) { return P; } else { return Mod; } } constexpr static void setMod(int Mod_) { Mod = Mod_; } constexpr int norm(int x) const { if (x < 0) { x += getMod(); } if (x >= getMod()) { x -= getMod(); } return x; } constexpr int val() const { return x; } explicit constexpr operator int() const { return x; } constexpr MInt operator-() const { MInt res; res.x = norm(getMod() - x); return res; } constexpr MInt inv() const { assert(x != 0); return power(*this, getMod() - 2); } constexpr MInt& operator*=(MInt rhs)& { x = 1LL * x * rhs.x % getMod(); return *this; } constexpr MInt& operator+=(MInt rhs)& { x = norm(x + rhs.x); return *this; } constexpr MInt& operator-=(MInt rhs)& { x = norm(x - rhs.x); return *this; } constexpr MInt& operator/=(MInt rhs)& { return *this *= rhs.inv(); } friend constexpr MInt operator*(MInt lhs, MInt rhs) { MInt res = lhs; res *= rhs; return res; } friend constexpr MInt operator+(MInt lhs, MInt rhs) { MInt res = lhs; res += rhs; return res; } friend constexpr MInt operator-(MInt lhs, MInt rhs) { MInt res = lhs; res -= rhs; return res; } friend constexpr MInt operator/(MInt lhs, MInt rhs) { MInt res = lhs; res /= rhs; return res; } friend constexpr std::istream& operator>>(std::istream& is, MInt& a) { i64 v; is >> v; a = MInt(v); return is; } friend constexpr std::ostream& operator<<(std::ostream& os, const MInt& a) { return os << a.val(); } friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); } friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); } }; template<> int MInt<0>::Mod = 1E9 + 7; template<int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv(); constexpr int P = 1E9 + 7; using Z = MInt<P>; struct Comb { int n; std::vector<Z> _fac; std::vector<Z> _invfac; std::vector<Z> _inv; Comb() : n{ 0 }, _fac{ 1 }, _invfac{ 1 }, _inv{ 0 } {} Comb(int n) : Comb() { init(n); } void init(int m) { m = std::min(m, Z::getMod() - 1); if (m <= n) return; _fac.resize(m + 1); _invfac.resize(m + 1); _inv.resize(m + 1); for (int i = n + 1; i <= m; i++) { _fac[i] = _fac[i - 1] * i; } _invfac[m] = _fac[m].inv(); for (int i = m; i > n; i--) { _invfac[i - 1] = _invfac[i] * i; _inv[i] = _invfac[i] * _fac[i - 1]; } n = m; } Z fac(int m) { if (m > n) init(2 * m); return _fac[m]; } Z invfac(int m) { if (m > n) init(2 * m); return _invfac[m]; } Z inv(int m) { if (m > n) init(2 * m); return _inv[m]; } Z C(int n, int m) { if (n < m || m < 0) return 0; return fac(n) * invfac(m) * invfac(n - m); } Z find(int n, int m, int k) { /* x1+x2+x3+...+xm = k and 0<=x1,x2,x3...,xm<n x1+x2+x3+...=k+m and x1,x2,x3...xm>=1 f(i) = no of cases such that >=i numbers are >n f(i) = m C i * (m+k-i*n-1) C (m-1) ans= f(i) * (-1)^i for i->[0,m] */ Z ans = 0; int ops = 1; for (int i = 0; i <= m; i++) { Z t = C(m, i) * C(m + k - i * n - 1, m - 1); if (ops == -1) ans -= t; else ans += t; ops *= -1; } return ans; } } comb; //mod = 1E9+7 const int _N = 2010; Z dp[2][_N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int N, A, B; re(N, A, B); dp[1][1] = 1; for (int i = 2; i <= N; ++i) { int nw = i & 1, pv = (i - 1) & 1; for (int j = 1; j <= min(i, (N + 1) >> 1); ++j) { dp[nw][j] = 0; if (i == A || i == B) { dp[nw][j] += dp[pv][j] + dp[pv][j - 1]; continue; } dp[nw][j] += dp[pv][j - 1] * (j - (i > A) - (i > B)) + dp[pv][j + 1] * j;//不能放头,不能放尾 } } i64 v = dp[N & 1][1].val(); ps(v); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...