hdu 5459 Jesus Is Here (费波纳茨递推)

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

2015 ACM/ICPC Asia Regional Shenyang Online

题意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

2015 ACM/ICPC Asia Regional Shenyang Online

时间: 2024-10-10 16:08:51

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

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回来和队友

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

#pragma once #include <iostream> using namespace std; template <typename T> double *wMA(T &tArray, int iWMALen) // 应用模版数据类型 typename T 引用外部数组 tArray { int iArrayLen = sizeof(tArray) / sizeof(tArray[0]); // 计算传入数组长度 = 总数组字节大小 / 首元素字节大小 cout

hdu 1165 Eddy&#39;s research II(数学题,递推)

// Eddy 继续 Problem Description As is known, Ackermann function plays an important role in the sphere of theoretical computer science. However, in the other hand, the dramatic fast increasing pace of the function caused the value of Ackermann function

hdu 5459 Jesus Is Here

一道递推题 感觉这种题找规律对我来说好困难……还是慢慢想吧 题意:s1 = c,s2 = ff,s3 = cff,……s[i] = s[i - 2] + s[i - 1] 从s5开始(s5 = cffffcff),出现了两个cff, 相隔为5,那么输出5 s6 = ffcffcffffcff,输出所有cff之间距离的和,为3+5+8 = 16 s[i],i<=201314,输出ans[i] 思路:由题意发现了一些类斐波那契数列,字符串s[i] = s[i - 2] + s[i - 1],字符串长

HDU - 2604 Queuing(矩阵快速幂或直接递推)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题意:给出字符串长度L,并且字符串只由'f','m'构成,有2^L种情况,问在其中不包含'fmf','fff'的字符串有多少个. 1.直接递推,虽然过了,但是数据稍微大点就很可能TLE,因为代码上交上去耗时还是比较长的(感觉数据有点水)╭(′▽`)╭(′▽`)╯( 1 #include <iostream> 2 #include <cstdio> 3 #include <c

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 his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers

费波纳茨数列 几种实现方法

斐波那契数列,又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)在现代物理.准晶体结构.化学等领域,斐波纳契数列都有直接的应用,为此,美国数学会从1963起出版了以<斐波纳契数列季刊>为名的一份数学杂志,用于专门刊载这方面的研究成果. 定义     斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 2

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 定义 至此,总方案已经可

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前缀的