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: 88
Case #4: 352
Case #5: 318505405
Case #6: 391786781
Case #7: 133875314
Case #8: 83347132
Case #9: 16520782
题目要求当前字符串序列中某项里cff前缀两两间差值的和。
假设已经纪录了cff前缀的位置,
当前第k个字符串的位置为
v[1], v[2], …v[cnt(k)]
其中cnt(k)表示当前字符串cff前缀的个数。
那么对于k+1
u[1], u[2], …u[cnt(k+1)]
然后组合两个串
v[1], v[2], …v[cnt(k)], u[1]+len(k), u[2]+len(k),…, u[cnt(k+1)]+len(k)
其中len(k)表示第k个字符串的长度。
考虑前半部分v的内部差的和为sum(k), 后半部分u的内部和为sum(k+1)
然后就差交叉部分。
考虑v[i],
自然是u[1]+len(k)-v[i] + u[2]+len(k)-v[i] +…+ u[cnt(k+1)]+len(k)-v[i]
化简得s[k+1]+cnt(k+1)*len(k)-cnt(k+1)*v[i]
然后对i求和
cnt(k)*s[k+1]+cnt(k)*cnt(k+1)*len(k)-cnt(k+1)*s[k]
于是sum就是上面三部分的和。
然后只需要同时维护s, cnt, len即可。
s[k+2]=s[k]+s[k+1]+cnt(k+1)*len(k)
cnt(k+2)=cnt(k+1)+cnt(k)
len(k+2)=len(k+1)+len(k)
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long
#define MOD 530600414 using namespace std; const int maxN = ;
int n;
LL cnt[maxN], s[maxN], len[maxN], sum[maxN]; void init()
{
cnt[] = cnt[] = ;
s[] = ; s[] = ;
len[] = ; len[] = ;
sum[] = sum[] = ;
for (int i = ; i < maxN; ++i)
{
cnt[i] = (cnt[i-]+cnt[i-])%MOD;
s[i] = (s[i-]+s[i-]+cnt[i-]*len[i-]%MOD)%MOD;
len[i] = (len[i-]+len[i-])%MOD;
sum[i] = (sum[i-]+sum[i-])%MOD;
sum[i] += (cnt[i-]*s[i-]%MOD+(cnt[i-]*cnt[i-]%MOD)*len[i-]%MOD-cnt[i-]*s[i-]%MOD)%MOD;
sum[i] = (sum[i]%MOD+MOD)%MOD;
}
} int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
scanf("%d", &n);
printf("Case #%d: %d\n", times+, sum[n]);
}
return ;
}
ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)的更多相关文章
- ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)
Problem Description In Land waterless, water is a very limited resource. People always fight for the ...
- ACM学习历程—HDU 5326 Work(树形递推)
Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...
- ACM学习历程—HDU5490 Simple Matrix (数学 && 逆元 && 快速幂) (2015合肥网赛07)
Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progressio ...
- ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 && 快速幂)(2015沈阳网赛1002题)
Problem Description The so-called best problem solver can easily solve this problem, with his/her ch ...
- ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)
Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...
- ACM学习历程—HDU1023 Train Problem II(递推 && 大数)
Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know ...
- ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
- AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)
Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...
- HDU 5459 Jesus Is Here (递推,组合数学)
有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...
随机推荐
- xcode编译时,有第三方库时,编译设置build active architecture only问题
本文转载至 http://blog.csdn.net/ysysbaobei/article/details/16371263 编译静态库 1.http://blog.csdn.net/dengdeng ...
- 新升级!EasyNVR3.0功能概述--直播与录像
背景介绍: 对于摄像机直播已经是我们司空见惯的需求,但是,许多用户在现有的直播的基础上更有录像的需求,并且有关于录像的删除定时等录像计划的需求,更有客户不仅需要这些功能,还需要将这些功能集成到自身的业 ...
- iOS源代码管理svn
01. SVN介绍 SVN 是集中式源代码管理工具 概念: 1> Repository 代码仓库,保存代码的仓库 2> Server 服务器,保存所有版本的代码仓库 3&g ...
- 页游手游服务器(二)c支持mysql
上一篇说的是liua的net拓展,这一篇说lua的sql拓展,准确说是mysql拓展,这里推荐下postgre,比mysql好用,支持数组,各种好,不过腾讯平台不支持,所以你的公司要和腾讯合作,掂量下 ...
- [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException] The child node "db_driver" at path "fos_user" must be configured.
$ php bin/console server:run [Symfony\Component\Config\Definition\Exception\InvalidConfigurationExce ...
- 第10条:尽量用enumerate取代range
核心要点: (1)enumerate函数提供了一种精简的写法,可以在遍历迭代器时获知每个元素的索引. (2)尽量用enumerate来改写那种将range与下标访问相结合的序列遍历代码. (3)可以给 ...
- spring ioc和aop理解
1.IOC 表示控制反转. 简单点说就是原来的对象是在要使用之前通过在代码里通过new Something()的方式创建出来的: IOC则是由spring容器创建同一创建,在程序要使用到该对象的时候, ...
- Scala window下安装
第一步:Java 设置 检测方法前文已说明,这里不再描述. 如果还为安装,可以参考我们的Java 开发环境配置. 接下来,我们可以从 Scala 官网地址 http://www.scala-lang. ...
- 每天一个Linux命令(17)whereis命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. (1)用法: 用法: w ...
- 【leetcode】Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...