ACdream 1116 扩展KMP 线段树区间累加 Fib数列的矩阵加速
2015-01-28 19:46
309 查看
传送门:点击打开链接
SubmitStatisticNext
Problem
n is the length of the string
f() is the function of fibonacci, f(0) = 0, f(1) = 1...
a[i] is the total number of times any prefix appear in the suffix s[i....n-1].
(the prefix means s[0...i] )
each test case will give you a string consists of lowercase letters, the length of which is no more than 100000.
对于
aa这个后缀,前缀a出现了两次,前缀aa出现了一次,总共三次
对于a这个后缀,前缀a出现了一次
所以答案是f(3) + f(1)
题意:给出一个长度小于10W的字符串。用a[I]表示所以前缀在后缀s[I..n]中出现的次数。求
的值 mod 1000000007,f()为fib数列。
思路:求出字符串每一位的exnext值。(exnext的第一位在这道题强行定位s的长度)。用线段树维护a[I]的值,假设next[I]=k。则在后缀1到后缀I中,都出现了前缀1到前缀k。所以将区间[1,I]都加上exnext[I]。最后将a[I]都导出导到一个数组里面(不导出也可以)方便后面的计算。由于a[ii[]很大,可能超过int。所以求f(a[I])必须使用矩阵加速。
代码:
Gao the string!
[align=center]Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)[/align]SubmitStatisticNext
Problem
Problem Description
give you a string, please output the result of the following function mod 1000000007n is the length of the string
f() is the function of fibonacci, f(0) = 0, f(1) = 1...
a[i] is the total number of times any prefix appear in the suffix s[i....n-1].
(the prefix means s[0...i] )
Input
multiple test cases.each test case will give you a string consists of lowercase letters, the length of which is no more than 100000.
Output
ouput a number.Sample Input
aa
Sample Output
3
Hint
样例解释如下:对于
aa这个后缀,前缀a出现了两次,前缀aa出现了一次,总共三次
对于a这个后缀,前缀a出现了一次
所以答案是f(3) + f(1)
Source
wuyiqi题意:给出一个长度小于10W的字符串。用a[I]表示所以前缀在后缀s[I..n]中出现的次数。求
的值 mod 1000000007,f()为fib数列。
思路:求出字符串每一位的exnext值。(exnext的第一位在这道题强行定位s的长度)。用线段树维护a[I]的值,假设next[I]=k。则在后缀1到后缀I中,都出现了前缀1到前缀k。所以将区间[1,I]都加上exnext[I]。最后将a[I]都导出导到一个数组里面(不导出也可以)方便后面的计算。由于a[ii[]很大,可能超过int。所以求f(a[I])必须使用矩阵加速。
代码:
#include<cstdio> #include<cstring> #include<iostream> #define LL long long #define maxn 100005 #define MOD 1000000007 using namespace std; int exnext[maxn]; void getexnext(char s[]) { int len = strlen(s); exnext[0] = 0; int k, j, i; i = j = 0; for (k = 1; k < len; k++) { if (k + exnext[k - i] > j) { i = k; while (j < len&&s[j - i + 1] == s[j + 1]) j++; exnext[k] = j - k + 1; if (j < k) j = k; } else exnext[k] = k + exnext[k - i] - 1 < len ? exnext[k - i] : len - k + 1; } } struct node { int l,r; LL add; } tree[maxn<<2]; void build(int id,int l,int r) { tree[id].l=l; tree[id].r=r; tree[id].add=0; if(l==r) return; int mid=(l+r)>>1; build(id<<1,l,mid); build(id<<1|1,mid+1,r); } void op(int id,int l,int r,int add) { if(l<=tree[id].l&&tree[id].r<=r) tree[id].add+=add; else { int mid=(tree[id].l+tree[id].r)>>1; if(l<=mid) op(id<<1,l,r,add); if(mid<r) op(id<<1|1,l,r,add); } } void pushdown(int id) { tree[id<<1].add+=tree[id].add; tree[id<<1|1].add+=tree[id].add; } LL Ans[maxn]; void out(int id) { if(tree[id].l==tree[id].r) Ans[tree[id].l]=tree[id].add; else { pushdown(id); out(id<<1); out(id<<1|1); } } struct matrix { LL a[2][2]; }; matrix mul(matrix& x,matrix& y) { matrix ans; ans.a[0][0]=x.a[0][0]*y.a[0][0]+x.a[0][1]*y.a[1][0]; ans.a[0][0]%=MOD; ans.a[0][1]=x.a[0][0]*y.a[0][1]+x.a[0][1]*y.a[1][1]; ans.a[0][1]%=MOD; ans.a[1][0]=x.a[1][0]*y.a[0][0]+x.a[1][1]*y.a[1][0]; ans.a[1][0]%=MOD; ans.a[1][1]=x.a[1][0]*y.a[0][1]+x.a[1][1]*y.a[1][1]; ans.a[1][1]%=MOD; return ans; } matrix power(matrix x,long e) { matrix ans,tmp; if(e==0) { ans.a[0][0]=1; ans.a[0][1]=0; ans.a[1][0]=0; ans.a[1][1]=1; return ans; } if( e==1 ) return x; tmp=power(x,e>>1); ans=mul(tmp,tmp); if( e&1 ) ans=mul(ans,x); return ans; } LL fic(int n) { matrix result = {{1,0,0,1}}; matrix base = {{1,1,1,0}}; if(n&1) result = base; n>>=1; while(n) { base=mul(base, base); if(n&1) { result=mul(result, base); } n>>=1; } return result.a[0][1]; } char s[maxn]; int main() { while(~scanf("%s",s)) { getexnext(s); int len=strlen(s); LL ans=0; exnext[0]=len; build(1,0,len-1); for(int i=0;i<len;i++) op(1,0,i,exnext[i]); out(1); for(int i=0;i<len;i++) ans=(ans+fic(Ans[i]))%MOD; printf("%lld\n",ans); } return 0; }
相关文章推荐
- acdream 1116 Gao the string! (扩展kmp,dp思想,矩阵优化)
- 矩阵乘法加速fib数列
- “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】
- 矩阵快速幂/矩阵加速线性数列 By cellur925
- HDU - 4348 To the moon (可持久化线段树,区间查询加累加)
- 矩阵加速数列递推
- 洛谷 P1939 【模板】矩阵加速(数列):优化递推式的方法——矩阵快速幂
- hdu1556 color the ball 树状数组区间更新单点查询(附线段树做法)与二维扩展
- HDU6155(dp + 线段树区间更新 + 矩阵性质)
- HDU 1540(线段树,以一点扩展开的最大连续区间)
- P1939 【模板】矩阵加速(数列)
- hdu 1116 敌兵布阵 线段树 区间求和 单点更新
- hihoCoder - 1116 - 计算 (线段树区间合并)
- [bzoj5118]Fib数列2_费马小定理_矩阵乘法
- UVA 10689 Yet another Number Sequence(矩阵快速幂求Fib数列)
- luogu P1939 【模板】矩阵加速(数列)
- 洛谷1939 【模板】矩阵加速(数列)
- UVA 10689 Yet another Number Sequence(矩阵快速幂求Fib数列)
- HDU 4893(Wow! Such Sequence!-线段树单点修改+区间求和+改为最近Fib数)
- hihocoder 1116 : 计算(线段树 区间合并)