跳转至

M - Sekiro

基本信息

题目出处2019 山东省大学生程序设计竞赛
队伍通过率291/307 (94.8%)

题解

题目求的是 n 连续进行 k 次除以 2 上取整的结果。

如果 n=0,显然答案就是 0。否则 n 将在 O(logn) 次操作之后变成 1,因此最多只要模拟 O(logn) 次操作即可。

复杂度 O(logn)

参考代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, K; scanf("%d%d", &n, &K);
    if (n == 0) printf("0\n");
    else {
        while (K > 0 && n > 1) K--, n = (n + 1) / 2;
        printf("%d\n", n);
    }
}

int main() {
    int tcase; scanf("%d", &tcase);
    while (tcase--) solve();
    return 0;
}