UVa 11384

题目

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2379


题意

[1, .., n],每次可以减一个1-n之间的数,问至少多少次能将全部数字减为0(减为0后不再变化)

思路

如刘书思路。

想到1...n,就会想到树状数组分bit存储的思想,进而就会觉得分bit减是一个想法。因为log2(n) <= (x - 1)logx(n), 此处x大于等于3,所以可以认为答案就是结果的bit长度。

感想

1. 三倍ice cream!

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <tuple>
#include <set>
#include <map>
#include <cassert>
#define LOCAL_DEBUG
using namespace std;

int mylog(int n) {
    int x, bit;
    x = 1;
    bit = 0;
    while (x <= n) {
        x <<= 1;
        bit++;
    }
    return bit;
}

int main() {
#ifdef LOCAL_DEBUG
    freopen("input.txt", "r", stdin);
    //freopen("output2.txt", "w", stdout);
#endif // LOCAL_DEBUG
    int n;
    for (int ti = 1; scanf("%d", &n) == 1; ti++) {
        printf("%d
", mylog(n));
    }

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xuesu/p/10370996.html