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

这题主要是推公式,记c[i]为c字符的个数,s[i]为c字符的坐标和,n[i]为所有字符的总个数,f[i]表示求的距离差。

那么f[i]=(c[i-2]*n[i-2]-s[i-2])*c[i-1]+c[i-2]*s[i-1];其中c[i-2]*n[i-2]-s[i-2]为所有c前半部分到合并中间的距离和。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x7fffffff
#define mod 530600414
#define maxn 201316
ll f[maxn],c[maxn],s[maxn],n[maxn]; void init()
{
int i,j;
c[3]=1;s[3]=1;n[3]=3;f[3]=0;
c[4]=1;s[4]=3;n[4]=5;f[4]=0;
for(i=5;i<=201314;i++){
f[i]=(f[i-1]+f[i-2]+ ( (c[i-2]*n[i-2]-s[i-2])%mod )*c[i-1]%mod+c[i-2]*s[i-1]%mod )%mod;
n[i]=(n[i-1]+n[i-2])%mod;
c[i]=(c[i-1]+c[i-2])%mod;
s[i]=( (s[i-2]+ s[i-1])%mod+(n[i-2]*c[i-1])%mod )%mod;
}
} int main()
{
int m,i,j,T,num1=0,d;
scanf("%d",&T);
init();
while(T--)
{
scanf("%d",&d);
num1++;
printf("Case #%d: %lld\n",num1,f[d]);
}
return 0;
}

hdu5459 Jesus Is Here的更多相关文章

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

    Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission ...

  2. hdu 5459 Jesus Is Here 数学

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

  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. Jesus Is Here

    Jesus Is Here Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)To ...

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

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

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

  7. hdu 5459(2015沈阳网赛) Jesus Is Here

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意 给出一组字符串,每个字符串都是前两个字符串相加而成,求第n个字符串的c的各个坐标的差的和,结果 ...

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

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

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

随机推荐

  1. Centos 6 下安装 OSSEC-2.8.1 (一)

    ossec -2.8.1 安装: ## 1 ) 安装依赖包: RedHat / Centos / Fedora / Amazon Linux yum install -y pcre mysql mys ...

  2. Logrotate工具使用

    Logrotate ​ logrotate是一个被设计来简化系统管理日志文件的工具,在系统运行时,如果产生大量的日志文件,可以使用该工具进行管理,如/var/log/*文件夹是存储系统和应用日志的目录 ...

  3. 怎么判断是旧版本的ext3还是新版本?

    怎么判断是旧版本的ext3还是新版本的?   ---高性能419

  4. 【Linux】saltstack的使用详解 超详细

    一.salt常用命令 salt 该命令执行salt的执行模块,通常在master端运行,也是我们最常用到的命令 salt [options] '<target>' <function ...

  5. CTFshow-萌新赛杂项_签到

    查看网页信息 http://game.ctf.show/r2/ 把网页源码下载后发现有大片空白 使用winhex打开 把这些16进制数值复制到文件中 把20替换为0,09替换为1后 得到一串二进制数值 ...

  6. WeihanLi.Npoi 1.14.0 Release Notes

    WeihanLi.Npoi 1.14.0 Release Notes Intro 周末更新了一下项目,开始使用可空引用类型,并且移除了 net45 的支持,仅支持 netstandard2.0 Cha ...

  7. error: Failed dependencies: rpm安装包失败报错依赖包

    error: Failed dependencies: mysql-community-release conflicts with (installed) mysql57-community-rel ...

  8. The router relies on a tree structure which makes heavy use of common prefixes, it is basically a compact prefix tree (or just Radix tree).

    https://github.com/julienschmidt/httprouter/

  9. 从输入URL到页面展示,这中间都发生了什么?

    前言 在浏览器里,从用户输入URL到页面展示,这中间都发生了什么?这是一道非常经典的面试题.这里边涉及很多知识点,比如:网络协议.页面渲染.操作系统等.所以这是很好很全面的考察一个前端的知识.下面我将 ...

  10. loj10004智力大冲浪

    题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者 m 元.先不要太高兴!因为这些钱还不一定都是你的?!接下来主持人宣布了比赛规则 ...