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

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
 
Source
 

题意s[i] = s[i-1] + s[i-2], s[1] = c, s[2] = ff;

s[i]中没两个c之间的距离之和

定义:dp[i]表示i所对应的答案, 那么dp[i] = dp[i-1] + dp[i-2] + (s[i-1]中的每个c到s[i-2]中的c的距离)

定义:rd[i]表示s[i]中的每个c到s[i]的末尾的距离

ld[i]表示s[i]中的每个c到s[i]的首位的距离

ls[i]表示s[i]的长度

cnt[i]表示s[i]中c的个数

那么有:

rd[i] = rd[i-1] + rd[i-2] + cnt[i-2] * ls[i-1];

ld[i] = ld[i-1] + ld[i-2] + cnt[i-1] * ls[i-2];

  dp[i] = dp[i-1] + dp[i-2] + cnt[i-1] * rd[i-2] + cnt[i-2] * ld[i-1];

dp[i] = dp[i-1] + dp[i-2] + cnt[i-1] * rd[i-2] + cnt[i-2] * ld[i-1];

#include <bits/stdc++.h>
using namespace std; const int N = 201413;
const int M = 530600414;
typedef long long ll; ll dp[N], rd[N], ld[N];
ll ls[N], cnt[N]; void init()
{
ls[3] = 3; cnt[3] = 1; dp[3] = 0;
ls[4] = 5; cnt[4] = 1; dp[4] = 0;
rd[3] = 3; ld[3] = 0;
rd[4] = 3; ld[4] = 2;
for(int i = 5; i < N; ++i)
{
ls[i] = (ls[i - 1] + ls[i - 2]) % M;
cnt[i] = (cnt[i - 1] + cnt[i - 2]) % M; rd[i] = (rd[i - 2] + rd[i - 1] + cnt[i - 2] * ls[i - 1]) % M;
ld[i] = (ld[i - 2] + ld[i - 1] + cnt[i - 1] * ls[i - 2]) % M; dp[i] = (dp[i - 1] + dp[i - 2] + cnt[i - 1] * rd[i - 2] + cnt[i - 2] * ld[i - 1]) % M;
}
} int main()
{
int _, cas = 1; scanf("%d", &_);
init();
while(_ --)
{
int n; scanf("%d", &n);
printf("Case #%d: %lld\n",cas++, dp[n] % M);
}
return 0;
}

  

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

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
 
Source
 

hdu 5459 Jesus Is Here (费波纳茨递推)的更多相关文章

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

  2. C++ 以费波纳茨数列为权重的加权均值计算方法 wMA

    #pragma once #include <iostream> using namespace std; template <typename T> double *wMA( ...

  3. hdu 5459 Jesus Is Here 数学

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

  4. HDU 5459 Jesus Is Here(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意: S(1) = c,S(2) = ff, S(3) = cff,之后S(i) = S(i-1)+S( ...

  5. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  6. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  7. 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 ...

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

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

  9. hdu 5273 Dylans loves sequence 逆序数简单递推

    Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

随机推荐

  1. Java容器题库

    一.    填空题 Java集合框架提供了一套性能优良.使用方便的接口和类,包括Collection和Map两大类,它们都位于  java.util  包中 队列和堆栈有些相似,不同之处在于栈是先进后 ...

  2. sqlserver 解析Json字符串

    转自:https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/ http://ww ...

  3. September 28th 2016 Week 40th Wednesday

    Love all, trust a few, do wrong to none. 爱所有人,信任一些人,不妨害任何人. Reading is a way for me to expand my min ...

  4. vector 去重复

    ①首先将vector排序 sort( vecSrc.begin(), vecSrc.end() ); // 1,2,3,3,4,4,6,7,8,9 ②然后使用unique算法,unique返回值是重复 ...

  5. [转]c++ vector 遍历方式

    挺有趣的,转来记录 随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化.   vector是经常要使用到的std组件,对于vector的遍历,本文罗列了若干种写 ...

  6. 三、jQuery--jQuery基础--jQuery基础课程--第1章 初识jQuery

    环境搭建 搭建一个jQuery的开发环境非常方便,可以通过下列几个步骤进行. 下载jQuery文件库 在jQuery的官方网站(http://jquery.com)中,下载最新版本的jQuery文件库 ...

  7. php 复习

    <?php 一.php基础语法1.输出语句:echo print print_r var_dump() 2.php是弱类型语言强制转换类型: (类型)变量 settype(变量,类型) 3.变量 ...

  8. Eclipse的使用及Java程序的标识符和关键字

    Eclipse的使用 (1)创建Java项目 选择“文件”/“新建”/“Java项目”命令,在弹出的“新建Java项目”对话框输入项目名,然后点击“下一步”,最后单击“完成”. (2)创建Java类文 ...

  9. js 动态时钟

    js 动态时钟 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  10. linux中socket的理解

    对linux中socket的理解 一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用“打开open –> 读写write/read –> ...