跟Vresion 1一样,就是加了高精度。表示高精度还是不熟。。。
My Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1000;
void add(int a[], int b[], int res[]){
int i;
for(i = N; i >= 0; i--){
res[i] = a[i] + b[i];
}
for(i = N; i >= 0; i--){
if(res[i] > 9){
res[i-1] += res[i]/10;
res[i] %= 10;
}
}
}
void mult(int a[], int k, int res[]){
int i;
for(i = N; i >= 0; i--){
//printf("%d %d %d\n", a[i], k, res[i]);
res[i] = a[i] * k;
//printf("%d", res[i]);
}
for(i = N; i >= 0; i--){
if(res[i] > 9){
res[i-1] += res[i]/10;
res[i] %= 10;
}
}
}
void print(int res[]){
int i = 0;
while(res[i] == 0) i++;
for(; i <= N; i++){
printf("%d", res[i]);
}
printf("\n");
}
int main(){
//freopen("data.in", "r", stdin);
int ans[181][N+1], tmp[N+1];
int n, k, i;
while(~scanf("%d%d", &n, &k)){
memset(ans, 0, sizeof(ans));
ans[0][N] = k-1;
ans[1][N] = k*(k-1);
for(i = 2; i < n; i++){
memset(tmp, 0, sizeof(tmp));
add(ans[i-1], ans[i-2], tmp);
mult(tmp, k-1, ans[i]);
}
print(ans[n-1]);
}
return 0;
}