wiki 1157 2k进制数

此题是动态规划+大数相加

利用动态规划的方法就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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值