BZOJ3457 : Ring
根据Polya定理:
\[ans=\frac{\sum_{d|n}\varphi(d)cal(\frac{n}{d})}{n}\]
其中$cal(n)$表示长度为$n$的无限循环后包含$S$的串的数量。
对于$cal(n)$的计算,考虑用总方案数$2^n$减去单次循环内不包含$S$的方案数。
枚举进入循环时与$S$的KMP指针$k$,然后设$f[i][j]$表示考虑前$i$个位置,KMP指针为$j$的方案数,最终结果为$f[n][k]$。
转移可以用矩阵$G$表示,预处理出$G$的$2^0,2^1,...,2^{\log n}$次方,那么每次计算$cal(n)$只需要进行$O(k\log n)$次$O(k^2)$的矩阵乘向量。
总时间复杂度$O(d(n)k^3\log n)$。
#include<cstdio>
#define rep(i) for(int i=0;i<m;i++)
const int N=35,P=1000000007;
int n,m,i,j,k,nxt[N],g[N][2],e[N][N][N],c[N][N],f[N],h[N],ans;char a[N];
inline void mul(int a[][N],int f[][N]){
rep(i)rep(j)c[i][j]=0;
rep(i)rep(j)if(a[i][j])rep(k)if(a[j][k])c[i][k]=(1LL*a[i][j]*a[j][k]+c[i][k])%P;
rep(i)rep(j)f[i][j]=c[i][j];
}
inline void mulv(int a[][N]){
rep(i){
h[i]=0;
rep(j)if(f[j]&&a[i][j])h[i]=(1LL*f[j]*a[i][j]+h[i])%P;
}
rep(i)f[i]=h[i];
}
inline int phi(int n){
int t=1;
for(int i=2;i<=n/i;i++)if(n%i==0){
t*=i-1,n/=i;
while(n%i==0)t*=i,n/=i;
}
if(n>1)t*=n-1;
return t;
}
inline int po(int a,int b){int t=1;for(;b;b>>=1,a=1LL*a*a%P)if(b&1)t=1LL*t*a%P;return t;}
inline int cal(int n){
int t=0;
for(int i=0;i<m;i++){
rep(j)f[j]=i==j;
for(int j=0;(1LL<<j)<=n;j++)if(n>>j&1)mulv(e[j]);
t=(t+f[i])%P;
}
return(po(2,n)-t+P)%P;
}
int main(){
scanf("%d%d%s",&n,&m,a+1);
for(i=1;i<=m;i++)a[i]=a[i]=='R';
for(i=2;i<=m;nxt[i++]=j){
while(j&&a[j+1]!=a[i])j=nxt[j];
if(a[j+1]==a[i])j++;
}
rep(i)for(j=0;j<2;j++){
for(k=i;k&&a[k+1]!=j;k=nxt[k]);
if(a[k+1]==j)k++;
g[i][j]=k;
}
rep(i)for(j=0;j<2;j++)e[0][g[i][j]][i]++;
for(i=1;(1LL<<i)<=n;i++)mul(e[i-1],e[i]);
for(i=1;i<=n/i;i++)if(n%i==0){
ans=(1LL*phi(i)*cal(n/i)+ans)%P;
if(i*i!=n)ans=(1LL*phi(n/i)*cal(i)+ans)%P;
}
ans=1LL*ans*po(n,P-2)%P;
return printf("%d",ans),0;
}
BZOJ3457 : Ring的更多相关文章
- 一点公益商城开发系统模式Ring Buffer+
一个队列如果只生产不消费肯定不行的,那么如何及时消费Ring Buffer的数据呢?简单的方案就是当Ring Buffer"写满"的时候一次性将数据"消费"掉. ...
- OpenCASCADE Ring Type Spring Modeling
OpenCASCADE Ring Type Spring Modeling eryar@163.com Abstract. The general method to directly create ...
- 使用Ring Buffer构建高性能的文件写入程序
最近常收到SOD框架的朋友报告的SOD的SQL日志功能报错:文件句柄丢失.经过分析得知,这些朋友使用SOD框架开发了访问量比较大的系统,由于忘记关闭SQL日志功能所以出现了很高频率的日志写入操作,从而 ...
- [AlwaysOn Availability Groups]AlwaysOn Ring Buffers
AlwaysOn Ring Buffers 一些AlwaysOn的诊断信息可以从SQL Server ring buffers.或者从sys.dm_os_ring_buffers.ring buffe ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- Ring buffers and queues
Ring buffers and queues The data structure is extremely simple: a bounded FIFO. One step up from pla ...
- Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- hdoj 1016 Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
随机推荐
- 在windows下Apache安装配置
安装,从官网下载,安装即可. 配置遇到一些问题: 1. the requested operation has failed 这是因为安装后的文件目录没有没有写的权限.通过安全设置安装目录的所有 ...
- 常见的爬虫分析库(3)-Python正则表达式与re模块
在线正则表达式测试 http://tool.oschina.net/regex/ 常见匹配模式 模式 描述 \w 匹配字母数字及下划线 \W 匹配非字母数字下划线 \s 匹配任意空白字符,等价于 [\ ...
- google 插件
鼠标手势 crxMouse Chrome™ Gestures Google翻译 json格式化 JSONView github展开文件夹 Octotree axure 原型 Axur ...
- Spring Boot JPA的Column Table 注解命名字段无效
@Table(name = "OrderInfo") @Entity public class OrderInfo { @Id @GeneratedValue private Lo ...
- 真的分治fft
以前学的分治fft f[i]=sigma(f[i-x]*g[x]),其中g[x]已知 那么我们可以用cdq分治来做(l,mid 对mid+1,t的影响) 而现在的$f[i]=sum(f(i-x)*f( ...
- [WC2014]紫荆花之恋
题解: 首先考虑点分治 dis(i,u)+dis(i,v)<=value[u]+value[v] 移项就很容易发现用平衡树可以很简单的维护这个东西 但是有重复,需要在下一层的每个平衡树内减去这个 ...
- 搭建自己的docker仓库
https://docs.docker.com/registry/deploying/#run-a-local-registry https://docs.docker.com/registry/in ...
- Codeforces 380D Sereja and Cinema (看题解)
Sereja and Cinema 首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边. 难点在于计算方案数.. 我们可以从外往里把确定的人用组合数算上去,然后缩小范围. #include& ...
- AtCoder Grand Contest 017D (AGC017D) Game on Tree 博弈
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC017D.html 题目传送门 - AGC017D 题意 给定一棵 n 个节点的以节点 1 为根的树. 两个 ...
- .bat批处理添加Python任务
一.常用命令含义 例一.多进程python 任务 -- start 命令 @echo offstart python C:\Users\ntitled\n\update_restt\test_ba ...