50分 超时求助

P1045 [NOIP2003 普及组] 麦森数

AVLw @ 2023-10-03 17:22:12

超了五秒多。。不知道为啥 是vector太慢了吗 那也不至于这么慢吧 加了注释求大佬解答

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include<vector>
using namespace std;
//高精度减法
vector<int> sub(vector<int>& a, vector<int>& b) {
    vector<int> c;
    for (int i = 0, t = 0; i < a.size(); i++) {
        t = a[i] - t;
        if (i < b.size())
            t -=b[i];
        c.push_back((t + 10) % 10);
        if (t < 0)
            t = 1;
        else
            t = 0;
    }
    while (c.size() > 1 && c.back() == 0)
        c.pop_back();
    return c;
}
//高精度乘法
vector<int> mul2(vector<int>& a, vector<int>& b) {
    vector<int>c(100010);
    for (int i = 0; i < a.size(); i++) {
        for (int j = 0; j < b.size(); j++) {
            c[i + j] += a[i] * b[j];
            c[i + j + 1] += c[i + j] / 10;
            c[i + j] %= 10;
        }
    }
    while (c.size() > 1 && c.back() == 0)
        c.pop_back();
    return c;
}
//高精度快速幂
vector<int> highPow(int n) {  //2^n  
    vector<int>ans = { 1 }; vector<int> t = { 2 };
    while (n) {
        if (n & 1) {
            ans = mul2(ans, t);  //ans*=t
        }
        t = mul2(t, t);   //t*=t
        n >>= 1;
    }
    return ans;
}
int main() {

    int p; scanf("%d", &p);
    vector<int>ans;
    ans = highPow(p);
    cout << ans.size() << endl;

    vector<int> tmp={1};
    vector<int>res;
    res=sub(ans, tmp);  //2^p-1
    res.resize(500);  //不够500位填充0
    for (int i = 499; i >=0; i--) {
        if (i != 499 && (i+1) % 50 == 0)
            cout << "\n" << res[i];
        else
            cout << res[i];
    }

    return 0;
}

by jzhjzh123 @ 2023-10-06 13:45:20

求幂时,一次乘2^30


by AVLw @ 2023-10-12 01:00:38

@jzhjzh123 什么意思 没懂求详解


by jzhjzh123 @ 2023-10-14 13:45:06

e...我那个方法好像有点问题,不过我的快速幂做法AC了,给你看一眼```cpp

include <iostream>

include <cstdio>

include <cstring>

include <cmath>

using namespace std;

define MAXNUM 1000

struct Bigint { int num[MAXNUM]; int len;

Bigint(int n = 0){
    len = 0;
    memset(num, 0, sizeof(num));
    while(n){
        num[len] = n%10;
        n /= 10;
        len ++;
    }
    len = 500;
}

int &operator[](int index){
    return num[index];
}

void zhanPing(){
    for(int i = 0;i < len;i ++){
        num[i+1] += num[i] / 10;
        num[i] %= 10;
    }
}

void print(){
    for(int i = len - 1;i >= 0;i --) cout << num[i];
}

void new_print(){
    for(int i = 0;i < 10;i ++){
        for(int j = 0;j < 50;j ++){
            cout << num[len-1-(i*50+j)];
        }
        printf("\n");
    }
}

};

Bigint operator(Bigint a, Bigint b){ Bigint c; for(int i = 0;i < a.len;i ++){ for(int j = 0;j < b.len;j ++) c[i+j] += a[i] b[j]; } c.zhanPing(); return c; }

Bigint pow2(int b){ Bigint chengShus[MAXNUM]; int i = 0; int powNum = b; Bigint chengShuNow(2); while(powNum){ if(powNum % 2){ chengShus[i++] = chengShuNow; powNum --; } else{ chengShuNow = chengShuNow chengShuNow; powNum /= 2; } } Bigint ans(1); while (i--) { ans = ans chengShus[i]; }

return ans;

}

int main(){ int p; cin >> p; double log2 = log10(2); cout << (int)(p * log2 + 1) << endl; Bigint ans(1);

ans = pow2(p);
ans[0] --;

ans.new_print();
return 0;

}


by jzhjzh123 @ 2023-10-14 13:45:47

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define MAXNUM 1000

struct Bigint {
    int num[MAXNUM];
    int len;

    Bigint(int n = 0){
        len = 0;
        memset(num, 0, sizeof(num));
        while(n){
            num[len] = n%10;
            n /= 10;
            len ++;
        }
        len = 500;
    }

    int &operator[](int index){
        return num[index];
    }

    void zhanPing(){
        for(int i = 0;i < len;i ++){
            num[i+1] += num[i] / 10;
            num[i] %= 10;
        }
    }

    void print(){
        for(int i = len - 1;i >= 0;i --) cout << num[i];
    }

    void new_print(){
        for(int i = 0;i < 10;i ++){
            for(int j = 0;j < 50;j ++){
                cout << num[len-1-(i*50+j)];
            }
            printf("\n");
        }
    }
};

Bigint operator*(Bigint a, Bigint b){
    Bigint c;
    for(int i = 0;i < a.len;i ++){
        for(int j = 0;j < b.len;j ++)
            c[i+j] += a[i] * b[j];
    }
    c.zhanPing();
    return c;
}

Bigint pow2(int b){
    Bigint chengShus[MAXNUM];
    int i = 0;
    int powNum = b;
    Bigint chengShuNow(2);
    while(powNum){
        if(powNum % 2){
            chengShus[i++] = chengShuNow;
            powNum --;
        }
        else{
            chengShuNow = chengShuNow * chengShuNow;
            powNum /= 2;
        }
    }
    Bigint ans(1);
    while (i--)
    {
        ans = ans * chengShus[i];
    }

    return ans;
}

int main(){
    int p;
    cin >> p;
    double log2 = log10(2);
    cout << (int)(p * log2 + 1) << endl;
    Bigint ans(1);

    ans = pow2(p);
    ans[0] --;

    ans.new_print();
    return 0;
}

by jzhjzh123 @ 2023-10-14 13:47:12

快速幂思路来自:快速幂


|