TYVJ1172 自然数拆分Lunatic版 - 背包DP[完全背包]

TYVJ1172 自然数拆分Lunatic版

传送门

思路:

类比TYVJ1096 数字组合 , 本题的数字可以重复使用,所以是一个完全背包模型。\(f[i,j]\)表示当前选到第\(i\)类数字凑成的数字为\(j\)的方案数。

Tips:

1.模数为\(2^64\),需要用\(unsigned~long~long\)存储,所以\(f\)数组也要用\(unsigned~long~long\)存储。
2.因为是自然数拆分,所以最终答案减去凑成0的方案数。

AC Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 4000+100;
typedef unsigned int us;
us f[N];
int main(){
    int n;scanf("%d",&n);
    f[0]=1;
    for(int i=1;i<=n;i++){
        for(int j=i;j<=n;j++){
            f[j]=(f[j]+f[j-i])%2147483648u;
        }
    }
    printf("%d",(f[n]-1+2147483648u)%2147483648u);//减去凑成0的方案 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Loi-Brilliant/p/9612423.html
今日推荐