此题是动态规划+大数相加
利用动态规划的方法就c[i][j],其中c[i][j]就是在i个元素中取j个的方法数,即c(i,j)。c[i][j]=c[i-1][j-1]+c[i-1][j]。
我的AC代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int c[513][513][100];
int big (int a, int b){
return (a > b ? a : b);
}
void myplus (int a[], int b[], int c[]){
c[0] = big(a[0], b[0]);
int temp = 0;
for(int i = 1; i <= c[0]; i++){
c[i] = a[i]+b[i] + temp;
temp = (c[i] >= 10 ? 1 : 0);
c[i] %= 10;
}
if(temp) {
c[0]++;
c[c[0]] = 1;
}
}
void myplus1(int x[],int y[]) {
x[0]=max(x[0],y[0]);
for (int i=1;i<=x[0];i++) {
x[i]+=y[i];
x[i+1]+=x[i]/10;
x[i]%=10;
}
if (x[x[0]+1]!=0) x[0]++;
}
int main(int argc, const char * argv[])
{
int k,w;
scanf("%d %d", &k,&w);
int bigx = (1<<k);
int bigxx = 1<<(w%k);
int result[201];
memset(result, 0, sizeof(result));
memset(c, 0, sizeof(c));
c[0][0][0] = 1;
c[0][0][1] = 1;
for(int i = 1; i < bigx; i++){
for(int j = 0; j <= i; j++){
if(j==0) {
c[i][j][0] = 1;
c[i][j][1] = 1;
}else{
myplus(c[i-1][j-1],c[i-1][j],c[i][j]);
}
}
}
for(int i = 2; i <= w/k&&i < bigx; i++) myplus1(result, c[bigx-1][i]);
for(int i = 1; i < bigxx && w/k+i < bigx;i++) myplus1(result, c[bigx-1-i][w/k]);
for(int i = result[0]; i >= 1; i--) cout << result[i];
cout << endl;
}