HRBUST 2039 麦森数

题目链接:https://vjudge.net/problem/HRBUST-2039

  这题其实是一道数学题,首先我们要知道计算位数的话一种是循环计数,还有一种是用对数计数求以进制数为底n的对数再加1就是所求的数字的位数。
然后题目要计算的是2^p-1的位数,即(lg2 ^p-1)-1, 因为2的幂都是以2的倍数(2, 4, 6, 8)结尾的,所以其实计算(lg2^p) + 1就行了,然后我们用一个简单的变形, 变为(plg2) + 1就行了。

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a))
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll ll_INF = 233333333333333;
const int maxn = 1e4 + 10;
int main(void) {
    ll p;
    while(~scanf("%lld", &p))
        printf("%lld\n", (ll)(p*log10(2)+eps+1)); //因为log10()函数返回的是double类型,所以我们加上eps弥补一些误差
    return 0;
}
原文地址:https://www.cnblogs.com/shuitiangong/p/12266519.html