2 solutions
-
4
参考了这篇洛谷题解
不喜可踩
#include <bits/stdc++.h> using namespace std; int a[10005],w[1005]; int main(int argc, char **argv){ int n,m; cin >> n >> m; for (int i = 1;i <= n;i++){ cin >> w[i]; } a[0] = 1; for (int j = 1;j <= n;j++){ for (int i = w[j];i <= m;i++){ a[i] += a[i - w[j]]; a[i] %= (int)1e9 + 7; } } cout << a[m]; return 0; }
-
2
参考老师课件滚动数组代码
#include<bits/stdc++.h> using namespace std; long long arr[int(1e3)+5],b[int(1e4)+5]; int main(){ int w,n; cin>>n>>w; for(int i=1;i<=n;i++){ cin>>arr[i]; } b[0]=1; for(int i=1;i<=n;i++){ for(int j=arr[i];j<=w;j++){ b[j]=(b[j]+b[j-arr[i]])%(int(1e9)+7); } } cout<<b[w]; return 0; }
- 1
Information
- ID
- 1024
- Time
- 1000ms
- Memory
- 512MiB
- Difficulty
- 8
- Tags
- (None)
- # Submissions
- 46
- Accepted
- 9
- Uploaded By