Files
python/TangDou/LuoGuBook/P1045_01.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

54 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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 = min(500, 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() {
//计算 2^p-1的值
int p;
cin >> p;
//利用快速幂计算2^p
qmi(2, p);
//最后一位减去一个1因为2^p最后一位肯定不是0所以减1不会产生借位直接减去即可
a[1]--;
//一共多少位
printf("%d\n", (int)(p * log10(2) + 1));
for (int i = 500; i; i--) {
printf("%d", a[i]);
//该换行了,就是到了第二行的行首
if ((i - 1) % 50 == 0) puts("");
}
return 0;
}