【Cf1204E】E.ナターシャ、サーシャとプレフィックス合計(組み合わせ数学+ DP)

ポータル

問題の意味:
\(N- \)\(1 \)、\ (M \)\( - 1、N-、M \のLeq 2000 \) これらの数字のシーケンスを形成するために、定義された\ [\ DisplayStyle F()= MAX(0 、MAX_ {1 \当量I \当量のN + M} \ sum_ {J = 1} ^ ia_j)\]

その\(F()\)シーケンス\(A \)すべてのプレフィックスと最大の。
すべての可能なシーケンスの最後の問い合わせ\(F()\)どのくらいの合計。

アイデア:

  • 私たちは、リクエストがプレフィックスとあることに注意してください。これによれば、現在のシーケンスと、見つけることができる\(F(A)= X \)は、次いでトップを追加(1 \)\、次に\(F(A「)= 1 + X \) ;プラスA \( - 1 \) 次いで\(F(「)= MAX(0 ,. 1-X)\)
  • 上記は、我々はそれに応じてすることができ、非常に重要な観察である(DP \)\
  • 定義(dp_ {X、Y} \ \) を表し、今\(X \) A \(1 \)を、\ (Y \)\( - 1 \)の答え。その後、転送タイプがあります:

\ [Dp_ {X、Y} = dp_ {X-1、Y} + {X-1 + Y \ Yを選択} + dp_ {X、Y-1} - ({X + Y-1 \ Xを選択} - F_ {X、Y-1} )\] \(F_ {X、Y} \) が存在することを意味する\(Xは\)\(1 \)、\ (Y \)\( - 1 \)場合、\ (F()= 0 \)は、シーケンス番号です。これは、接頭辞のいずれかと等価である(1 \)\数を超えていない\( - 1 \)は、直接式番号カトレアに従って計算することができます。コードはよく書かれている、この問題を解決する鍵は、「接頭辞と」コンセプト、従来把握することです\(DP \)の違いを、私たちは正面から挿入することを選択し、この時間です。コードは以下の通りであります:


/*
 * Author:  heyuhhh
 * Created Time:  2020/3/14 15:52:51
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 4000 + 5, MOD = 998244853;

int C[N][N];
void init() {
    C[0][0] = 1;
    for(int i = 1; i < N; i++) {
        C[i][0] = 1;
        for(int j = 1; j <= i; j++) {
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;   
        }
    }
}

int n, m;
int f[N][N], g[N][N];

void run() {
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        f[0][i] = 1;
        for(int j = i; j <= m; j++) {
            f[i][j] = (C[i + j][i] - C[i + j][i - 1] + MOD) % MOD;
        }   
    }
    for(int i = 1; i <= n; i++) g[i][0] = i;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            g[i][j] = (g[i - 1][j] + C[i + j - 1][j]) % MOD + ((g[i][j - 1] - (C[i + j - 1][j - 1] - f[i][j - 1])) % MOD + MOD) % MOD;
            g[i][j] %= MOD;
        }   
    }
    cout << g[n][m] << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    init();
    run();
    return 0;
}

おすすめ

転載: www.cnblogs.com/heyuhhh/p/12507040.html