You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], al;
int b[N], bl;
void mul(int a[], int &al, int b[], int bl) {
int c[N] = {0}, cl = al + bl;
for (int i = 1; i <= al; i++)
for (int j = 1; j <= bl; j++)
c[i + j - 1] += a[i] * b[j];
int t = 0;
for (int i = 1; i <= al + bl; i++) {
t += c[i];
c[i] = t % 10;
t /= 10;
}
memcpy(a, c, sizeof c);
al = cl;
//前导0
while (al > 1 && a[al] == 0) al--;
}
//快速幂+高精度 x^k
void qmi(int x, int k) {
a[++al] = 1, b[++bl] = x; // 2 ^100 b[1]=2
while (k) {
if (k & 1) mul(a, al, b, bl);
k >>= 1;
mul(b, bl, b, bl);
}
}
int main() {
int k = 100; // k=100 2^k
// 1、利用高精度计算2^p位数 暴力法
a[++al] = 1, b[++bl] = 2;
for (int i = 1; i <= k; i++) mul(a, al, b, bl); // O(N)
printf("%d\n", al);
for (int i = al; i; i--) printf("%d", a[i]);
puts("");
// 2、利用高精度+快速幂来计算2^p位数
al = 0, bl = 0;
// memset(a, 0, sizeof a);
// memset(b, 0, sizeof b);
// 64 32 16 8 4 2 1
// 1 1 0 0 1 0 0
qmi(2, k); // log2(100)=6.6=7
printf("%d\n", al); // O(log2(N)≈lg(N)=2)
for (int i = al; i; i--) printf("%d", a[i]);
puts("");
// 3、利用对数公式计算2^k位数
// k*log10(2)+1
printf("%d\n", int(100 * log10(2) + 1)); // O(1)
return 0;
}