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
``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.
Following T lines, each line contain an integer n (3≤n≤201314), as the identifier of message.
Each line contains an integer equaling to:
where sn as a string corresponding to the n-th message.
题意: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
``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.
Following T lines, each line contain an integer n (3≤n≤201314), as the identifier of message.
Each line contains an integer equaling to:
where sn as a string corresponding to the n-th message.
5
6
7
8
113
1205
199312
199401
201314
Case #2: 16
Case #3: 88
Case #4: 352
Case #5: 318505405
Case #6: 391786781
Case #7: 133875314
Case #8: 83347132
Case #9: 16520782
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的距离是多少? 解题思路: 直觉告诉我 ...
- C++ 以费波纳茨数列为权重的加权均值计算方法 wMA
#pragma once #include <iostream> using namespace std; template <typename T> double *wMA( ...
- hdu 5459 Jesus Is Here 数学
Jesus Is Here Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- 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( ...
- 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 ...
- CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD
题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...
- 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 ...
- HDU 5459 Jesus Is Here (递推,组合数学)
有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...
- hdu 5273 Dylans loves sequence 逆序数简单递推
Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
随机推荐
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 创建odoo数据库时出现错误原因
安装完odoo 8.0后创建数据库时出现如下错误信息: Odoo Odoo Server Error Traceback (most recent call last): File "D:\ ...
- CodeSign error: code signing is required for product type Application in SDK iOS
在真机测试的时候往往会突然出现这样一个错误,code signing is required for product type 'Application' in SDK 'iOS 7.0' ,就是说 ...
- UI课堂笔记
2016.7.18 + (UIColor *)blackColor; + (UIColor *)darkGrayColor; 深灰色 + (UIColor *)lightGrayColor; 浅 ...
- Ios cordova安装
安装NODEJS:http://nodejs.org/ (如果你安装了旧版本的需要手动安装npm,不过最新版的自带了) 进入终端命令行使用npm安装cordova如下: $ sudo npm inst ...
- [Android Pro] Android保存图片到系统图库
http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/ http://blog.csdn.net/x ...
- main方法并发测试
public static void main(String[] args) throws Exception{ RequestModel r = new RequestModel(); r.setT ...
- 1 mysql的安装
win10 总之前期的步骤大概有:1下载安装:2 安装好后配置环境变量:3:登陆数据库 1:安装 mysql有安装版和直接解压就可以用的,据说大神都是安装的直接解压的,但鉴于自己是小白,就整了个安装版 ...
- hdu 2020
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2020 思路:优先队列水过priority_queue #include <cstdio> ...
- MVC - 17.OA项目
1.分层 2.项目依赖关系 MODEL IDAL -> MODEL DAL -> IDAL,MODEL,EntityFramewrok(注意和MODEL里的版本要一致),S ...