CF-Edu101-D-Ceil Divisions(构造)
题意: 给定 n n n 个数,从 1 1 1 到 n n n 。现在有一种操作,每一次可以选择两个数 x , y ( x ≠ y ) x,y \ \ (x \neq y) x,y (x=y) ,然后 a x = a x a y ( 向上取整 ) a_x = \frac{a_x}{a_y}(\text{向上取整}) ax=ayax(向上取整)。现在要将这 n n n 个数变成 n − 1 n-1 n−1 个 1 1 1 和 1 1 1 个 2 2 2。你最多有 n + 5 n + 5 n+5 次操作机会,该如何操作?
思路: 肯定是先利用最大的 n n n ,将其他非 1 1 1 非 2 2 2 的数变成 1 1 1 ,再最后通过 2 2 2 将 n n n 变成 1 1 1 。但是这里有个问题,如果用 2 2 2 来将 n n n 变成 1 1 1,最坏的情况要 18 18 18 次,再加上其他数字变成 1 1 1 的操作,直接就超出了上限了。于是换一下,不用 2 2 2 ,改用 8 8 8 来降 n n n,这时在极限情况是刚刚好 n + 5 n+5 n+5。然后就是模拟了。
代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#define fi first
#define se second
//#include<stdlib.h>
//#include <time.h>
//srand((unsigned)time(NULL));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-7;
const int N = 1e6 + 10;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
vector<pair<int, int>> ans;
if (n <= 8) {
for (int i = 3; i <= n - 1; i++) {
ans.push_back({i, n});
}
int now = n;
do {
int p = now / 2;
if (now % 2 > 0) p++;
ans.push_back({n, 2});
now = p;
} while (now != 1);
}
else {
for (int i = 3; i <= 7; i++) {
ans.push_back({i, n});
}
for (int i = 9; i <= n - 1; i++) {
ans.push_back({i, n});
}
int now = n;
do {
int p = now / 8;
if (now % 8 > 0) p++;
ans.push_back({n, 8});
now = p;
} while (now != 1);
ans.push_back({8, 2});
ans.push_back({8, 2});
ans.push_back({8, 2});
}
printf("%d\n", (int)ans.size());
for (int i = 0; i < (int)ans.size(); i++) {
printf("%d %d\n", ans[i].first, ans[i].second);
}
}
return 0;
}