【 HDU - 5459】Jesus Is Here(dp)

题干:

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"s1=‘‘c" and the second one is s2=‘‘ff"s2=‘‘ff". 
The ii-th message is si=si−2+si−1si=si−2+si−1 afterwards. Let me give you some examples. 
s3=‘‘cff"s3=‘‘cff", s4=‘‘ffcff"s4=‘‘ffcff" and s5=‘‘cffffcff"s5=‘‘cffffcff". 

``I found the ii-th message's utterly charming," Jesus said. 
``Look at the fifth message". s5=‘‘cffffcff"s5=‘‘cffffcff" and two ‘‘cff"‘‘cff" appear in it. 
The distance between the first ‘‘cff"‘‘cff" and the second one we said, is 55. 
``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"‘‘cff" as substrings of the message. 

Input

An integer T (1≤T≤100)T (1≤T≤100), indicating there are TT test cases. 
Following TT lines, each line contain an integer n (3≤n≤201314)n (3≤n≤201314), as the identifier of message.

Output

The output contains exactly TT lines. 
Each line contains an integer equaling to: 

∑i<j:sn[i..i+2]=sn[j..j+2]=‘‘cff"(j−i) mod 530600414,∑i<j:sn[i..i+2]=sn[j..j+2]=‘‘cff"(j−i) mod 530600414,


where snsn as a string corresponding to the nn-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

题目大意:

   字符串s[1]="c",s[2]="ff",s[i]=s[i-2]+s[i-1](i>=3);

   对于每个n,求s[n]中所有的任意两个字符c的距离之和f[n];

解题报告:

   同时维护五个值然后dp就行了。对于f[i],为f[i-1]+f[i-2]左右两侧分别的 + 左侧对右侧造成的贡献。所以求f[i]的同时维护一下每个字符串的  所有c到右侧的距离和  ,  所有c到左侧的距离和,c的个数,字符串长度。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 3e5 + 5;
const ll mod = 530600414LL;
ll l[MAX],r[MAX],f[MAX],len[MAX],c[MAX];
int main()
{len[3] = 3;len[4] = 5;l[4] = 3;r[4] = 3;c[4] = 1;l[5] = 1 + 6;r[5] = 3 + 8;c[5] = 2;len[5] = len[4] + len[3];//8l[6] = 3 + 6 + 11;//l[4] + len[4]*c[5] + l[5]r[6] = 3 + 8 + 11;//r[5] + c[4]*len[5] + r[4]c[6] = c[5]+c[4];len[6] = len[5]+len[4];f[5] = 5;f[6] = 16;for(int i = 7; i<=201314; i++) {len[i] = len[i-1] + len[i-2];c[i] = c[i-1]+c[i-2];l[i] = l[i-2] + len[i-2]*c[i-1] + l[i-1];r[i] = r[i-1] + c[i-2]*len[i-1] + r[i-2];f[i] = f[i-1]+f[i-2] + (r[i-2]-c[i-2]+mod)*(c[i-1]) + c[i-2]*(l[i-1]);l[i]%=mod;r[i]%=mod;f[i]%=mod;len[i]%=mod;c[i]%=mod;}int t,n,iCase=0;cin>>t;while(t--) {scanf("%d",&n);printf("Case #%d: %lld\n",++iCase,f[n]%mod);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440239.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

TextRank算法原理和提取关键词的主要过程详解 计算句子相似度 计算句子重要性公式

1、TextRank计算句子相似度和句子重要性的公式 2、TextRank算法提取关键词的过程 3、TextRank建立关键词无向图

Apollo自动驾驶入门课程第⑥讲 — 预测

目录 1. 简介 2. 不同的预测方式 3. 基于车道序列的预测 4. 障碍物状态 5. 预测目标车道 6. 递归神经网络 7. 递归神经网络在目标车道预测的应用 8. 轨迹生成 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a;涛涛CV Apollo开发者社区 9月6日 上一篇文…

使用PDF.js实现前端和手机端网页预览PDF文件(可定制,支持本地文件、Base64编码和远程URL跨域方式)

1.插件下载地址&#xff1a;https://mozilla.github.io/pdf.js/ 下载后解压pdfjs-1.10.88-dist.zip文件后得到&#xff1a; 2.把pdfjs-1.10.88-dist放到项目静态资源中&#xff0c;在自己的页面中通过iframe链接到pdfjs-1.10.88-dist/web/viewer.html文件中。 3.访问自己的页…

【HDU - 5878】I Count Two Three(打表)

题干&#xff1a; I will show you the most popular board game in the Shanghai Ingress Resistance Team. It all started several months ago. We found out the home address of the enlightened agent Icount2three and decided to draw him out. Millions of missile…

移动互联网浩荡十年 有的升腾,有的陨落

原创&#xff1a; 颜西龙 猎云网 &#xff08;ilieyun&#xff09;1周前 中国移动互联网的十年&#xff0c;是波澜壮阔、荡气回肠的十年。本文回溯了这段历史&#xff0c;在这十年间里&#xff0c;有的企业升腾&#xff0c;有的企业陨落。 2011年8月16日&#xff0c;北京798艺术…

原生JS动态计算输入框文本内容的宽度,当内容宽度超过输入框的宽度时可控

需求场景&#xff1a;左边输入框输入内容&#xff0c;右边输入框用placeholder展示&#xff0c;当placeholder的内容宽度超过右边输入框的宽度时&#xff0c;placeholder强行替换为“请选择” 注意事项&#xff1a;1、左右输入框的大小、样式都无关&#xff1b; 2、实际业务中…

【HDU - 5881】Tea(思维,找规律)

题干&#xff1a; Tea is good. Tea is life. Tea is everything. The balance of tea is a journey of pursuing balance of the universe. Alice knows that. Alice wants to teach you the art of pouring tea. Alice has a pot of tea. The exact volume of tea is…

Apollo自动驾驶入门课程第⑦讲 — 规划(上)

目录 1. 规划简介 2. 将地图转为图形 3. 路径查找算法&#xff1a;A* 4. 轨迹生成 5. Fernet坐标系 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月13日 上周我们发布了无人驾驶技术的 预测篇&#xff0c;简要介绍了预测的…

JS正则表达式常见场景下的用法总结

&#xff08;一&#xff09;前置知识总结&#xff1a; 1. 正则表达式 /xxxx/[标识] 其中的标识含义 •g &#xff08;全文查找&#xff09; •i &#xff08;忽略大小写&#xff09; •m &#xff08;多行查找&#xff09; 2. 正则表达式创建的两种方式&#xff08;等价&#…

【HDU - 5882】Balanced Game (找规律,思维)

题干&#xff1a; Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock", "paper", and "scisso…

Apollo自动驾驶入门课程第⑧讲 — 规划(下)

目录 1. 路径-速度解耦规划 2. 路径生成与选择 3. ST图 4. 速度规划 5. 优化 6. 路径-速度规划的轨迹生成 7. Lattice规划 8. ST轨迹的终止状态 9. SL轨迹的终止状态 10. Lattice规划的轨迹生成 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿…

网络编程懒人入门(五):快速理解为什么说UDP有时比TCP更有优势

转自即时通讯网&#xff1a;http://www.52im.net/ 本文观点仅作参考&#xff0c;请根据自已系统的应用场景合理地选择数据传输层协议即可&#xff0c;无需盲目崇拜大牛言论。 1、前言 对于即时通讯开者新手来说&#xff0c;在开始着手编写IM或消息推送系统的代码前&#xff…

CSS定位总结:position=static/relative/absolute/fixed时的区别、top/bottom/left/right与margin外边距的运用

准备布局&#xff1a; <!DOCTYPE html> <html> <head> <style> *{ margin:0; padding:0; } .base-container{width: 500px;height: 500px;background-color:lightgray;border:1px dashed red; } .base-compare {width:200px;height:100px;background…

【HDU - 5883】The Best Path(判断欧拉回路)

题干&#xff1a; Alice is planning her travel route in a beautiful valley. In this valley, there are NN lakes, and MM rivers linking these lakes. Alice wants to start her trip from one lake, and enjoys the landscape by boat. That means she need to set up …

网络编程懒人入门(六):史上最通俗的集线器、交换机、路由器功能原理入门

转自即时通讯网&#xff1a;http://www.52im.net/ 本文引用了知乎网友“薛定谔不在家”的部分文字内容&#xff0c;感谢原作者的分享。 1、前言 即时通讯网整理了大量的网络编程类基础文章和资料&#xff0c;包括《TCP/IP协议 卷1》、《[通俗易懂]深入理解TCP协议》系列、《…

总结JSON.parse()报错VM71:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0等之类的问题

问题场景&#xff1a;在调试前端应用的时候经常出现形如“Uncaught SyntaxError: Unexpected”之类的令人头疼觉得莫名其妙的问题&#xff1b;所以有必要总结整理一下关于JSON.parse()这个API方法的注意事项。以便在以后的开发中出现类似的问题能第一时间想到可能是这个方法的参…

【HDU - 5884】Sort(k叉哈夫曼树,优化tricks,两个队列代替优先队列)

题干&#xff1a; Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice. Alice will give Bob NN sorted sequences, and the ii-th sequence includes aiai elements. Bob need to merge all of these sequences. H…

金额输入框校验和自动校正、支持指定任意位数小数decimal、支持只能输入整数、支持是否允许输入负数等功能

应用场景&#xff1a;开发前端交互页面时&#xff0c;经常遇到金额输入框、指定小数位数的数字输入框&#xff0c;单一的正则表达式无法满足大部分的业务校验需求&#xff0c;下面总结一个实用巧妙而又灵活的把普通输入框变成自动校正输入框的解决方案&#xff1a; 数字&#…

网络编程懒人入门(七):深入浅出,全面理解HTTP协议

转自即时通讯网&#xff1a;http://www.52im.net/ 本文引用了自简书作者“涤生_Woo”的文章&#xff0c;内容有删减&#xff0c;感谢原作者的分享。 1、前言 HTTP&#xff08;全称超文本传输协议&#xff0c;英文全称HyperText Transfer Protocol&#xff09;是互联网上应用…

【HDU - 5889】Barricade(最短路+网络流,最小割)

题干&#xff1a; The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as NN towns and MM roads, and each road has the same length and connects two towns. The town numbered 11 is where generals cast…