【BJOI2019 Day1】简要题解

T1:

传送门

很显然是要在AcAc自动机上dpdp
一个显然的dpdpf[i][j][k]f[i][j][k]表示前ii个字符,当前在自动机的点jj,已经有kk个咒语的最大价值

但是由于咒语最多有O(n2)O(n^2)个复杂度太差了
由于maxvc\max \sqrt[c]{\prod v}不好处理
考虑转成对数
就变成maxvc\max \frac{\sum v}{c}

就是一个显然的分数规划了
复杂度O(nslogv)O(ns*logv)
不过很卡精度,开大会wawa,开小也会wawa
调了十多发才过

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=1505;
cs double eps=5e-6;
int n,m;
double v[N],c[N];
char t[N];
namespace Ac{
	int nxt[N][11],fail[N],tot,ed[N];
	double f[N][N],val[N];pii pre[N][N];
	cs double inf=-1e18;
	queue<int> q;
	vector<int> e[N];
	int pos[N],idx[N],dfn,mxpos;
	inline void insert(char *s,int id){
		int p=0;
		for(int i=0,len=strlen(s);i<len;i++){
			int c=s[i]-'0';
			if(!nxt[p][c])nxt[p][c]=++tot;
			p=nxt[p][c];
		}
		ed[id]=p;
	}
	void dfs(int u){
		if(u)pos[u]=++dfn,idx[dfn]=u;
		for(int &v:e[u])
		dfs(v);
	}
	inline void buildfail(){
		for(int i=0;i<10;i++){
			int v=nxt[0][i];
			if(v)fail[v]=0,q.push(v);
		}
		while(!q.empty()){
			int p=q.front();q.pop();
			for(int c=0;c<10;c++){
				int v=nxt[p][c];
				if(!v)nxt[p][c]=nxt[fail[p]][c];
				else fail[v]=nxt[fail[p]][c],q.push(v);
			}
		}
		for(int i=1;i<=tot;i++)e[fail[i]].pb(i);
		dfs(0);
	}
	inline void dp(){
		for(int i=1,len=strlen(t+1);i<=len;i++){
			if(t[i]=='.'){
				for(int c=0;c<10;c++)
				for(int p=0;p<=tot;p++)if(f[i-1][p]>inf){
					int v=nxt[p][c];
					double now=f[i-1][p]+val[v];
					if(now>f[i][v])f[i][v]=now,pre[i][v]=pii(p,c);
				}
			}
			else{
				int c=t[i]-'0';
				for(int p=0;p<=tot;p++)if(f[i-1][p]>inf){
					int v=nxt[p][c];
					double now=f[i-1][p]+val[v];
					if(now>f[i][v])f[i][v]=now,pre[i][v]=pii(p,c);
				}
			}
		}
	}
	inline bool check(double k){
		for(int i=1;i<=tot;i++)val[i]=0;
		for(int i=1;i<=m;i++)val[ed[i]]+=c[i]-k;
		for(int i=1;i<=tot;i++)
			val[idx[i]]+=val[fail[idx[i]]];
		int len=strlen(t+1);
		for(int j=0;j<=len;j++)for(int i=0;i<=tot;i++)f[j][i]=inf;
		f[0][0]=0;
		dp();
		double mx=inf;mxpos=0;
		for(int i=0;i<=tot;i++)
			if(f[len][i]>mx)mx=f[len][i],mxpos=i;
		return mx>eps;
	}
	int ans[N];
	inline void write(int pp,int u){
		if(!pp)return;
		pii now=pre[pp][u];
		ans[pp]=now.se;
		write(pp-1,now.fi);
	}
	inline void solve(){
		double l=0,r=0;
		for(int i=1;i<=m;i++)c[i]=log(v[i]),r=max(r,c[i]);
		while(l+eps<r){
			double mid=(l+r)/2;
			if(check(mid))l=mid;
			else r=mid;
		}
		int len=strlen(t+1);
		write(len,mxpos);
		for(int i=1;i<=len;i++)cout<<ans[i];
	}
}
char s[N];
int main(){
	n=read(),m=read();
	scanf("%s",t+1);
	for(int i=1;i<=m;i++){
		scanf("%s",s);
		Ac::insert(s,i),v[i]=read();
	}
	Ac::buildfail();
	Ac::solve();
}

T2:

f[i]f[i]表示长度为ii的方案数

首先对于m=2m=2的情况
可以很显然发现dpdp式是f[i]=f[i1]+f[i2]f[i]=f[i-1]+f[i-2]
其实就是斐波那契数列

所以实际上求的是i=lr(Fik)\sum_{i=l}^r{F_i\choose k}
考虑化简式子
ans=1k!n=lrFnkans=\frac{1}{k!}\sum_{n=l}^{r}F_n^{\underline k}
=1k!n=lri=0k(1)kis(k,i)Fni=\frac 1{k!}\sum_{n=l}^{r}\sum_{i=0}^{k}(-1)^{k-i}s(k,i)F_n^i

考虑斐波那契数列通项公式
A=15,B=A,x=(1+52),y=152A=\frac 1{\sqrt5},B=-A,x=(\frac{1+\sqrt 5}{2}),y=\frac{1-\sqrt 5}{2}
那么Fi=Axi+ByiF_i=Ax^i+By^i

ans=1k!n=lri=0ks(k,i)(1)ki(Axn+Byn)ians=\frac 1{k!}\sum_{n=l}^{r}\sum_{i=0}^{k}s(k,i)(-1)^{k-i}(Ax^n+By^n)^i
=1k!n=lri=0ks(k,i)(1)kij=0i(ij)AjBijxnjyn(ij)=\frac 1{k!}\sum_{n=l}^{r}\sum_{i=0}^{k}s(k,i)(-1)^{k-i}\sum_{j=0}^{i}{i\choose j}A^jB^{i-j}x^{nj}y^{n(i-j)}
=1k!i=0ks(k,i)(1)kij=0i(ij)AjBijn=lr(xjyij)n=\frac 1{k!}\sum_{i=0}^{k}s(k,i)(-1)^{k-i}\sum_{j=0}^{i}{i\choose j}A^jB^{i-j}\sum_{n=l}^r(x^jy^{i-j})^n

后面是一个等比数列,前面直接k2k^2枚举
构造一个模意义下a+b5a+b\sqrt 5这样的复数就可以做了

至于m=3m=3的时候
手玩一下可以发现递推式
ii为奇数答案为00
否则
f[i]=3f[i2]+2f[i4]+2f[i6]+2f[i8]....f[i]=3*f[i-2]+2*f[i-4]+2*f[i-6]+2*f[i-8]....

f[i]=4f[i2]f[i4]f[i]=4*f[i-2]-f[i-4]
除以2之后就相当于是f[i]=4f[i1]f[i2]f[i]=4*f[i-1]-f[i-2]
解一下特征方程算出来A=3+36,B=336,x=(2+3),y=(23)A=\frac{3+\sqrt 3}{6},B=\frac{3-\sqrt 3}{6},x=(2+\sqrt 3),y=(2-\sqrt 3)

然后就和上面一样了

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define int long long
#define cs const
#define bg begin
const int mod=998244353,G=3;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b%mod;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
int T;
cs int inv2=Inv(2),inv6=Inv(6);
ll I;
struct plx{
	int x,y;
	plx(int _x=0,int _y=0):x(_x),y(_y){}
	friend inline plx operator +(cs plx &a,cs plx &b){
		return plx(add(a.x,b.x),add(a.y,b.y));
	}
	friend inline plx operator -(cs plx &a,cs plx &b){
		return plx(dec(a.x,b.x),dec(a.y,b.y));
	}
	friend inline plx operator *(cs plx &a,cs plx &b){
		return plx((1ll*a.x*b.x+I*a.y*b.y)%mod,(1ll*a.x*b.y+1ll*a.y*b.x)%mod);
	}
	friend inline plx operator +(cs plx &a,cs int &b){
		return plx(add(a.x,b),a.y);
	}
	friend inline plx operator -(cs plx &a,cs int &b){
		return plx(dec(a.x,b),a.y);
	}
	friend inline plx operator *(cs plx &a,cs int &b){
		return plx(mul(a.x,b),mul(a.y,b));
	}
};
inline plx pksm(plx a,ll b){
	plx res(1,0);
	for(;b;b>>=1,a=a*a)if(b&1)res=res*a;
	return res;
}
inline plx pInv(plx x){
	return plx(x.x,mod-x.y)*Inv(((1ll*x.x*x.x-I*x.y*x.y)%mod+mod)%mod);
}
plx X,Y,A,B;
cs int N=555;
ll l,r,L,R;
int k,s[N][N],ifac[N],fac[N];
inline int C(int n,int m){
	return n<m?0:mul(fac[n],mul(ifac[m],ifac[n-m]));
}
inline void init(){
	fac[0]=ifac[0]=1;
	for(int i=1;i<N;i++)fac[i]=mul(fac[i-1],i);
	ifac[N-1]=Inv(fac[N-1]);
	for(int i=N-2;i;i--)ifac[i]=mul(ifac[i+1],i+1);
	s[0][0]=1;
	for(int i=1;i<N;i++)
	for(int j=1;j<N;j++)
	s[i][j]=add(s[i-1][j-1],mul(s[i-1][j],i-1));
}
inline void init1(){
	I=5;
	X=plx(inv2,inv2),Y=plx(inv2,mod-inv2),A=pInv(plx(0,1)),B=pInv(plx(0,mod-1));
}
inline void init2(){
	I=3;
	X=plx(2,1),Y=plx(2,mod-1),A=plx(inv2,inv6),B=plx(inv2,mod-inv6);
}
inline void solve(){
	k=read();
	int ans=0;
	for(int j=0;j<=k;j++){
		int coef=((k-j)&1)?mod-s[k][j]:s[k][j];
		int res=0;
		for(int t=0;t<=j;t++){
			plx now=pksm(A,t)*pksm(B,j-t)*C(j,t),tmp=pksm(X,t)*pksm(Y,j-t);
			if(tmp.x==1&&tmp.y==0)tmp=plx((r-l+1)%mod,0);
			else tmp=(pksm(tmp,r+1)-pksm(tmp,l))*pInv(tmp-1);
			Add(res,(now*tmp).x);
		}
		Add(ans,mul(res,coef));
	}
	cout<<mul(Inv((R-L+1)%mod),mul(ifac[k],ans))<<'\n';
}
signed main(){
	T=read();int tp=read();
	init();
	if(tp==2)init1();
	else init2();
	while(T--){
		scanf("%lld%lld",&L,&R);
		if(tp==2)l=L+1,r=R+1;
		else l=(L+1)>>1,r=R>>1;
		solve();
	}
}

T3:

AkAk

出题人:这道题其实不难,只需要把几个细节想清楚就可以了

咕咕咕

posted @ 2019-09-05 14:22  Stargazer_cykoi  阅读(127)  评论(0编辑  收藏  举报