高精求2^n

· · 个人记录

虽然高精求2^n人人都会,但我的代码效率应该算是很高了,2^100000秒出,2^1000000耗时20秒,2^10000000耗时40分钟。

#include <bits/stdc++.h>
using namespace std;
long long yaw=10;//压10位
long long base=pow(10,yaw);
long long a[1000010];
long long n;
long long siz=1;

void cheng(int o)
{
    long long r=0;
    for (int i=1;i<=siz;i++)
    {
        a[i]=a[i]*o+r;
        r=a[i]/base;
        a[i]%=base;
    }
    if(r>=1)
    {
        siz++;
        a[siz]=r;
    }
    return;
}

int main()
{
    a[1]=1;
    cin >> n;
    for (int i=1;i<=(n/25);i++)
    {
        cheng(33554432);
    }
    n%=25;
    for (int i=1;i<=n;i++)
    {
        cheng(2);
    }
    //cout << siz<<endl;
    cout <<a[siz];
    for (int i=siz-1;i>=1;i--)
    {
        cout << setw(yaw) << setfill('0')<<a[i];
    }
    return 0;
}

UPD at 2025.6.27

现在写了一个快很多的版本,2^10000000 只需要用 90s 就够了。

2^10000000 大概长这样:

90498173063608003013964026677087073562487623880180026759109215920533437487259295764175614344644597126202886784513490130340102063092398692355774391506733580370555102712985979389......014757522374377225812795708768144006461812307685186544238466810221372788000538124553867594784742485292176993914781862714107917533235555019174728608970564630687735669667838540318899783521847841664834323410018255732662370891387109376

顺便想了个弱智题:已知 x 是 a\times b \times c \times ... ,判断 x 是不是对的。

一会做一下

#include <bits/stdc++.h>
#define int long long
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
/*
NTT!!!

000
001
010
011
100
101
110
111

0 4 2 6 1 5 3 7
*/
int n,m;
const int N = (1<<23);
const int p = 998244353;
int a[N],b[N];
int c[N];

int fpm(int x,int y){
    int res = 1;
    while(y){
        if(y&1) res = (res*x)%p;
        x = (x*x)%p;
        y >>= 1;
    }
    return res;
}
void fft(int *a,bool on){
    for (int i=0;i<N;i++){
        if(c[i]<i) swap(a[i],a[c[i]]);
    }
    for (int s=1;s<=23;s++){
        int curn = (1<<s);
        int wn = fpm(3,(p-1)/curn);
        if(on) wn = fpm(wn,p-2)%p;
        for (int j=0;j<N;j+=curn){
            int w = 1;
            for (int k=0;k<curn/2;k++){
//              j+k,j+k+curn/2
                int X = (a[j+k]+a[j+k+curn/2]*w)%p;
                int Y = ((a[j+k]-a[j+k+curn/2]*w)%p+p)%p;
                w = (w*wn)%p;
                a[j+k] = X;
                a[j+k+curn/2] = Y;
            }
        }
    }
    if(on){
        int ni = fpm(N,p-2);
        for (int i=0;i<N;i++) a[i] = (a[i]*ni)%p;
    }
}
void jin(int *a){
    for (int i=0;i<N;i++){
        a[i+1] += a[i]/10;
        a[i] %= 10;
    }
}

signed main()
{
    freopen("final.txt","w",stdout);
    cout << "2^10000000=\n";
    for (int i=1;i<N;i++){
        c[i] = c[i>>1]>>1;
        if(i&1) c[i] |= (N>>1);
    }
    int n;
    cin >> n;
    a[0] = 2;
    b[0] = 1;
    while(n){
        cerr << n << "\n";
        if(n&1){
            fft(a,0);fft(b,0);
            for (int i=0;i<N;i++) b[i] = (b[i]*a[i])%p;
            fft(a,1);fft(b,1);
            jin(b);
        }
        fft(a,0);
        for (int i=0;i<N;i++) a[i] = (a[i]*a[i])%p;
        fft(a,1);
        jin(a);

        n >>= 1;
    }
    bool ok = 0;
    for (int i=N-1;i>=0;i--){
        if(b[i]!=0) ok = 1;
        if(ok) cout << b[i];
    }
    return 0;
}