http://acm.hdu.edu.cn/showproblem.php?pid=5459

题意:

S(1) = c,S(2) = ff, S(3) = cff,之后S(i) = S(i-1)+S(i-2)。

现在给出n,求S(n)中任意两个c之间距离的总和。

思路:
现在假设第i-1和第i-2要合成第i个,计算S(i)的过程如下:

ans[i] = ans[i-1]+ans[i-2]+add,现在要求就是add新增的部分值。

假设S(i-2)中有2个c,下标分别为{a,b}(下标以1为起始点计)总长度为len2,S(i-1)中有3个c,下标为别为{x,y,z},总长度为len1。

那么新增的部分就是(len2-a)+x+ (len2-a)+y + (len2-a)+z +(len2-b)+x +(len2-b)+y +(len2-b)+z,也就是(len2-a+len2-b)*3 + (x+y+z)*2。

这里的话(len2-a+len2-b)就是S(i-2)中所有c点到末端的距离之和,3是S(i-1)中c的个数,x+y+z是S(i-1)中所有c点到始端的距离之和,2是S(i-2)中c的个数。

所有维护四个值进行递推,len是长度,sum是所有c点到末端的距离之和(到始端的距离之和可以通过len和sum计算出来),num是c的个数,ans是最终答案。

 #include<iostream>
#include<cstdio>
using namespace std;
const int mod = ;
const int maxn = +; long long len[maxn],num[maxn],sum[maxn],ans[maxn]; void init()
{
len[] = , num[] = , sum[] = , ans[] = ;
len[] = , num[] = , sum[] = , ans[] = ;
len[] = , num[] = , sum[] = , ans[] = ;
len[] = , num[] = , sum[] = , ans[] = ;
for(int i=;i<=;i++)
{
len[i] = (len[i-]+len[i-])%mod;
num[i] = (num[i-]+num[i-])%mod;
ans[i] = (ans[i-]+ans[i-]+sum[i-]*num[i-]+(((len[i-]*num[i-]-sum[i-])%mod)*num[i-])%mod)%mod;
sum[i] = ((sum[i-]+sum[i-])%mod+(num[i-]*len[i-])%mod)%mod;
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
int kase = ;
init();
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
printf("Case #%d: %lld\n",++kase,ans[n]);
}
return ;
}

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

  1. HDU 5459 Jesus Is Here (递推,组合数学)

    有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...

  2. 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: 8 ...

  3. 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的距离是多少? 解题思路: 直觉告诉我 ...

  4. hdu 5459 Jesus Is Here 数学

    Jesus Is Here Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

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

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. 题解报告:hdu 2084 数塔(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...

  7. HDU 5965 三维dp 或 递推

    题意:= =中文题 思路一:比赛时队友想的...然后我赛后想了一下想了个2维dp,但是在转移的时候,貌似出了点小问题...吧?然后就按照队友的思路又写了一遍. 定义dp[i][j][k],表示第i列, ...

  8. HDU 2569(简单的递推)

    彼岸 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

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

    折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. over(partition by)开窗函数的使用

    开窗函数是分析函数中的一种,开窗函数与聚合函数的区别是:开窗函数是用于计算基于组的某种聚合值且每个的组的聚合计算结果可以有多行,而聚合函数每个组的聚合计算结果只有一个.使用开窗函数可以在没有group ...

  2. Linux基础命令---查找进程id

    pidof pidof可以查找指定名称的进程的pid,将结果送到标准输出.pidof有两种返回值:0,找到至少一个进程:1,没有找到进程.pidof实际上与killall5相同:程序根据调用它的名称进 ...

  3. 创建一个简单的WCF程序

    1.创建WCF服务库 打开VS2010,选择文件→新建→项目菜单项,在打开的新建项目对话框中,依次选择Visual C#→WCF→WCF服务库,然后输入项目名称(Name),存放位置(Location ...

  4. 51Nod 1256 乘法逆元

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256 给出2个数M和N(M < N),且M与N互质,找 ...

  5. Shell生成数字序列

    转自http://kodango.com/generate-number-sequence-in-shell Shell里怎么输出指定的数字序列: for i in {1..5}; do echo $ ...

  6. Pytorch的torch.cat实例

    import torch 通过 help((torch.cat)) 可以查看 cat 的用法 cat(seq,dim,out=None) 其中 seq表示要连接的两个序列,以元组的形式给出,例如:se ...

  7. Prometheus监控学习笔记之Prometheus普罗米修斯监控入门

    0x00 概述 视频讲解通过链接网易云课堂·IT技术快速入门学院进入,更多关于Prometheus的文章. Prometheus是最近几年开始流行的一个新兴监控告警工具,特别是kubernetes的流 ...

  8. git clone 报错Unable to negotiate with xxx.xxx.xxx.xxx port 12345: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1

    在执行git clone命令报错 Unable to negotiate with xxx.xxx.xxx.xxx port 12345: no matching key exchange metho ...

  9. history 基本用法

    设置记录保存的数量,默认1000: /etc/profile 记录保存文件,可用来查看或修改记录: ~/.bash_history 如果是root用户就是在/root/.bash_history 直接 ...

  10. Codeforces 834D The Bakery - 动态规划 - 线段树

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...