跳到正文

题解

题解:CF1469D Ceil Divisions

coding_goat约 5 分钟
原文

这是 2025年9月3日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

纪念一下秒了的构造。

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

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

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