跳到正文

题解

题解:P12572 [UOI 2023] An Array and Addition Again

Exoplanetary_Mirage约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月31日
收录
2026年8月5日
更新
2026年11月17日
篇幅
约 2 分钟
获赞
1
题目
P12572

版本

  1. v1当前

简单题,不知道为啥模拟赛一车人做这个做了 3h 左右,反正我 30min 做完了,甚至是第一遍把题读成 20002000 次操作能过于是写了一个错误代码的情况下。

考虑我们先对 i=99,98,⋯ ,1i=99,98,\cdots,1,做一遍操作,让 ai=1a_i=1。

那么我们现在要让 a1a_1 增加 n−1n-1,考虑递归构造,假设我们现在要把第 ii 个位置增加 xx:

  • 若 x≤2x\le 2,直接在 ii 做 xx 次操作。
  • 否则,如果 x mod 2=1x\bmod 2=1,在 ii 做一次操作,x←x−1x\gets x-1。
  • 递归的将 i+1i+1 个位置增加 x2\frac x2。
  • 在 ii 做两次操作。

次数跑下来平均在 250250 次左右,最坏次数应当为 100+3log⁡n100+3\log n,大概 280280 次,于是就做完了。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long

int n,a[105];
vector<int> res;

void calc(int x){
	res.push_back(x);
	a[x]+=a[x+1];
}

void slv(int n,int x){
	if(x<=2){
		for(int i=1;i<=x;i++) calc(n);
		return;
	}
	if(x&1) calc(n);
	slv(n+1,x/2-1);
	calc(n),calc(n);
}

void solve(){
	res.clear();
	cin>>n;
	for(int i=1;i<=100;i++) a[i]=0;
	a[100]=1;
	for(int i=99;i>=1;i--) calc(i);
	slv(1,n-1);
	cout<<res.size()<<endl;
	for(int i:res) cout<<i<<' ';
	cout<<endl;
}

signed main(){
//	freopen("construct.in","r",stdin);
//	freopen("construct.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);
	int T,_;cin>>T>>_;while(T--) solve();
	return 0;
}
原文