HDU5459 Jesus Is Here
来源:互联网 发布:知乎洛基香港代购苹果 编辑:程序博客网 时间:2024/06/10 10:03
题目:
每个S(n)是由S(n-2)和S(n-1)拼起来得到的,是一串’c’和’f’的组合,求S(n)中每两个’c’的距离之和。
分析:
基本思路是递推,F(n)代表S(n)中每两个c的距离之和;
首先可以知道
F(n)=F(n-2)+F(n-1)+分别处在S(n-2)和S(n-1)中的c的距离之和;
我们考虑如何求得“分别处在S(n-2)和S(n-1)中的c的距离之和”即
由于两个S组合得到一个新S,我们可以很容易得到递推关系:
1.用 num[n] 代表S(n)中c的个数
num[i]=num[i-2]+num[i-1];
( num[1]=1;num[2]=0; )
2.用 lenth[n] 代表S(n)的长度
lenth[i]=lenth[i-2]+lenth[i-1];
( lenth[1]=1;lenth[2]=2; )
3.用 last[n] 代表S(n)中c到末尾的距离之和
last[i]=last[i-1]+last[i-2]+num[i-2]*lenth[i-1];
4.用 front[n] 代表S(n)中c到开头的距离之和
front[i]=front[i-1]+front[i-2]+num[i-1]*lenth[i-2];
( front[1]=1;front[2]=0; )
5.F[i]=F[i-2]+F[i-1]+num[i-1]*last[i-2]+num[i-2]*front[i-1];
( F[1]=0;F[2]=0; )
以上都可以O(n)递推得到,于是此题可解
代码:
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int Tmax=201320,TT=201315,MOD=530600414;int n;long long int F[Tmax],front[Tmax],last[Tmax],num[Tmax],lenth[Tmax];void work(){ int i; lenth[1]=1;lenth[2]=2; num[1]=1;num[2]=0; for(i=3;i<=TT;i++) { lenth[i]=(lenth[i-1]+lenth[i-2])%MOD; num[i]=(num[i-1]+num[i-2])%MOD; } last[1]=0;last[2]=0; front[1]=1;front[2]=0; for(i=3;i<=TT;i++) { last[i]=(last[i-1]+last[i-2]+(num[i-2]*lenth[i-1])%MOD)%MOD; front[i]=(front[i-1]+front[i-2]+(num[i-1]*lenth[i-2])%MOD)%MOD; } F[1]=0;F[2]=0; for(i=3;i<=TT;i++) F[i]=(F[i-1]+F[i-2]+(num[i-1]*last[i-2])%MOD+(num[i-2]*front[i-1])%MOD)%MOD; return;}int main(){ int T,t; scanf("%d",&T); work(); for(t=1;t<=T;t++) { scanf("%d",&n); printf("Case #%d: %lld\n",t,F[n]); } return 0;}
阅读全文
0 0
- hdu5459 Jesus Is Here
- HDU5459 Jesus Is Here
- hdu5459 Jesus Is Here 递推
- hdu5459 Jesus Is Here(沈阳网赛)
- Jesus Is Here HDU
- Jesus Is Here HDU
- HDU 5459 Jesus Is Here
- HDU 5459 Jesus Is Here
- HDU 5459 Jesus Is Here
- HDU 5459 Jesus Is Here(DP)
- HDU_5459 Jesus Is Here(递推)
- 【递推】 HDU 5459 Jesus Is Here
- HDU 5459 Jesus Is Here(递推)
- hdu 5459 Jesus Is Here(dp)
- HDU 5459 Jesus Is Here dp+预处理
- hdu 5459 Jesus Is Here(推公式)
- Jesus Is Here(区域赛网络赛选拔)
- hdoj 5459 Jesus Is Here 【求公式 递推】
- PHP环境搭建
- 闲下来的程序员能做点什么
- 趣图 | 程序员眼中的 Eclipse
- Reverse digits of an integer.
- C++中构造函数的执行顺序
- HDU5459 Jesus Is Here
- 排序
- Xamarin Android 中修改 TabbedPage 标题字体
- Java9之HttpClientAPI实战详解
- NOI2014魔法森林题解报告
- HTML 地址 缩写和首字母缩写
- hdu_4422_题意很重要
- 数据结构与算法
- 深入探讨Java字符串的拼接