2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

Removal

链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网

题目描述

Bobo has a sequence of integers s1, s2, ..., sn where 1 ≤ si ≤ k.
Find out the number of distinct sequences modulo (109+7) after removing exactly m elements.

输入描述:

The input consists of several test cases and is terminated by end-of-file.The first line of each test case contains three integers n, m and k.The second line contains n integers s1, s2, ..., sn.

输出描述:

For each test case, print an integer which denotes the result.

示例1

输入

[复制](javascript:void(0)😉

3 2 2
1 2 1
4 2 2
1 2 1 2

输出

[复制](javascript:void(0)😉

2
4

备注:

* 1 ≤ n ≤ 105* 1 ≤ m ≤ min{n - 1, 10}* 1 ≤ k ≤ 10* 1 ≤ si ≤ k* The sum of n does not exceed 106.

思路:

定义\(dp[i][j]\) 代表长度为i,以j数字结尾的子序列个数。

定义\(sum[i]\) 代表长度为i的子序列个数。

初始化:

sum[i]=1;

状态转移方程:

repd(i, 1, n) {
            for (int j = i; j >= i - m && j >= 1; j--) {
                sum[j] = ((sum[j] + sum[j - 1]) % mod - dp[j][a[i]] + mod) % mod;
                dp[j][a[i]] = sum[j - 1];
            }
        }

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int *p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
const ll mod = 1e9 + 7;
ll dp[maxn][12];
ll sum[maxn];
int a[maxn];
int n, m, k;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    sum[0] = 1ll;
    while (~scanf("%d %d %d", &n, &m, &k)) {
        repd(i, 1, n) {
            scanf("%d", &a[i]);
        }
        // init
        repd(i, 1, n) {
            repd(j, 1, k) {
                dp[i][j] = 0ll;
            }
            sum[i] = 0ll;
        }

        repd(i, 1, n) {
            for (int j = i; j >= i - m && j >= 1; j--) {
                sum[j] = ((sum[j] + sum[j - 1]) % mod - dp[j][a[i]] + mod) % mod;
                dp[j][a[i]] = sum[j - 1];
            }
        }
        printf("%lld\n", sum[n - m]);
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}




posted @ 2019-10-29 09:49  茄子Min  阅读(338)  评论(0编辑  收藏  举报