Codeforces B. Captain Flint and a Long Voyage (思维 / 二进制) (Round #660 Div.2)

传送门

题意: 找到最小的n位十进制数,使得起每位数的二进制拼接在一起形成的长二进制串删除末尾n位数后剩下的数max。
在这里插入图片描述
思路:

  • 这场比赛我当时没有参加,还一直口胡误导队友,啊啊啊!太菜了!
  • 要想剩下的数最大,那么就得要求每位数的二进制位数最高(就像二进制100绝对比11大),而不难发现0~9中只有8和9是4位的二进制数。
  • 要想剩下的数最大,不仅需要最高位,也需要低位来凑。所以只要不在后n位范围内的地方都可是9,而在后n位中不管是9还是8都会被去掉。
  • 但我们需要的是最小的n位十进制数,当然就选择最小的8啦。所以,十进制后面为8的应该是 ⌈n/4⌉。

代码实现:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int t, n;

signed main()
{
    IOS;

    cin >> t;
    while(t --){
        cin >> n;
        int t = ceil(n/4.0);
        for(int i = 0; i < n-t; i ++) cout << 9;
        for(int i = 0; i < t; i ++) cout << 8;
        cout << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Satur9/article/details/107888454
今日推荐