CF-Edu101-D-Ceil Divisions(构造)

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 n1 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值