各位大佬帮我看看,40分

P1045 [NOIP2003 普及组] 麦森数

jh2023 @ 2024-07-14 23:29:14

各位大佬帮我看看,40分

#include<bits/stdc++.h>
using namespace std;

string multiplication(string aa, string bb) {
    long long a[100010];
    long long b[100010];
    long long r[100010];
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(r, 0, sizeof(r));
    for (long long i = aa.length() - 1; i >= 0; i--) {
        a[aa.length() - i] = aa[i] - '0';
    }
    for (long long i = bb.length() - 1; i >= 0; i--) {
        b[bb.length() - i] = bb[i] - '0';
    }

    for (long long i = 1; i <= aa.length(); i++) {
        long long x = 0;
        for (long long j = 1; j <= bb.length(); j++) {
            r[i + j - 1] = a[i] * b[j] + x + r[i + j - 1];
            x = r[i + j - 1] / 10;
            r[i + j - 1] %= 10;
        }
        r[bb.length() + i] = x;
    }
    long long len = aa.length() + bb.length();
    while (r[len] == 0 && len > 1) {
        len--;
    }
    string res = "";
    for (long long i = len; i >= 1; i--) {
        res += to_string(r[i]);
    }
    return res;
}

string subtraction(string aa, string bb) {
    long long a[100010];
    long long b[100010];
    long long r[100010];
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(r, 0, sizeof(r));
    char flag = '+';
    if (aa.size() < bb.size() || aa.size() == bb.size() && aa < bb) {
        flag = '-';
        swap(aa, bb);
    }
    for (long long i = aa.length(); i >= 0; i--) {
        a[aa.length() - i] = aa[i] - '0';
    }
    for (long long i = bb.length(); i >= 0; i--) {
        b[bb.length() - i] = bb[i] - '0';
    }

    for (long long i = 1; i <= aa.length(); i++) {
        if (a[i] < b[i]) {
            a[i] += 10;
            a[i + 1] -= 1;
        }
        r[i] = a[i] - b[i];
    }
    long long len = aa.length();
    while (r[len] == 0 && len > 1) {
        len--;
    }
    string res = "";
    if (flag == '-') {
        res += '-';
    }
    for (long long i = len; i >= 1; i--) {
        res += to_string(r[i]);
    }
    return res;
}

char s[100010];
int main() {
    long long p;
    cin >> p;
    string res = "1";
    for (long long i = 1; i <= p; i++) {
        res = multiplication("2", res);
    }
    res = subtraction(res, "1");
    cout << res.length() << endl;
    memset(s, '0', sizeof(s));
    for (long long i = res.length() - 1; i >= 0; i--) {
        s[res.length() - i] = res[i];
    }
    for (long long i = 500; i >= 1; i--) {
        cout << s[i];
        if ((i - 1) % 50 == 0) {
            cout << endl;
        }
    }
    return 0;
}

|