åºéåå²ãã¦ãã DP ãæ®éã«ãã㨠ã«ãªã (ãªã¬ã³ã¸ã®åºè·ããã)ããããç´¯ç©åãç¨ãã¦é«éåããã
åé¡æ¦è¦ (æ訳)
åã®æ£ã®æ´æ° ãä¸ãããããããããããã¤ãã®é£ç¶ããåºéã«åå²ãã¦ããããã ãã©ã®åºéã«ã¤ãã¦ããç·åã 以ä¸ã§ãªããã°ãªããªãã
ãã®ãããªåå²æ¹æ³ã®åæ°ã 1234567 ã§å²ã£ããã¾ããæ±ããã
å¶ç´
èãããã¨
åºéãåå²ããã¿ã¤ãã®åé¡ã¯ãDP ã§è§£ãããã¨ãå¤ãããããªæãã® DP ãããã®ã ã
- dp[ i ] := æåã® i åã®æ´æ°ãããã¤ãã®åºéã«åå²ããæ¹æ³ã®åæ°
ãã®ã¨ãã次ã®ãã㪠DP é·ç§»ãä½ãã
dp[ i ] += dp[ j ]ã(åºé [j, i) ã®ç·åã P 以ä¸ã§ãããã㪠j ã«å¯¾ãã¦)
ãã® DP ã®è¨ç®é㯠ã¨ãªããããã¾ã§ã¯ããªã¬ã³ã¸ã®åºè·ããªãããä¼¼ã DP ã ããããé«éåãããã
DP é«éå
DP é·ç§»ãæ¹ãã¦ã¡ããã¨æ¸ãã¨ããããªãµãã«ãªãã
- åºé [j, i) ã®ç·åã 以ä¸ã¨ãªããããªæå°ã® j ã j = l(i) ã¨ããã¨ã
- dp[ i ] = dp[ l(i) ] + dp[ l(i) + 1 ] + ... + dp[ i - 1 ]
ããã§é å dp ã®ç´¯ç©å sdp ãå°å ¥ããã¨ã
- dp[ i ] = sdp[ i ] - sdp[ l(i) ]
ã¨ç°¡æ½ã«æ¸ããããã®ããã«ããã°è¨ç®é㯠ã«ä¸ããã
ã³ã¼ã
ããã§ã¯ modint ãç¨ãããã¨ã«ãããã¾ããl(i) ãæ±ããé¨åã¯ããããã¨ãæ³ããç¨ããã
#include <bits/stdc++.h> using namespace std; // modint template<int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() const { return MOD; } constexpr Fp operator - () const noexcept { return val ? MOD - val : 0; } constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; } constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; } constexpr Fp& operator += (const Fp& r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp& operator -= (const Fp& r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp& operator *= (const Fp& r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp& operator /= (const Fp& r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b, swap(a, b); u -= t * v, swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator == (const Fp& r) const noexcept { return this->val == r.val; } constexpr bool operator != (const Fp& r) const noexcept { return this->val != r.val; } friend constexpr istream& operator >> (istream& is, Fp<MOD>& x) noexcept { is >> x.val; x.val %= MOD; if (x.val < 0) x.val += MOD; return is; } friend constexpr ostream& operator << (ostream& os, const Fp<MOD>& x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD>& r, long long n) noexcept { if (n == 0) return 1; if (n < 0) return modpow(modinv(r), -n); auto t = modpow(r, n / 2); t = t * t; if (n & 1) t = t * r; return t; } friend constexpr Fp<MOD> modinv(const Fp<MOD>& r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b, swap(a, b); u -= t * v, swap(u, v); } return Fp<MOD>(u); } }; const int MOD = 1234567; using mint = Fp<MOD>; int main() { long long N, P; cin >> N >> P; vector<long long> H(N); for (int i = 0; i < N; ++i) cin >> H[i]; // ãããã¨ãæ³ vector<int> l(N+1, N+1); int left = 0; long long sum = 0; for (int right = 0; right <= N; ++right) { while (left < right && sum > P) sum -= H[left++]; l[right] = left; if (right < N) sum += H[right]; } // ç´¯ç©åãç¨ãã DP vector<mint> dp(N+1, 0), sdp(N+2, 0); dp[0] = sdp[1] = 1; for (int n = 1; n <= N; ++n) { dp[n] = sdp[n] - sdp[l[n]]; sdp[n+1] = sdp[n] + dp[n]; } cout << dp[N] << endl; }