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

· · 题解

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

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

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

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

#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;
}