博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[HAOI2008]硬币购物
阅读量:4496 次
发布时间:2019-06-08

本文共 1574 字,大约阅读时间需要 5 分钟。

· 假设此时已求出标准完全背包,用$f[j]$表示。

· 本题关键在于,由于有个数限制,那么可以强制令当前状态不满足限制,即若最多取$Have[i]$个,强制令其先取$Have[i] + 1$个,那么减去$f[S - (Have[i] + 1)]$即可,当然需用容斥原理来进行加减。

· 代码:

1 #include 
2 #include
3 #include
4 5 using namespace std; 6 7 typedef long long LL; 8 9 const int MAXN = 4 + 5;10 const int MAXV = 1e05 + 10;11 12 LL f[MAXV]= {
0};13 14 const int N = 4;15 16 int W[MAXN];17 int Have[MAXN];18 19 int T;20 21 void Preparation () {22 f[0] = 1;23 for (int i = 1; i <= N; i ++)24 for (int j = W[i]; j <= MAXV - 10; j ++)25 f[j] += f[j - W[i]];26 }27 28 int main () {29 for (int i = 1; i <= N; i ++)30 scanf ("%d", & W[i]);31 scanf ("%d", & T);32 33 Preparation ();34 35 for (int Case = 1; Case <= T; Case ++) {36 int S;37 for (int i = 1; i <= N; i ++)38 scanf ("%d", & Have[i]);39 scanf ("%d", & S);40 41 LL ans = 0;42 int MAX = (1 << N) - 1;43 for (int state = 0; state <= MAX; state ++) {44 int rest = S;45 int cnt = 0;46 for (int k = 1; k <= N; k ++)47 if (state & (1 << (k - 1)))48 cnt ++, rest -= (Have[k] + 1) * W[k];49 50 if (rest < 0)51 continue;52 53 ans -= ((cnt & 1) ? 1 : - 1) * f[rest];54 }55 56 printf ("%lld\n", ans);57 }58 59 return 0; 60 }61 62 /*63 1 2 5 10 264 3 2 3 1 1065 1000 2 2 2 90066 */
View Code

 

转载于:https://www.cnblogs.com/Colythme/p/9696570.html

你可能感兴趣的文章
Android之Adapter用法总结-(转)
查看>>
总结列表显示ListView知识点
查看>>
android 教程实例系列
查看>>
lucene笔记
查看>>
tomcat无法正常shutdown
查看>>
zookeeper + dubbo 搭建
查看>>
根据前序遍历和中序遍历求出二叉树并打印
查看>>
UOJ356 [JOI2017春季合宿] Port Facility 【启发式合并】【堆】【并查集】
查看>>
Delphi的命令行编译命令
查看>>
BZOJ 1901 Zju2112 Dynamic Rankings 题解
查看>>
C++虚析构函数
查看>>
《玩转.NET Micro Framework 移植-基于STM32F10x处理器》--微软中国.NET Micro Framework项目组工程师所作之序...
查看>>
php服务端搜索,功能改进
查看>>
unity, 在surface shader中访问顶点色
查看>>
Spring声明式事务配置
查看>>
并查集的实现
查看>>
Leetcode 350. Intersection of Two Arrays II
查看>>
EditPlus VC2010 and 2008 C/C++配置
查看>>
Practical Lessons from Predicting Clicks on Ads at Facebook
查看>>
JFrame面板
查看>>