题解:CF1469D Ceil Divisions

· · 题解

纪念一下秒了的构造。

首先我们发现要把一个数 x 变为 1 就不得不寻找一个大于等于该数的数 y。我们因此有一个简单的想法,将最后一个数作为 y 和前面的 [3,n-1] 的数进行操作,一共 n-3 次操作,使得最后变成 1,2,\dots,1,1,n。但是此时我们再消除 n 便达到了 \log_2 n 级别的,当 n 为 2\times 10^5 时需要 18 次才能解决。这使得我们不得不想一点更优的方法。

我们发现对于一个数 x 只需要和 \lceil\sqrt{x}\rceil 分别作为 x,y 进行操作即可在两次将 x 消除。因此我们从 n 开始寻找对于该数的 \lceil\sqrt{x}\rceil 即可,最后一定会变成 2。那么这个会比刚刚的更优吗?我们来算算。2^{2^{5}}>2\times 10^5,因此 \lceil\sqrt{x}\rceil 只会有最多 5 个,那么最坏情况会是 n-3+5\times 2-4=n+3<n+5。(-4 是因为有四个原本要和最后一个匹配但是现在不用了)。

#include<bits/stdc++.h>

#define pii pair<int,int> 
#define pll pair<long long,long long> 
#define ll long long
#define i128 __int128

#define mem(a,b) memset((a),(b),sizeof(a))
#define m0(a) memset((a),0,sizeof(a))
#define m1(a) memset(a,-1,sizeof(a))
#define lb(x) ((x)&-(x))
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)
#define pb(G,x) (G).push_back((x))
#define For(a,b,c) for(int a=(b);a<=(c);a++)
#define Rep(a,b,c) for(int a=(b);a>=(c);a--)
#define in1(a) a=read()
#define in2(a,b) a=read(), b=read()
#define in3(a,b,c) a=read(), b=read(), c=read()
#define in4(a,b,c,d) a=read(), b=read(), c=read(), d=read()
#define fst first 
#define scd second 
#define dbg puts("IAKIOI")

using namespace std;

int read() {
    int x=0,f=1; char c=getchar();
    for(;c<'0'||c>'9';c=getchar()) f=(c=='-'?-1:1); 
    for(;c<='9'&&c>='0';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
    return x*f;
}
void write(int x) { if(x>=10) write(x/10); putchar('0'+x%10); }

const int mod = 998244353;
int qpo(int a,int b) {int res=1; for(;b;b>>=1,a=(a*a)%mod) if(b&1) res=res*a%mod; return res; }
int inv(int a) {return qpo(a,mod-2); }

#define maxn 200050

int n;
map<int,int> mp;
vector<int> a;
pii ans[maxn]; int top;

void work() {
    in1(n);
    mp.clear(); a.clear();
    int x=n; mp[x]=1;
    while(x>2) {
        int qwq=sqrt(x);
        if(qwq*qwq<x) qwq++;
        x=qwq; mp[x]=1; a.push_back(x);
    }
//  for(auto x:a) cout<<"!!!"<<x<<'\n';
    top=0;
    For(i,2,n) if(!mp[i]) ans[++top]={i,n};
    int lst=n;
    for(auto x:a) {
        ans[++top]={lst,x};
        ans[++top]={lst,x};
        lst=x;
    }
    cout<<top<<'\n';
    For(i,1,top) cout<<ans[i].first<<' '<<ans[i].second<<'\n';
}

signed main() {
//  freopen("data.in","r",stdin);
//  freopen("myans.out","w",stdout);
//  ios::sync_with_stdio(false); 
//  cin.tie(0); cout.tie(0);
    double stt=clock();
    int _=1;
    _=read();
//  cin>>_;
    For(i,1,_) {
        work();
    }
    cerr<<"\nTotal Time is:"<<(clock()-stt)*1.0/1000<<" second(s)."<<'\n';
    return 0;
}