此代码可求d的n次方(忽视我数组的奇怪命名,如果数很大,请自行调整大小和类型)
#include<iostream>
#include<string>
using namespace std;
int answer[1000000];
int pre[500000],next[500000];
void print(int a[]){
int i;
for(i = a[0]; i >= 1; --i){
cout << a[i];
}
cout << endl;
return ;
}
void reset(int a[]){
int i;
for(i = 1; i <= a[0]; ++i)a[i] = 0;
return ;
}
void copy(int a[],int b[]){
int i;
for(i = 0; i <= a[0]; ++i)b[i] = a[i];
return ;
}
void multiply(int pre[],int next[]){
int i,j;
for(i = 1; i <= pre[0]; ++i){
for(j = 1; j <= next[0]; ++j){
answer[i+j-1] += pre[i]*next[j];
}
}
for(i = 1; i <= pre[0]+next[0]; ++i){
answer[i+1] += answer[i] / 10;
answer[i] %= 10;
}
int ans = pre[0] + next[0];
while(!answer[ans])ans--;
answer[0] = ans;
copy(answer,pre);
reset(answer);
}
void multiplyself(int next[]){
int i,j;
for(i = 1; i <= next[0]; ++i){
for(j = 1; j <= next[0]; ++j){
answer[i+j-1] += next[i]*next[j];
}
}
for(i = 1; i <= 2 * next[0]; ++i){
answer[i+1] += answer[i] / 10;
answer[i] %= 10;
}
int ans = 2 * next[0];
while(!answer[ans])ans--;
answer[0] = ans;
copy(answer,next);
reset(answer);
}
void init(int d){
pre[0] = 1;
pre[1] = 1;
next[1] = d;
int ans = 0;
while(d){
ans++;
d /= 10;
}
next[0] = ans;
}
int main(){
int d;
cin >> d;
init(d);
int n;
cin >> n;
while(n){
if(int(n & 1)){
multiply(pre,next);
}
n >>= 1;
multiplyself(next);
}
print(pre);
return 0;
}