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. 洛谷 P5016 龙虎斗

    输入兵营总数.兵营人数.以m分界. 然后输入s1个兵到了p1兵营. 最终我们要求的是把s2个兵放到哪个兵营使龙虎双方气势差距最小. 第一要把每个兵营的气势算出来,并且加到它所属的阵营里(<m是龙 ...

  2. 科技庄园(背包dp)---对于蒟蒻来说死了一大片的奇题

    题目描述: Life种了一块田,里面种了一些桃树. Life对PFT说:“我给你一定的时间去摘桃,你必须在规定的时间之内回到我面前,否则你摘的桃都要归我吃!” PFT思考了一会,最终答应了! 由于PF ...

  3. 【NOIP2017提高组模拟7.3】B

    树上路径统计,点分治解决. 统计一段区间,naive地用了set解决,这样的复杂度是O(nlog^2n)的 考场代码出了个问题,统计答案时找到了之前的最优答案,但是没有加上新的一段,导致60分 #in ...

  4. ubuntu16.04安装 java JDK8

    安装openjdk1.更新软件包列表: sudo apt-get update 2.安装openjdk-8-jdk: sudo apt-get install openjdk-8-jdk 3.查看ja ...

  5. GoogleTest 之路2-Googletest 入门(Primer)

    Why googletest? 为啥要用GoogleTest呢? googletest 是由测试技术Team 开发的带有google 特殊的需求和限制的测试框架. 不管你在什么平台上写C++代码,go ...

  6. zigbee 中 OSAL 事件传递机制和消息传递机制

    一.概述 OSAL (Operating System Abstraction Layer) ,翻译为"操作系统抽象层". OSAL 就是一种支持多任务运行的系统资源分配机制.OS ...

  7. Mysql显示所有数据库

    show databases; mysql> show databases; +--------------------+ | Database | +--------------------+ ...

  8. UVa 579 Clock Hands

    水题.. 求任意时刻时针和分针的夹角,其结果在0°到180°之间. 这里又一次用到了sscanf()函数,确实很方便. 思路:我们分别求出时针和分针转过的角度,然后大的减小的,如果结果ans大于180 ...

  9. Ubuntu首次登入与在线求助man page总结

    1.为了避免瞬间断电造成的Linux系统的危害,建议做为服务器的Linux主机应该加上不断电系统来持续提供稳定的电力 2.默认的图形模式登入中,可以选择语系以及作业阶段.作业阶段为多种窗口管理员软件所 ...

  10. asp.net多线程在web页面中简单使用

    需求:一个web页面 default.aspx 里面有两个控件GridView1,GridView2,通过两个线程分别加载绑定数据. 绑定GridView1:void BindCategory()  ...