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之 ...
随机推荐
- Android 4.4(KitKat)中apk包的安装过程
原文地址:http://blog.csdn.net/jinzhuojun/article/details/25542011 事实上对于apk包的安装.4.4和之前版本号没大的区别. Android中a ...
- Android 禁止状态栏下拉
同学项目用到Android 禁止状态栏下拉,我也迷茫,网上很多资料都不行,最终找到了下面一篇博客,感觉很不错,说的比较详细,供大家参考了 http://blog.csdn.net/u011913612 ...
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
本文转载至 http://blog.csdn.net/allison162004/article/details/38756649 OS允许Objective-C 和 Core Foundation ...
- OKR与KPI管理的区别与联系
OKR是一种新兴的管理体系,最近几年被引进中国.由于在IT.互联网.金融.游戏等知识密集型企业中有着显著的效果,得到中国企业的认可. OKR是英文Objectives & Key Result ...
- django启动时报错:Apps aren't loaded yet.
1.解决方法 编辑manage.py文件,在文件顶部引入django模块.
- DDD开源框架
DDD开源框架: ABP ENODE https://github.com/VirtoCommerce/vc-community APWorks https://github.com/daxnet/B ...
- 11.Django数据库操作(查)
django.db.models.query.QuerySet1.可迭代2.可切片 官方文档:https://docs.djangoproject.com/en/1.9/ref/models/quer ...
- 第2条:遵循PEP8风格指南
<Python Enhancement Proposal #8>(8号Python增强提案)又叫PEP8,它是针对Python代码格式而编订的风格指南. 尽管可以在保证语法正确的前提下随意 ...
- 【整理学习Hadoop】Hadoop学习基础之一:服务器集群技术
服务器集群就是指将很多服务器集中起来一起进行同一种服务,在客户端看来就像是只有一个服务器.集群可以利用多个计算机进行并行计算从而获得很高的计算速度,也可以用多个计算机做备份,从而使得任 ...
- 七招从办公室政治中取胜 发表于 09 May 2008 ? 领导力培养
办公室政治,对有些人来说是一个禁忌词汇,但在工作场合它却不可回避.简单说来,它就是职场上人与人的不同:观念的差异.利益的冲突 都可以看成是办公室政治的表现.它等于人与人之间的交流和关系.没必要害怕办公 ...