输出最后500位数字:快速幂
输出位数:p*log10(2)+1
#include <iostream>
#include <cmath>
using namespace std;
int a[1001], b[1001];
void mult(int* x, int* y)
{
int t[1001];
for (int i = 0; i < 1000; i++)
t[i] = 0;
for (int i = 0; i < 500; i++)
{
for (int j = 0; j < 500; j++)
{
t[j + i] += x[i] * y[j];
t[j + i + 1] += t[j + i] / 10;
t[j + i] = t[j + i] % 10;
}
}
for (int i = 0; i < 500; i++)
x[i] = t[i];
}
int main()
{
a[0] = 1, b[0] = 2;
int p;
cin >> p;
int num = p * log10(2) + 1;
while (p)
{
if (p & 1)
{
mult(a, b);
}
mult(b, b);
p >>= 1;
}
a[0]--;
cout << num;
for (int i = 500; i > 0; i--)
{
if (i % 50 == 0) cout << endl;
cout << a[i - 1];
}
}