跳到正文

个人记录

《CF1905E One-X》解题报告

daduoli约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年12月17日
收录
2026年2月4日
更新
2026年11月11日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

不会,我是小唐人。

一个结论:线段树中,每一个深度层中至多只有两种大小的节点。

具体怎么证明呢,有一个口糊,如果倘若某个节点比另一个和他同深度的要大 22 ,那么一定要匀一个给他。

然后对两种不同大小的讨论一下就好了

时间复杂度 O(Tlog⁡2n)O(T\log^2 n)

C++
#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const LL MAXN=1010,P=998244353;
LL n;
struct ddl {
	LL x,n,s;
	void clear() {
		x=n=s=0;
	}
	ddl() {
		clear();
	}
}a,b;
LL ans;
LL ksm(LL x,LL y) {
	LL ret=1;
	while(y) {
		if(y&1) ret=ret*x%P;
		x=x*x%P;
		y>>=1;
	}
	return ret;
}
LL fad(LL x,LL y) {
	return (x+y>=P?x+y-P:x+y);
}
LL cl(LL x,LL y) {
	return (ksm(2,x+y)-ksm(2,x)-ksm(2,y)+2*P+1)%P;
}
void solve(ddl a,ddl &b,ddl &c) {
	if(a.x<=1) {
		ans=fad(ans,a.s);
		return ;
	}
	LL rlen=a.x/2,llen=a.x-rlen;
	b.n=a.n;
	b.x=llen;
	b.s=a.s*2%P;
	c.n=a.n;
	c.x=rlen;
	c.s=(a.s*2%P+a.n)%P;

	ans=fad(ans,a.s*cl(llen,rlen)%P);
}
void insert(ddl c) {
	if(!c.x) return ;
	if(!a.x||c.x==a.x) {
		a.s+=c.s;
		a.n+=c.n;
		a.s%=P;
		a.x=c.x;
	}
	else {
		b.s+=c.s;
		b.n+=c.n;
		b.s%=P;
		b.x=c.x;
	}
}
void vmain() {
	scanf("%lld",&n);
	a.clear(),b.clear();
	a.x=n; a.n=1; a.s=1; ans=0;
	while(1) {
		if(!a.x&&!b.x) break;
		ddl ls1,ls2,ls3,ls4;
		solve(a,ls1,ls2);
//		cout<<ans<<' ';
		solve(b,ls3,ls4);
//		cout<<ans<<endl;
		a.clear(); b.clear();
		insert(ls1);
		insert(ls2);
		insert(ls3);
		insert(ls4);
		if(a.x>b.x) swap(a,b);
//		cout<<a.x<<' '<<a.s<<" "<<b.x<<' '<<b.s<<endl;
	}
	printf("%lld\n",ans);
}
int main () {
	int T;
	scanf("%d",&T);
	while(T--) vmain();
	return 0;
}
原文
存档评论1 条抓取于