高精度快速幂
2024-02-12 20:48:43
发布于:湖南
25阅读
0回复
0点赞
注意看题目,输出10行,每行输出50位
#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
using namespace std;
short a[1005], b[1005], c[1005];
void chengfa(short *a, short *b){
for(int i = 1; i <= 500; i++){
for(int j = 1; j <= 500; j++){
int weishu = i + j - 1;
c[weishu] += a[i] * b[j];
c[weishu + 1] += c[weishu] / 10;
c[weishu] %= 10;
}
}
}
void chengfang(int n){
while(n){
if(n % 2){
chengfa(a, b);
for(int i = 0; i <= 500; i++){
b[i] = c[i];
}
memset(c, 0, sizeof(c));
}chengfa(a, a);
for(int i = 0; i <= 500; i++) a[i] = c[i];
memset(c, 0, sizeof(c));
n /= 2;
}
}
int main(){
int n;
cin >> n;
a[1] = 2, b[1] = 1;
int ws = n * log10(2) + 1;
cout << ws << endl;
chengfang(n);
b[1]--;
for(int i = 10; i >= 1; i--){
for(int j = 50 * i; j >= 50 * i - 49; j--){
cout << b[j];
}cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个