链接

You have an array a 1 , a 2 , … , a n a_1,a_2,…,a_n a1​,a2​,…,an​ where a i = i a_i=i ai​=i.

In one step, you can choose two indices x x x and y y y ( x ≠ y ) (x≠y) (x​=y) and set a x = ⌈ a x a y ⌉ a_x=⌈\frac{a_x}{a_y}⌉ ax​=⌈ay​ax​​⌉ (ceiling function).

Your goal is to make array a a a consist of n − 1 n−1 n−1 ones and 1 1 1 two in no more than n + 5 n+5 n+5 steps. Note that you don’t have to minimize the number of steps.

Input

The first line contains a single integer t ( 1 ≤ t ≤ 1000 ) t (1≤t≤1000) t(1≤t≤1000) — the number of test cases.

The first and only line of each test case contains the single integer n ( 3 ≤ n ≤ 2 ⋅ 105 ) n (3≤n≤2⋅105) n(3≤n≤2⋅105) — the length of array a a a.

It’s guaranteed that the sum of n over test cases doesn’t exceed 2 ⋅ 105 2⋅105 2⋅105.

Output

For each test case, print the sequence of operations that will make a as n − 1 n−1 n−1 ones and 1 1 1 two in the following format: firstly, print one integer m ( m ≤ n + 5 ) m (m≤n+5) m(m≤n+5) — the number of operations; next print m pairs of integers x x x and y y y ( 1 ≤ x , y ≤ n ; x ≠ y ) (1≤x,y≤n; x≠y) (1≤x,y≤n;x​=y) ( x x x may be greater or less than y y y) — the indices of the corresponding operation.

It can be proven that for the given constraints it’s always possible to find a correct sequence of operations.

Example

input

2
3
4

output

2
3 2
3 2
3
3 4
4 2
4 2

Note
In the first test case, you have array a = [ 1 , 2 , 3 ] a=[1,2,3] a=[1,2,3]. For example, you can do the following:

choose 3 , 2 3, 2 3,2: a 3 = ⌈ a 3 a 2 ⌉ = 2 a_3=⌈\frac{a_3}{a_2}⌉=2 a3​=⌈a2​a3​​⌉=2 and array a = [ 1 , 2 , 2 ] a=[1,2,2] a=[1,2,2];
choose 3 , 2 3, 2 3,2: a 3 = ⌈ 2 2 ⌉ = 1 a_3=⌈\frac22⌉=1 a3​=⌈22​⌉=1 and array a = [ 1 , 2 , 1 ] a=[1,2,1] a=[1,2,1].
You’ve got array with 2 2 2 ones and 1 1 1 two in 2 2 2 steps.
In the second test case, a = [ 1 , 2 , 3 , 4 ] a=[1,2,3,4] a=[1,2,3,4]. For example, you can do the following:

choose 3 , 4 3, 4 3,4: a 3 = ⌈ 3 4 ⌉ = 1 a_3=⌈\frac34⌉=1 a3​=⌈43​⌉=1 and array a = [ 1 , 2 , 1 , 4 ] a=[1,2,1,4] a=[1,2,1,4];
choose 4 , 2 4, 2 4,2: a 4 = ⌈ 4 2 ⌉ = 2 a_4=⌈\frac42⌉=2 a4​=⌈24​⌉=2 and array a = [ 1 , 2 , 1 , 2 ] a=[1,2,1,2] a=[1,2,1,2];
choose 4 , 2 4, 2 4,2: a 4 = ⌈ 2 2 ⌉ = 1 a_4=⌈\frac22⌉=1 a4​=⌈22​⌉=1 and array a = [ 1 , 2 , 1 , 1 ] a=[1,2,1,1] a=[1,2,1,1].

思路一

选择一个 x ( x < n ) x~(x<n) x (x<n),将 [ 2 , n − 1 ] [2,n-1] [2,n−1] 中除了 x x x 之外的每个数都与 n n n 进行操作,变为 1 1 1,总共需要操作 n − 3 n-3 n−3 次。 之后在 8 8 8 次操作之内,将 x x x 与 n n n 变为 1 1 1、 2 2 2。

这个思路是否可行取决于 x x x 的选取,若 x x x 取 2 2 2,那么它只能解决 n < = 2 8 n<=2^8 n<=28 的情况; x x x 取 4 4 4 的话,若 n = 1024 n=1024 n=1024 ,那么最终只能变为 1 、 4 1、4 1、4两个数;选 n \sqrt n n ​ 也不行。

首先发现,要使得操作次数尽可能小,这两个数不能大小相近。并且,一次操作后,得到的两个数同样不能大小相近。那么,设 x = n y x=n^y x=ny ,一次操作后 n n n 变为 n 1 − y n^{1-y} n1−y,设操作前后两个数字比例相同:
1 y = y 1 − y \frac1y=\frac{y}{1-y} y1​=1−yy​
解得: y = 5 − 1 2 y=\frac{\sqrt 5-1}{2} y=25 ​−1​.

所以, x x x 取 n 5 − 1 2 n^{\frac{\sqrt 5-1}{2}} n25 ​−1​ 时,可以使用较小的操作数使得 x x x 与 n n n 尽快减小。
例如, n = 2 × 1 0 5 n=2\times 10^5 n=2×105 时,选 x = c e i l ( n 5 − 1 2 ) = 3420 x=ceil(~n^{\frac{\sqrt 5-1}{2}}~)=3420 x=ceil( n25 ​−1​ )=3420,这对数的变化如下:

{ 200000 , 3420 } = > { 59 , 3420 } = > { 59 , 6 } = > { 10 , 6 } = > { 2 , 6 } = > { 2 , 3 } = > { 2 , 1 } \{200000,3420\}=>\{59,3420\}=>\{59,6\}=>\{10,6\}=>\{2,6\}=>\{2,3\}=>\{2,1\} {200000,3420}=>{59,3420}=>{59,6}=>{10,6}=>{2,6}=>{2,3}=>{2,1}

在 8 8 8 次操作之内可实现。

但是,有时也会失败,因为 c e i l ceil ceil 函数的影响,还是会使得这对数无法保持比例,例如:

{ 19 , 7 } = > { 3 , 7 } = > { 3 , 3 } = > { 1 , 3 } \{19,7\}=>\{3,7\}=>\{3,3\}=>\{1,3\} {19,7}=>{3,7}=>{3,3}=>{1,3}

所以把黄金分割周围的 10 10 10 个数字都试一遍,哪个可行选哪个。

#include<bits/stdc++.h>
using namespace std;
int T,n,op[100][2],tot;bool calc(int x,int y){tot=0;int a=x,b=y;while(a+b!=3){if(a==b&&a!=2) return false;if(a>b){op[tot][0]=x,op[tot++][1]=y;a=ceil(1.0*a/b);}else{op[tot][0]=y,op[tot++][1]=x;b=ceil(1.0*b/a);}}return tot<=8;
}void solve(){cin>>n;int x=ceil(pow(n,2.0/(1+sqrt(5))));for(int i=max(x-5,2);i<=min(x+5,n-1);i++)if(calc(n,i)){ x=i; break; } cout<<n-3+tot<<"\n";for(int i=2;i<n;i++) if(i!=x)cout<<i<<" "<<n<<"\n";for(int i=0;i<tot;i++) cout<<op[i][0]<<" "<<op[i][1]<<"\n";
}int main(){ios::sync_with_stdio(false);for(cin>>T;T;T--) solve();
}

方法二 STD

采用了递归的方法。取 x = c e i l ( n ) x=ceil(~\sqrt n~) x=ceil( n ​ ),那么可以把原序列分为左右两段,对于右边一段 [ x + 1 , r ] [x+1,r] [x+1,r],除了 r r r 之外的每个数字都与 r r r 进行操作,那么一次操作即可变为 1 1 1 ,至于 r r r ,它与 x x x 进行两次操作必然会变为 1 1 1,对 [ l , m i d ] [l,mid] [l,mid] 进行递归。

总操作数为 n − 2 + ( s e g m e n t − 1 ) n-2+(segment-1) n−2+(segment−1) , s e g m e n t segment segment 是段的数量,当 n = 2 × 1 0 5 n=2\times 10^5 n=2×105 时, s e g m e n t = 6 < 8 segment=6<8 segment=6<8 .

#include<bits/stdc++.h>
using namespace std;
int T,n,tn;void divi(int l,int r){if(r<=2) return;int mid=ceil(sqrt(r));for(int i=mid+1;i<r;i++) cout<<i<<" "<<r<<"\n";for(int i=0;i<2;i++) cout<<r<<" "<<mid<<"\n";divi(l,mid);
}void solve(){int op=0; cin>>n,tn=n;while(tn>2) op++,tn=ceil(sqrt(tn));cout<<n-2+op<<"\n";divi(1,n);
}int main(){ios::sync_with_stdio(false);for(cin>>T;T;T--) solve();
}

Ceil Divisions(递归,黄金分割应用)相关推荐

  1. D. Ceil Divisions

    D. Ceil Divisions 题意: a[i] = i 一共有n个数字, 操作 最多操作n+5次,让数组a 变成一个2 和剩下全部是1 题解:首先我们如果要最后单独处理n的话一定会大于n+5次的 ...

  2. CodeForces - 1469D - Ceil Divisions (思维+数学)

    Ceil Divisions 题意 对于一个大小为 n n n 的排列 在一次操作中可以选择两个数 a x a_x ax​ 和 a y a_y ay​ ( x ≠ y ) (x≠y) (x​=y) ...

  3. Codeforce D. Ceil Divisions (构造+思维)

    题目链接 题意: 给你一个长度为 \(n\) 的序列 \(a\) 满足 \(a_i=i\) 你每次可以进行一次如下操作: 选择两个数 \(a_x,a_y\),将 \(a_x\) 修改为 \(\lcei ...

  4. codeforces1469D Ceil Divisions(构造题、规律题)

    传送门 题解:其实就是发现规律来构造的题目,题目要 n + 5 n+5 n+5步构造完成,那么必然有很多操作是第 i i i个数除以第 i + 1 i+1 i+1个数,可以发现一个大数要想降下来,最少 ...

  5. Educational Codeforces Round 101 (Rated for Div. 2) D. Ceil Divisions(思维)

    给出数 n ,数组 a 为 {1,2,3,--n},最多有 n+5 次机会,将任意两个不相等的数做  ceil(x/y) 运算,使得最后的数组 a 只有一个 2,其余全为 1,给出运算次数具体的运算步 ...

  6. Educational Codeforces Round 101 (Rated for Div. 2) D. Ceil Divisions 思维 + 根号数

    传送门 题意: 给一个数组ai=ia_i=iai​=i,每次可以进行操作ax=⌈axay⌉a_x=\left \lceil \frac{a_x}{a_y} \right \rceilax​=⌈ay​a ...

  7. Educational Codeforces Round 101 D. Ceil Divisions(构造)

    LINK 考虑 1 , 2 1,2 1,2不去管,问题变成花 n + 5 n+5 n+5步操作使剩下的 [ 3 , n ] [3,n] [3,n]都变成 1 1 1 考虑选定一个数 x x x 让 [ ...

  8. D. Ceil Divisions(思维+根号构造)

    https://codeforces.com/problemset/problem/1469/D 思路: 直接一个个用大的去除最后不满足n+5. 再用大的除的过程中当碰到i*i<=n的时候,就可 ...

  9. codeforces1469D. Ceil Divisions

    https://codeforces.com/contest/1469/problem/D 这题观察发现一个比较优的策略 我们选择一个基数c,然后让n/(n/c), (n/c)/(n/c/c).... ...

最新文章

  1. 关于扫描仪——你不知道的秘密
  2. 使用fliter实现ie下css中rgba的效果
  3. java getattribute intvalue_Java NodeTree.getAttributeI方法代码示例
  4. 函数exit()详解:参数EXIT_FAILURE(是1),EXIT_SUCCESS(是0)
  5. 如何更改自己电脑上的COM端口号
  6. .NET 和 Mono 的一点历史
  7. python基本数据类型float_Python基本数据类型
  8. 完了!CPU 一味求快出事儿了!| 原力计划
  9. java中spring的注解_Java代码中spring注解浅析
  10. 网吧显示最近使用计算机,影子系统怎么用?实现像网吧电脑一样重启后自动还原系统教程...
  11. matlab多元函数拟合,只有数据,不知道函数形式,未知函数关系的多元函数拟合
  12. access vba代码大全_这本VBA经典图书终于做活动了,还是5折!
  13. 【TS中的面向对象】
  14. CentOS系统安装(7.8.2003)
  15. 通过XtraBackup进行数据库表备份和表空间传输实例
  16. [软件人生]抢钱的电影与现在的软件开发
  17. 【华为机试真题详解】欢乐的周末
  18. 中证指数公司调整指数样本股定期更换时间
  19. 计算机网络3 数据链路层
  20. 【HNU小学期硬件实训】基于QT上位机的汽车监控警报系统

热门文章

  1. 男女通用,关于爱情的70句哲理
  2. 43 errors and 3 warnings potentially fixable with the `--fix` option.
  3. 简单封装XMLHttpRequest
  4. linux下结束后台进程的命令
  5. Win10的两个实用技巧系列之华硕电脑设置面部识别的技巧、删除背景图片的方法
  6. 如何解决微信屏蔽二维码下载APK 手机APP
  7. 密码学--文件安全传输
  8. 什么是AOP?AOP面向切面编程
  9. 蓝牙打印 设置打印样式_GitHub - shen173869710/PrintUtils: Android蓝牙打印机,带你真正了解各种打印格式。...
  10. 只有本科学历的传奇数学家去世了:他打开了通往费马大定理的大门