Jesus Is Here

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 512    Accepted Submission(s): 368

Problem Description
I've sent Fang Fang around 201314 text messages in almost 5 years. Why can't she make sense of what I mean?
``But Jesus is here!" the priest intoned. ``Show me your messages."
Fine, the first message is s1=‘‘c" and the second one is s2=‘‘ff".
The i-th message is si=si−2+si−1 afterwards. Let me give you some examples.
s3=‘‘cff", s4=‘‘ffcff" and s5=‘‘cffffcff".

``I found the i-th message's utterly charming," Jesus said.
``Look at the fifth message". s5=‘‘cffffcff" and two ‘‘cff" appear in it.
The distance between the first ‘‘cff" and the second one we said, is 5.
``You are right, my friend," Jesus said. ``Love is patient, love is kind.
It
does not envy, it does not boast, it is not proud. It does not dishonor
others, it is not self-seeking, it is not easily angered, it keeps no
record of wrongs.
Love does not delight in evil but rejoices with the truth.
It always protects, always trusts, always hopes, always perseveres."

Listen - look at him in the eye. I will find you, and count the sum of distance between each two different ‘‘cff" as substrings of the message.

 
Input
An integer T (1≤T≤100), indicating there are T test cases.
Following T lines, each line contain an integer n (3≤n≤201314), as the identifier of message.
 
Output
The output contains exactly T lines.
Each line contains an integer equaling to:

∑i<j:sn[i..i+2]=sn[j..j+2]=‘‘cff"(j−i) mod 530600414,

where sn as a string corresponding to the n-th message.

 
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
 
这个题真的很难想,但是hdu划分其难度只有2,那么肯定要好好总结一下了。
给出前9项
c
0
ff
0
cff
0
ffcff
0
cffffcff
5
ffcffcffffcff
16
cffffcffffcffcffffcff
88
ffcffcffffcffcffffcffffcffcffffcff
352
cffffcffffcffcffffcffffcffcffffcffcffffcffffcffcffffcff
1552
 
题意:si = si-1+si-2 ,问第i个串中所有c距离之和为多少??
设dp[i]代表第i个串中c的距离之和.
那么dp[i] = dp[i-1]+dp[i-2]+Δ
关键是怎么求Δ。
我们设第i个串的长度为 len[i],那么容易得到 len[i] = len[i-1]+len[i-2] , len[1]=1,len[2]=2;
设第i个串中c的数量为 num[i],那么也容易得到 num[i]=num[i-2]+num[i-2],num[1]=1,num[2]=0;
接下来是最重要的:我们设dis[i]代表第i个串中所有的c到末尾的距离之和.那么dis[i]=dis[i-1]+dis[i-2]+num[i-2]*len[i-1],dis[1]=0,dis[2]=0,dis[i-1]+dis[i-2]的话不用说,num[i-2]*len[i-1]代表的是第i-2个串的所有的c到第i-1个串末尾的距离之和,那么增量肯定就是加上第i-2个串中所有的c的个数乘上第i-1个串的长度了。
接下来是如何将所有的条件用上了。
分段考虑:
1.考虑将i-2串中所有的c出发"跳跃"到i-2串的末尾,每个c进行num[i-1]次跳跃,所以总距离为dis[i-2]*num[i-1]
2.接下来考虑i-1串所有的c出发跳跃到i-1串的开头,这样是没办法直接求的,我们知道所有的c跳跃到末尾是dis[i-1],那么假设所有的c跳跃到开头走的总距离是L,那么L+dis[i-1]=len[i-1]*num[i-1],每个点进行i-2次跳跃,总共的距离是num[i-2]*(len[i-1]*num[i-1]-dis[i-1])
所以最后的结果为dp[i]=dp[i-1]+dp[i-2]+dis[i-2]*num[i-1]+num[i-2]*(len[i-1]*num[i-1]-dis[i-1])
记得碰到-号多加个mod..不然变成负数就错了!
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = ;
const LL mod =;
LL num[N],len[N],dis[N],dp[N];
void init()
{
num[]=,num[]=,num[]=,num[]=;
len[]=,len[]=,len[]=,len[]=;
dis[]=dis[]=,dis[]=dis[]=;
dp[]=dp[]=dp[]=dp[]=,dp[]=;
for(int i=;i<=;i++){
num[i]=(num[i-]+num[i-])%mod;
len[i]=(len[i-]+len[i-])%mod;
}
for(int i=;i<=;i++){
dis[i]=((dis[i-]+dis[i-])%mod+num[i-]*len[i-]%mod)%mod;
}
for(int i=;i<=;i++){
dp[i] = ((dp[i-]+dp[i-])%mod+num[i-]*dis[i-]%mod+
(len[i-1]*num[i-1]%mod-dis[i-1]+mod)%mod*num[i-]%mod)%mod;
}
}
int main(){
init();
int tcase;
scanf("%d",&tcase);
for(int i=;i<=tcase;i++){
int n;
scanf("%d",&n);
printf("Case #%d: %lld\n",i,dp[n]);
}
return ;
}

hdu 5459(递推好题)的更多相关文章

  1. J - Jesus Is Here HDU - 5459 (递推)

    大意: 定义$f_1="c",f_2="ff",f_n=f_{n-2}+f_{n-1}$, 求所有"cff"的间距和. 记录c的个数, 总长 ...

  2. HDOJ(HDU).2044-2049 递推专题

    HDOJ(HDU).2044-2049 递推专题 点我挑战题目 HDU.2044 题意分析 先考虑递推关系:从1到第n个格子的时候由多少种走法? 如图,当n为下方格子的时候,由于只能向右走,所以有2中 ...

  3. hdu 1465:不容易系列之一(递推入门题)

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  4. hdu 2044-2050 递推专题

    总结一下做递推题的经验,一般都开成long long (别看项数少,随便就超了) 一般从第 i 项开始推其与前面项的关系(动态规划也是这样),而不是从第i 项推其与后面的项的关系. hdu2044:h ...

  5. FZU- Problem 1147 Tiling,递推坑题,大数水过~~

    Problem 1147 Tiling Time Limit: 1000 mSec Memory Limit : 32768 KB http://acm.fzu.edu.cn/problem.php? ...

  6. ZOJ 3182 HDU 2842递推

    ZOJ 3182 Nine Interlinks 题目大意:把一些带标号的环套到棍子上,标号为1的可以所以操作,标号i的根子在棍子上时,只有它标号比它小的换都不在棍子上,才能把标号为i+1的环,放在棍 ...

  7. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  8. "红色病毒"问题 HDU 2065 递推+找循环节

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=2065 递推类题目, 可以考虑用数学方法来做, 但是明显也可以有递推思维来理解. 递推的话基本就是状态 ...

  9. Children’s Queue HDU 1297 递推+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

随机推荐

  1. C++系统学习之九:顺序容器

    元素在顺序容器中的顺序与其加入容器时的位置相对应.关联容器中元素的位置由元素相关联的关键字值决定.所有容器类都共享公共的接口,不同容器按不同方式对其进行扩展. 一个容器就是一些特定类型对象的集合.顺序 ...

  2. VB6 代码编辑页面添加支持滚轮模式

    VB6 中的代码编辑页面默认是不支持滚轮模式的,这让在编辑代码时的体验很是不爽. 但在64位win10系统进行加载配置时,可能会出现问题,可用如下方法解决: http://download.micro ...

  3. 《linux设备驱动开发详解》笔记——10中断与时钟

    10.1 中断与定时器 中断一般有如下类型: 内部中断和外部中断:内部中断来自CPU,例如软件中断指令.溢出.除0错误等:外部中断有外部设备触发 可屏蔽中断和不可屏蔽中断 向量中断和非向量中断,ARM ...

  4. OpenCV中图像的读取,显示与保存

      图像的读取,显示与保存 相关函数:cv2.imread().cv2.imshow().cv2.imwrite() 1.读入图像: 用cv2.imread()函数来读取图像,cv2.imread(路 ...

  5. LeetCode(217)Contains Duplicate

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  6. CentOS 7 配置OpenCL环境(安装NVIDIA cuda sdk、Cmake、Eclipse CDT)

    序 最近需要在Linux下进行一个OpenCL开发的项目,现将开发环境的配置过程记录如下,方便查阅. 完整的环境配置需要以下几个部分: 安装一个OpenCL实现,基于硬件,选择NVIDIA CUDA ...

  7. vagrant中的precise64使用静态的能和host所在局域网的其他机器互相通信

    vagrant中的precise64使用静态的能和host所在局域网的其他机器互相通信

  8. ssh登陆之忽略known_hosts文件

    在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁止,并弹出如下类似提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  9. golang导出excel(excel格式)

    之前写过一篇导出cvs格式的,如果只是简单导出完全能满足需要.按时如果想要有复杂需求,如样式定制.多个sheet之类的,就无法完成了.之后发现有人已经实现golang直接excel对excel的操作, ...

  10. THUSC2019游记

    Day 0 完全没有明明是最后一次机会的紧张感.大概是滚粗的预兆. 住在西郊.房间好小. 和thupc前一样又有一场cometoj,好像又有小裙子了.upd:改成星空棒棒糖! Day 1 早餐有点棒. ...