标签:贵的 集合 序列 fine space 无法 很多 bsp img
·背包问题:
有很多的变式
if(m<5) cout<<m<<endl; else { int f[1010];//f[j]表示买前i件物品,预算为j时的最大花销 memset(f,0,sizeof(f)); for(int i=1; i<=n-1; i++) for(int j=m-5; j>=price[i]; j--) if(f[j]<f[j-price[i]]+price[i]) f[j]=f[j-price[i]]+price[i]; cout<<m-max-f[m-5]<<endl; }
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int f[550][550], a[550]; int main() { int n, m, b, mod; scanf("%d %d %d %d", &n, &m, &b, &mod); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); f[0][0] = 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) for(int k = a[i]; k <= b; k++) f[j][k] = (f[j][k] + f[j-1][k-a[i]]) % mod; int ans = 0; for(int i = 0; i <= b; i++) ans = (ans + f[m][i]) % mod; printf("%d\n", ans); return 0; }
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; #define SZ 20100 int c[33], g[33]; int f[33][SZ]; int main() { int C, G; scanf("%d %d", &C, &G); for(int i = 0; i < C; i++) scanf("%d", &c[i]); for(int i = 1; i <= G; i++) scanf("%d", &g[i]); memset(f, 0, sizeof(f)); for(int i = 0; i < C; i++) f[1][7500 + g[1]*c[i]] = 1; int ans = 0; for(int i = 2; i <= G; i++) for(int j = 0; j < 15000; j++) for(int k = 0; k < C; k++) if(f[i-1][j] != 0) f[i][j + g[i]*c[k]] += f[i-1][j]; printf("%d\n", f[G][7500]); return 0; }
#include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long LL; #define SZ 1010 const LL mod = 1e9+7; LL f[SZ][1030], g[SZ][1030], dp[SZ][1030]; int a[SZ]; int main() { int T; scanf("%d", &T); while(T--) { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 0; i <= n; i++) for(int j = 0; j <= 1024; j++) f[i][j] = g[i][j] = dp[i][j] = 0; for(int i = 1; i < n; i++) { f[i][a[i]]++; for(int j = 0; j <= 1024; j++) if(f[i-1][j]) { f[i][j^a[i]] = (f[i][j^a[i]] + f[i-1][j]) % mod; f[i][j] = (f[i][j] + f[i-1][j]) % mod; } } for(int i = n; i > 1; i--) { g[i][a[i]]++; dp[i][a[i]]++; for(int j = 0; j <= 1024; j++) if(g[i+1][j])//到i+1位时的方案数,i+1位可取可不取 { g[i][j] = (g[i][j] + g[i+1][j]) % mod;//到i位时的方案数,i位不取 g[i][j&a[i]] = (g[i][j&a[i]] + g[i+1][j]) % mod;//到i位时的方案数,i位取 dp[i][j&a[i]] = (dp[i][j&a[i]] + g[i+1][j]) % mod;//到i位时的方案数,i位必须取 } } LL ans = 0; for(int i = 1; i < n; i++) for(int j = 0; j <= 1024; j++) ans = (ans + f[i][j] * dp[i+1][j]) % mod; printf("%lld\n", ans); } return 0; }
标签:贵的 集合 序列 fine space 无法 很多 bsp img
原文地址:https://www.cnblogs.com/pinkglightning/p/9551492.html