数字选择
http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4261
題解:變形01背包問題
背包問題:https://blog.csdn.net/weixin_43272781/article/details/83214622
當放第i個數字x時,我們詢問當前容量j,在j-x的數量,那么可以構成j的數量就是dp[j]本來的數量加上dp[j-x]的數量
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int using namespace std; typedef long long ll; //typedef __int128 lll; const int N=100000+10; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k,q,ans; int a[N]; int dp[N]; char str; int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifwhile(~scanf("%d%d",&n,&m)){memset(dp,0,sizeof(dp));dp[0]=1;for(int i=1;i<=n;i++){scanf("%d",&a[i]);for(int j=m;j>=a[i];j--){if(dp[j-a[i]])dp[j]=dp[j-a[i]]+dp[j];}}cout << dp[m]<< endl;}//cout << "Hello world!" << endl;return 0; }?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: magic number
- 下一篇: 情人节的阻击