P1045麦森数

P1045麦森数

#include<iostream>
#include <cmath>
#include <cstring>

const int maxn = 1e5 + 10;
#define int long long
using namespace std;

//第一个表示位数,之后的数字表示每个位数上的数
int ans[maxn] = {1, 1}, k[maxn] = {1, 2}, c[maxn];

void muti(int a[], int b[]) {
    memset(c, 0, sizeof(c));
    c[0] = a[0] + b[0];
    if (c[0] > 500) c[0] = 500;
    for (int i = 1; i <= b[0]; i++) {//模拟乘法 32 * 23
//          3 2
//        * 2 3
//        ———————
//          9  6
//       6  4
//       ————————
//       7  3  6
        for (int j = 1; j <= a[0]; j++) {
            c[j + i - 1] += b[i] * a[j];
            if (c[j + i - 1] > 9) {
                c[i + j] += c[i + j - 1] / 10;
                c[i + j - 1] %= 10;
            }
        }
    }
    for (int i = 0; i <= c[0]; i++) a[i] = c[i];
}

void quick(int x)//快速幂
{
    while (x) {
        if (x & 1) muti(ans, k);
        muti(k, k);
        x >>= 1;
    }
}

signed main() {
    int x;
    cin >> x;
    cout << (int) (x * log10(2) + 1);
    quick(x);
    ans[1]--;//-1操作
    for (int i = 500; i >= 1; i--) {
        if (i % 50 == 0) cout << endl;
        cout << ans[i];
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xcfxcf/p/12301608.html