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. 基于 MapReduce 的单词计数(Word Count)的实现

    完整代码: // 导入必要的包 import java.io.IOException; import java.util.StringTokenizer; import org.apache.hado ...

  2. HAProxy-1.8.20 根据后缀名转发到后端服务器

    global maxconn 100000 chroot /data/soft/haproxy stats socket /var/lib/haproxy/haproxy.sock mode 600 ...

  3. python模块详解 | shutil

    简介: shutil是python的一个内置模块,提供了许多关于文件和文件集合的高级操作,特别提供文件夹与文件操作.归档操作了支持文件复制和删除的功能. 文件夹与文件操作: copyfileobj(f ...

  4. 十一:WEB渗透必懂知识点

    简述WEB层面上的漏洞以及类型,具体漏洞的危害等级, 如何形成以及如何发现 右边权重大于左边 CTF,SRC,红蓝对抗,实战 简要说明以上漏洞危害 简要说课以上漏洞等级划分 简要说明以上漏洞重点内容 ...

  5. MySQL select if 查询最后一个主键 id

    查询最后一个主键id SELECT IF(MAX(id) IS NULL, 0, MAX(id)) AS maxid FROM users; 查询最小的主键id SELECT IF(MIN(id) I ...

  6. 【ORACLE】删除表空间,没有删除数据文件怎么办?解决办法

    创建表空间 SQL> create tablespace TEST datafile='+DATA/rac/datafile/test01.dbf' size 1g; Tablespace cr ...

  7. 19.java设计模式之备忘录模式

    基本需求 游戏的角色有攻击力和防御力,在大战Boss之前保存自身的状态(攻击力和防御力),当大战Boss之后攻击力和防御力下降,从备忘录对象恢复到大战前的状态 传统方案 一个对象,就对应一个保存对象状 ...

  8. SAP里会话结束方法(杀死进程)

    在SAP的ERP里,有很多方法可以结束一个会话,然而在不同情况下,需要使用的方法也不同.下面从先后顺序来简单说明:1.SM04:最常用的方法,在SM04点击工具栏的会话->结束会话,来关闭一个会 ...

  9. 开发中的你的Git提交规范吗?

    1. 前言 目前大部分公司都在使用Git作为版本控制,每个程序员每天都要进行代码的提交.很多开发者也包括我自己,有时候赶时间或者图省事,就这么提交: git commit -m "修改bug ...

  10. 二. SpringCloud基本Rest微服务工程搭建

    1. 父工程构建 1.1 Maven项目搭建 环境 版本 JDK 1.8 Maven 3.6+ Maven模板 maven-archetype-size 删除父工程src文件 1.2 父工程pom文件 ...