跳到正文

算法·理论

ABC310 做题笔记 | 作为没码力没脑力选手,我该怎么办

MatrixGroup约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 10 分钟

版本

  1. v1当前

E

考虑分治,每次考虑 [l,r][l,r] 内左端点 ≤m\le m,右端点 >m>m 的区间。

直接维护左边取值有多少个 0/1(通过计算每段“前加 1”“前加 0”和“不前加”的答案)代入到右边遍历即可。

F

考虑 dpi,maskdp_{i,mask} 表示前 ii 个 dice 目前能凑出(0-10)的数的状态为 mask 的概率。

G

码力做法:考虑对于每个基环树分别求值。赛后过了。

C++
ll qkpw(ll a,ll b)
{
	ll r=1;
	while(b)
	{
		if(b&1)r=r*a%mod1;a=a*a%mod1;b>>=1;
	}
	return r;
}
ll inv(ll x)
{
	return qkpw(x,mod1-2);
}
struct dsu{
	int p[200005];
	void init(int n)
	{
		rep1(i,n) p[i]=i;
	}
	int getp(int x)
	{
		return (p[x]==x)?x:(p[x]=getp(p[x]));
	}
	void link(int fr,int to)
	{
		p[getp(fr)]=getp(to);
	}
};
dsu Q;
int n;
ll k;
ll inv_k;
int a[200005];
ll b[200005];
int type[200005];
int dire[200005];
vector<int> fr[200005];
vector<int> tp[200005];
int deg[200005];
int ordr[200005];
int dis[200005];
ll subtr[200005];
ll resubtr[200005];
ll answer[200005];
int cycl[200005],cnt;
ll te[200005];
queue<int> q;
void solvecomp(int id)
{
//	cout<<"! Begin "<<id<<endl;
	for(auto v:tp[id])
	{
		if(dire[v]!=v) answer[v]=(resubtr[v]-b[v]+mod1)*inv_k%mod1;
		else q.push(v);
	}
//	cout<<"Completed."<<endl;
	cycl[++(cnt=0)]=q.front();ordr[cycl[1]]=1;
	while(a[cycl[cnt]]!=cycl[1])
	{
		cycl[cnt+1]=a[cycl[cnt]];++cnt;ordr[cycl[cnt]]=cnt;
	}
	for(int i=0;i<=cnt;++i)te[i]=0;
//	cout<<"Okay"<<endl;
	while(!q.empty())
	{
		int v=q.front();q.pop();
		for(auto u:fr[v])
		{
			if(dire[u]!=u)
			{
				dis[u]=dis[v]+1;q.push(u);
			}
		}
	}
//	cout<<"Finished."<<endl;
	rep1(i,cnt) te[i]=0;
	for(auto v:tp[id])
	{
		int D=dis[v],u=ordr[dire[v]];
	//	cout<<v<<" "<<"! "<<D<<" "<<dire[v]<<" "<<u<<"("<<cycl[u]<<")"<<endl;
		if(D==0)
		{
			ll K=k;
			te[1]=(te[1]+K/cnt%mod1*b[v])%mod1;
			u=u%cnt+1;
			D=int(K%cnt);
			if(u+D-1<=cnt)
			{
			//	cout<<u<<"+"<<" "<<u+D<<"-"<<endl;
				te[u]+=b[v];if(te[u]>=mod1)te[u]-=mod1;
				te[u+D]-=b[v];if(te[u+D]<0)te[u+D]+=mod1;
			}
			else
			{
		//		cout<<u<<"+"<<" 1+ "<<D+u-cnt<<"-"<<endl;
				te[u]+=b[v];if(te[u]>=mod1)te[u]-=mod1;
				// n-u+1 + D+u-n-1 = D
				te[1]+=b[v];if(te[1]>=mod1)te[1]-=mod1;
				te[D+u-cnt]-=b[v];if(te[D+u-cnt]<0)te[D+u-cnt]+=mod1;
			}
			// D begin with u
		}
		else
		{
			ll K=k-D+1;
			if(K<=0) continue;
			te[1]=(te[1]+K/cnt%mod1*b[v])%mod1;
			D=int(K%cnt);
			if(u+D-1<=cnt)
			{
		//		cout<<u<<"+"<<" "<<u+D<<"-"<<endl;
				te[u]+=b[v];if(te[u]>=mod1)te[u]-=mod1;
				te[u+D]-=b[v];if(te[u+D]<0)te[u+D]+=mod1;
			}
			else
			{
		//		cout<<u<<"+"<<" 1+ "<<D+u-cnt<<"-"<<endl;
				te[u]+=b[v];if(te[u]>=mod1)te[u]-=mod1;
				// n-u+1 + D+u-n-1 = D
				te[1]+=b[v];if(te[1]>=mod1)te[1]-=mod1;
				te[D+u-cnt]-=b[v];if(te[D+u-cnt]<0)te[D+u-cnt]+=mod1;
			}
			// D begin with u
		}
	}
//	cout<<"Frank!"<<endl;
	rep1(i,cnt)
	{
		te[i]+=te[i-1];if(te[i]>=mod1)te[i]-=mod1;
		answer[cycl[i]]=te[i]*inv_k%mod1;
	}
//	cout<<"! Done "<<id<<endl;
}
int fa[200005];
int re[200005];
int temp[200005];
void Multiply(int *p,int *qq)
{
	rep1(i,n) temp[i]=p[qq[i]];
	rep1(i,n) p[i]=temp[i];
}
void CalcKth()
{
	rep1(i,n) fa[i]=a[i];
	rep1(i,n) re[i]=i;
	ll K=k+1;
	while(K)
	{
		if(K&1) Multiply(re,fa);
		Multiply(fa,fa);K>>=1;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin>>n>>k;inv_k=inv(k%mod1);
	rep1(i,n)cin>>a[i];
	rep1(i,n)fr[a[i]].pb(i);
	rep1(i,n)deg[i]=int(fr[i].size());
	rep1(i,n)cin>>b[i];
	Q.init(n);
	rep1(i,n)
	{
		Q.link(i,a[i]);
	}
	rep1(i,n) tp[type[i]=Q.getp(i)].pb(i);
	Q.init(n);
	rep1(i,n) if(deg[i]==0) q.push(i);
	rep1(i,n) subtr[i]=b[i];
	while(!q.empty())
	{
		int v=q.front();q.pop();
		Q.link(v,a[v]);
	//	cout<<"linking "<<v<<" to "<<a[v]<<endl;
		subtr[a[v]]+=subtr[v];
		if(subtr[a[v]]>=mod1)subtr[a[v]]-=mod1;
		if((--deg[a[v]])==0)
		{
			q.push(a[v]);
		}
	}
//	cout<<"!12 "<<subtr[12]<<endl;
//	cout<<"!Done"<<endl;
	rep1(i,n) resubtr[i]=subtr[i];
	CalcKth();
	rep1(i,n)
	{
		resubtr[re[i]]-=subtr[i];
		if(resubtr[re[i]]<0)resubtr[re[i]]+=mod1;
	}
//	cout<<"!Done"<<endl;
	rep1(i,n) dire[i]=Q.getp(i);
//	cout<<"!!Done"<<endl;
	rep1(i,n) if(type[i]==i) solvecomp(i);
//	cout<<".Done"<<endl;
	rep1(i,n) cout<<answer[i]<<' ';
	return 0;
}

脑力做法:发现可以合并 ⟨a,b⟩\langle a,b\rangle,快速幂即可。(by zhuzhu2891)

C++
int n;ll k;
int a[200005];
ll b[200005];
ll b0[200005];
int c[200005];
ll d[200005];
int e[200005];
ll f[200005];
void multiply(int *a1,ll *b1,int *a2,ll *b2)
{
	rep1(i,n)
	{
		e[i]=a2[a1[i]];
		f[i]=b2[i];
	}
	rep1(i,n)
	{
		f[a2[i]]+=b1[i];if(f[a2[i]]>=mod1)f[a2[i]]-=mod1;
	}
	rep1(i,n)
	{
		a1[i]=e[i];b1[i]=f[i];
	}
}
ll qkpw(ll x,ll y)
{
	ll r=1;
	while(y)
	{
		if(y&1)r=r*x%mod1;x=x*x%mod1;y>>=1;
	}
	return r;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin>>n>>k;ll iv=qkpw(k%mod1,mod1-2);
	rep1(i,n)
	{
		cin>>a[i];c[i]=i;
	}
	rep1(i,n)
	{
		cin>>b[i];b0[i]=b[i];
	}
	++k;
	while(k)
	{
		if(k&1)multiply(c,d,a,b);
		multiply(a,b,a,b);
		k>>=1;
	}
	rep1(i,n) cout<<(d[i]-b0[i]+mod1)*iv%mod1<<' ';
	return 0;
}
原文