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题)的更多相关文章

  1. 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 ...

  2. ACM学习历程—HDU 5326 Work(树形递推)

    Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...

  3. 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 ...

  4. 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 ...

  5. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

  6. ACM学习历程—HDU1023 Train Problem II(递推 && 大数)

    Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know  ...

  7. ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

  8. 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 ...

  9. HDU 5459 Jesus Is Here (递推,组合数学)

    有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...

随机推荐

  1. java服务安装(一):使用java service wrapper及maven打zip包

    目录(?)[+] 1概述 1_1为什么要用服务形式运行 1_2如何让java程序以服务形式运行 1_3打包需求 2程序示例 3maven打zip包 3_1maven-assembly-plugin介绍 ...

  2. iOS 添加震动效果

    开发过程中,有时候会碰到点击按钮或者某个动画会配合震动效果:下面介绍iOS开发过程中的震动添加: 导入:#import <AudioToolbox/AudioToolbox.h> 在需要出 ...

  3. python爬虫之Selenium

    Selenium的使用 #!/usr/bin/env python # -*- coding:utf-8 -*- """ Selenium是一个第三方模块,可以完全模拟用 ...

  4. Unicode Category

    Lu(letter,uppercase):大写字母. Ll(letter.lowercase):小写字母. Lt(letter,titlecase):词首字母大写的字母. Lm(letter,modi ...

  5. centos install docker setup centos7 安装docker

    centos7 安装docker 1: 安装必要的一些系统工具sudo yum install -y yum-utils device-mapper-persistent-data lvm2 2: 添 ...

  6. docker centos yum 源

    aliyun yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.rep ...

  7. Java InetAddress.getByAddress()的使用

    import java.net.*; public class NetDemo { public static void main(String[] args) throws Exception{ S ...

  8. HTTPS与HTTP

    HTTP HyperText Transfer Protocol超文本传输协议 HTTPS HyperText Transfer Protocol over Secure Socket Layer 基 ...

  9. 剑指offer——树中两个节点的最低公共祖先

    代码来源与<剑指offer> 得到从根节点开始到输入的两个结点的两条,需要遍历两次树,每遍历一次的时间复杂度是O(n),得到的两条路径的长度在最差情况时是O(n),通常情况下两条路径的长度 ...

  10. 暑假集训第一周比赛G题

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83146#problem/G G - 向 Crawling in process... C ...