HDU 5459 Jesus Is Here (递推)

有点麻烦的递推,看的时候请耐心,递推的时候不要有嵌套,向小的问题方向分解,然后注意边界。

字符串的递推式为

定义f为Si中的总长度

首先可以得到

然后考虑Si-2和Si-1之间的组合

为了得到小的问题,进行拆分

为了以后表示的方便和逻辑上的清晰,把Si~Si之间的组合总长度定义出来

因为这里的si-2和si-2的中间还有一段Si-3

所以其组合总长度就可以表示为

Ci表示Si中cff出现的次数,Li表示Si的长度

定义一个函数ccl

最后还剩下一个部分Si-2和Si-3

定义

至此,总方案已经可以表示出来

然后再从顶往下的逐步细化

考虑g(i)的计算

这里面也有个递推,首先可以得到

为了方便再定义一个函数

显然这个容易计算得多

剩下的组合也可以表示出来了

最后一部分f12(i)

#include<bits/stdc++.h>
using namespace std;

#define PB push_back
#define MP make_pair
#define fi first
#define se second

typedef long long ll;

const int N = 201314+5;
ll f[N], c[N], l[N], g[N];
const int Mod =  530600414;

inline ll f21(int i) { return f[i]-f[i-2]-f[i-1]; }
inline ll ccl(int i,int j,int k) { return c[i]*c[j]%Mod*l[k]; }
inline ll f12(int i) { return g[i-2]+f21(i-1)+ccl(i-3,i-2,i-2); }//用到i-3

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    l[1] = 1; l[2] = 2;
    l[3] = 3; l[4] = 5;
    c[3] = 1; c[4] = 1;
    g[3] = 3; g[4] = 5;
    for(int i = 5; i <= 201314; i++){
        l[i] = (l[i-1]+l[i-2])%Mod;
        c[i] = (c[i-1]+c[i-2])%Mod;
        f[i] = (f[i-1]+f[i-2]+g[i-2]+ccl(i-2,i-2,i-3)+f12(i-1)+Mod)%Mod;//用到i-4,所以从5开始
        g[i] = (g[i-1]+g[i-2]+ccl(i-1,i-1,i-2)+ccl(i-2,i-2,i-1)+f12(i)+f21(i)+ccl(i-2,i-1,i))%Mod;//用到f[i]和l[i],所以f[i]l[i]在前面
    }
    int T, kas = 0; scanf("%d",&T);
    while(T--){
        int n; scanf("%d",&n);
        printf("Case #%d: %I64d\n",++kas,f[n]);
    }
    return 0;
}
时间: 2024-10-06 13:10:45

HDU 5459 Jesus Is Here (递推)的相关文章

ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)

Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 88 Case #4: 352 Case #5: 318505405 Case #6: 391786781 Case #7: 133875314 Case #8: 83347132 Case #9: 16520782 题目要求当前字符串序列中某项里cff前缀两两间差值的和. 假设已经纪录了cff前缀的

Hdu 5459 Jesus Is Here (2015 ACM/ICPC Asia Regional Shenyang Online) 递推

题目链接: Hdu 5459 Jesus Is Here 题目描述: s1 = 'c', s2 = 'ff', s3 = s1 + s2; 问sn里面所有的字符c的距离是多少? 解题思路: 直觉告诉我们,sn肯定由sn-1与sn-2推导出来的.然后呢,我们可以看出 n%2==1 的时候 sn-1 与 sn-2 由 ffff 衔接起来的,n%2==0 的时候,sn-1 与 sn-2由 ff 衔接起来的.告诉队友后,队友就把这个当成重要依据推啊,推啊!!到最后感觉丢队友自己看药丸,放弃02回来和队友

HDU Tickets(简单的dp递推)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 972    Accepted Submission(s): 495 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

hdu 4869 Turn the pokers(递推&amp;组合数学&amp;逆元)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1279    Accepted Submission(s): 466 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5069    Accepted Submission(s): 2868 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Outpu

hdu 2050 折线分割平面 (递推)

折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15709    Accepted Submission(s): 10836 Problem Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面

HDU 3899 树形DP||树上递推

JLUCPC 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3899 Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1375    Accepted Submission(s): 413 Problem Description Dr. Skywind and Dr. Walkonclo

HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 140 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, t

HDU 3485 Count 101(递推)

C - Count 101 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3485 Description You know YaoYao is fond of his chains. He has a lot of chains and each chain has n diamonds on it. There are two ki