Ural_1036. Lucky Tickets(DP + 高精度)

  纠结的高精度,终于拿下了。。。转移方程很简单 dp[i][j] = dp[i-1][j-k] {k| 0, 1, 2 .... , 9},高精度整了一下午。悲剧,各种wa。。。重新敲了一遍,终于过了。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 550;
const int M = 50;
const int inf = 10000;

int dp[M+5][N][M+5];

void add(int a[M+1], int b[M+1]){
int i;
for(i = 1; i <= M; i++){
a[i] += b[i];
}
for(i = 1; i <= M; i++){
a[i+1] += a[i]/inf;
a[i] %= inf;
}
}

void mul(int a[M+1], int b[M+1]){
int c[2*M+1], i, j;
memset(c, 0, sizeof(c));
for(i = 1; i <= M; i++)
for(j = 1; j <= M; j++)
c[i+j-1] += a[i]*b[j];
for(i = 1; i <= M; i++){
c[i+1] += c[i]/inf;
c[i] %= inf;
}
for(i = 1; i <= M; i++)
a[i] = c[i];
}

void display(int a[M+1]){
int j, i = M;
while(a[i] == 0 && i > 0) i--;
if(i == 0) printf("0\n");
else{
printf("%d", a[i]);
for(j = i-1; j > 0; j--){
printf("%04d", a[j]);
}
}
cout << endl;
}
int main(){
//freopen("data.in", "r", stdin);

int n, s, i, j, k;
while(~scanf("%d%d", &n, &s)){
if(s&1){
printf("0\n");
continue;
} else {
memset(dp, 0, sizeof(dp));

for(i = 0; i < 10; i++)
dp[1][i][1] = 1;

for(i = 1; i <= n; i++)
for(j = 0; j <= s/2; j++)
for(k = 0; k <= 9 && j >= k; k++)
add(dp[i][j], dp[i-1][j-k]);
mul(dp[n][s/2], dp[n][s/2]);
display(dp[n][s/2]);
}
}
return 0;
}

你可能感兴趣的:(dp)