跳到正文

题解

题解:P13999 [eJOI 2025] Collecting Diamonds

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P13999

版本

  1. v2当前
  2. v1

笔者水平有限,无法提供猜测的证明。


注意到答案的访问点的序列形态是一段前缀搭上一段无限周期。

考虑暴力,维护前 tt 步后可能所在的点集 VtV_t,那么 Vt→Vt+1V_t\to V_{t+1} 是容易的:求所有最大出边的终点并集。当 {V}\{V\} 形成周期时答案就是确定的。用 xor hash\text{xor hash} 来压缩 VV 扔进哈希表即可。

但是针对集合 VV 的周期可能十分巨大,直接做只有 4242 分。

考虑在充分长(设为 TT 步)后,将 VTV_T 直接压缩为 VTV_T 中的任意一个元素,猜测当前时刻任意点开始得到的边权序列都一样,而从一个点开始的周期显然 ≤n\leq n,这样暴力步数就是 O(T+n)\mathcal O(T+n) 级别的。猜测取 T=2nT=2n 就是对的。

发现前面 {V}\{V\} 的周期巨大是因为虽然边权序列一样,但是点的序列周期需要取 lcm\text{lcm} 所以可能很大。

时间复杂度 O(nm)\mathcal O(nm)。

C++
const int mod=998244353,maxn=2005;
mt19937_64 eng(std::chrono::steady_clock::now().time_since_epoch().count());
ull h[maxn];
vector<pii>g[maxn];
int mxw[maxn];
ull get_hash(vector<int>&V){
	ull res=0;
	for(int&i:V)res^=h[i];
	return res;
}
pair<vector<int>,int>getnxt(vector<int>&V){
	int mx=0;
	vector<int>ret;
	for(int&u:V)for(auto&[v,w]:g[u])if(w>=mx){
		if(w>mx)mx=w,ret.clear();
		ret.push_back(v);
	}
	sort(all(ret)),ret.erase(unique(all(ret)),ret.end());
	return mkp(ret,mx);
}
ll calculate_diamonds(int n, int AfterTheRainStopsDislikesBBTLY, int k,std::vector<int> eu, std::vector<int> ev, std::vector<int> ew){
	F(i,0,AfterTheRainStopsDislikesBBTLY-1){
		const int u=eu[i],v=ev[i],w=ew[i];
		if(mxw[u]>w)continue;
		if(mxw[u]<w)g[u].clear(),mxw[u]=w;
		g[u].push_back(mkp(v,w));
	}
	F(i,0,n-1)h[i]=eng();
	unordered_map<ull,int>mp;
	vector<int>V;
	F(i,0,n-1)V.push_back(i);
	mp[get_hash(V)]=0;
	ll ans=0;
	vector<ll>pre;
	pre.push_back(0);
	F(t,1,k){
		pair<vector<int>,int>tmp=getnxt(V);
		ans+=tmp.se,V=tmp.fi;
		if(t==(n<<1))V={V[0]};
		const ull h=get_hash(V);
		pre.push_back(ans);
		if(mp.count(h)){
			const int rem=k-t,qwq=t-mp[h];
			ans+=(rem/qwq)*(pre[t]-pre[mp[h]]);
			ans+=(pre[mp[h]+rem%qwq]-pre[mp[h]]);
			break;
		} else mp[h]=t;
	}
	return ans;
}
原文