文章
6
粉丝
83
获赞
90
访问
5.0k
完全背包+组合数问题,不会的朋友百度搜代码随想录,找动态规划题目列表一道一道学下来就会了,人家讲的比我好。 有几乎一样的leetcode题目377. 组合总和 Ⅳ
#include <iostream> #include "algorithm" #include "string" #include<vector> #include<math.h> using namespace std; int main() { int n; cin>>n; vector<int> nums; for(int i = 2;i<=n;i++){ int flag = 1; for(int j = 2;j<int(sqrt(i))+1;j++){ if(i%j==0){ flag = 0; break; } } if(flag == 1) nums.push_back(i); } vector<unsigned long long> dp(n+1,0); dp[0] = 1; for(int i = 0;i<nums.size();i++){ for(int j = nums[i];j<=n;j++){ dp[j] += dp[j-nums[i]]; } } cout<<dp[n]<<endl; }
登录后发布评论
暂无评论,来抢沙发