hdu 5459 Jesus Is Here(dp)

题目链接:hdu 5459 Jesus Is Here

解题思路

其实每个c都对应这一个cff,并且串i由串i-1和i-2生成的,所以答案肯定也可以通过i-1,i-2获得。

对于每个串,维护四个值dis(每个c的位置和),len(串的长度),cnt(多少个c),sum(答案)
具体转移公式见代码。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 201314;
const int mod = 530600414;

ll dis[maxn+5], len[maxn+5], cnt[maxn+5], sum[maxn+5];

void init () {
    len[1] = cnt[1] = 1;
    dis[1] = sum[1] = 0;

    len[2] = 2, cnt[2] = 0;
    dis[2] = sum[2] = 0;

    for (int i = 3; i <= maxn; i++) {
        dis[i] = (dis[i-2] + dis[i-1] + cnt[i-1] * len[i-2] % mod) % mod;
        len[i] = (len[i-1] + len[i-2]) % mod;
        cnt[i] = (cnt[i-1] + cnt[i-2]) % mod;
        ll add = cnt[i-2] * (dis[i-1] + cnt[i-1] * len[i-2] % mod) % mod;
        ll del = cnt[i-1] * dis[i-2] % mod;
        sum[i] = ((sum[i-1] + sum[i-2] + add - del) % mod + mod) % mod;
    }
}

int main () {
    init();

    int cas, n;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++) {
        scanf("%d", &n);
        printf("Case #%d: %lld\n", kcas, sum[n]);
    }
    return 0;
}

你可能感兴趣的:(hdu 5459 Jesus Is Here(dp))