codeforces587B Duff in Beach

题意就是两个数组A,B其中B[i] = A[i%n],B有l个元素,A有n个元素。

从B中选取x个元素,相邻元素在B中的下标满足Ij/n + 1 == I[j+1]/n。

可以用dp[i][j]表示第i个数选的是j,那么dp[i][j] = ∑dp[i-1][t] && t <= j。

数字比较大,得先离散化一下,且要用滚动数组。。。。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2016
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
LL dp[2][1000010];
LL a[1000010],b[1000010];
int n, k;
LL l;
int main()
{    
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    cin >> n >> l >> k;
    for (int i = 1;i <= n;++i) {
        // cin >> a[i];
        scanf("%I64d",&a[i]);
        b[i] = a[i];
    }
    sort(b + 1,b + 1 + n);
    int cnt = unique(b + 1,b + 1 + n) - b;
    for (int i = 1;i <= n;++i) {
        a[i] = lower_bound(b + 1,b + cnt, a[i]) - b;
        // cout << a[i] << ' ';
    }
    // cout << endl;
    int now = 1, la = 0;
    dp[now][0] = 1;
    LL ans = 0;
    LL seg = l / n;
    for (int i = 1;i <= k && i <= seg + 1;++i) {
        swap(la, now);
        for (int j = 0;j <= cnt;++j)
            dp[now][j] = 0;
        for (int j = 1;j <= cnt;++j)
            dp[la][j] = (dp[la][j-1] + dp[la][j]) % MOD;
        for (int j = 1;j <= n;++j)
            dp[now][a[j]] = (dp[now][a[j]] + dp[la][a[j]]) % MOD;
        for (int j = 1;j <= cnt;++j)
            ans = (ans + (seg - i + 1)%MOD * dp[now][j]%MOD) % MOD;
        for (int j = 1;j <= l - seg*n;++j)
            ans = (ans + dp[la][a[j]]) % MOD;
    }
    cout << ans << endl;
    return 0;
}


你可能感兴趣的:(dp)