zoukankan      html  css  js  c++  java
  • 洛谷P4548 [CTSC2006]歌唱王国(概率生成函数)

    题面

    传送门

    给定一个长度为(L)的序列(A)。然后每次掷一个标有(1)(m)的公平骰子并将其上的数字加入到初始为空的序列(B)的末尾,如果序列B中已经出现了给定序列(A),即(A)(B)的子串,则停止,

    求序列(B)的期望长度。(L ≤ 10^5)

    题解

    不知道概率生成函数是什么的可以看看这篇文章,题解也在里面了

    //minamoto
    #include<bits/stdc++.h>
    #define R register
    #define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
    #define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
    #define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
    using namespace std;
    char buf[1<<21],*p1=buf,*p2=buf;
    inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
    int read(){
        R int res,f=1;R char ch;
        while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
        for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
        return res*f;
    }
    char sr[1<<21],z[20];int C=-1,Z=0;
    inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
    void print(R int x){
        if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
        while(z[++Z]=x%10+48,x/=10);
        while(sr[++C]=z[Z],--Z);sr[++C]='
    ';
    }
    const int N=1e5+5,P=1e4;
    inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
    inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
    int bin[N],kmp[N],a[N],n,p,res,pos;
    int main(){
    //	freopen("testdata.in","r",stdin);
    	p=read(),bin[0]=1;fp(i,1,1e5)bin[i]=mul(bin[i-1],p);
    	for(int T=read();T;--T){
    		n=read();fp(i,1,n)a[i]=read();
    		kmp[0]=kmp[1]=0;
    		for(R int i=2,j=0;i<=n;++i){
    			while(j&&a[j+1]!=a[i])j=kmp[j];
    			j+=(a[j+1]==a[i]),kmp[i]=j;
    		}
    		pos=n,res=0;
    		while(pos)res=add(res,bin[pos]),pos=kmp[pos];
    		printf("%04d
    ",res);
    	}
    	return 0;
    }
    
  • 相关阅读:
    笔记:多线程访问ConcurrentHashMap对key加锁
    根据第三列去重
    Correct the classpath of your application so that it contains a single, compatible version of org.apache.log4j.ConsoleAppender
    python 中将源配置为阿里
    criteria两个 判断
    git flow
    sqlmap用法详解
    MongoDB 入门
    MongoDB 手册
    OWASP TOP 10简单介绍
  • 原文地址:https://www.cnblogs.com/bztMinamoto/p/10560884.html
Copyright © 2011-2022 走看看