60分TLE求助大佬!!

P1045 [NOIP2003 普及组] 麦森数

BLX32M_10 @ 2021-07-23 13:37:58

rt

#include <cstdio>
#include <cmath>
int n, a[500] = {1};
int main() {

    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 500; j++) {
            a[j] *= 2;
        }
        for (int j = 0; j < 500; j++) {
            a[j + 1] += a[j] / 10;
            a[j] %= 10;
        }
    }

    a[0] -= 1;

    printf("%d", (int) (log10(2)*n) + 1);

    for (int i = 499; i >= 0; i--) {
        if ((i + 1) % 50 == 0) printf("\n");
        printf("%d", a[i]);
    }
    return 0;
}

by TulipeNoire @ 2021-08-01 16:33:10

快速幂?


by delic1ous @ 2021-08-10 19:42:09

时间复杂度太高(300w*500),建议循环时每次乘2^20,开long long的话可以乘2^59(结合压位更快),最优解是快速幂加压位


|