1354 选数字 DP背包 + 数学剪枝
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1354&judgeId=187448
其实这题和在若干个数字中,选取和为val,有多少种不同的选法是一样的。
只不过不能直接枚举背包容量,只能用map的iterate来枚举,这样来加速。
还有一个剪枝就是,所生成的数字要是val的约数,这些才加入去map那里,否则不加。
都是一些没用的状态。
还有这题不能用map的reverse_iterator,会蜜汁wa
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <hash_map>
using namespace __gnu_cxx;
const int MOD = ;
map<int, int>dp;
void init() {
dp.clear();
}
void work() {
init();
int n, val;
scanf("%d%d", &n, &val);
dp[] = ;
for (int i = ; i <= n; ++i) {
int x;
scanf("%d", &x);
//// if (val % x != 0) continue;
// for (map<int, int> :: reverse_iterator it = dp.rbegin(); it != dp.rend(); ++it) {
// if (val / it->first < x) continue;
// if (val % (it->first * x) != 0) continue;
//// dp[it->first * x] = (dp[it->first * x] + (it->second)) % MOD;
// dp[it->first * x] += it->second;
// dp[it->first * x] %= MOD;
// }
map<int, int> :: iterator it = dp.end();
it--;
for(;; --it) {
LL t = 1LL * it->first * x;
if (t <= val && val % t == ) {
dp[t] += it->second;
dp[t] %= MOD;
}
if (it == dp.begin()) break;
}
}
printf("%d\n", dp[val]);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
1354 选数字 DP背包 + 数学剪枝的更多相关文章
- 51 Nod 1354 选数字(体现动态规划的本质)
1354 选数字 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 当给定一个序列a[0],a[1],a[2],...,a[n-1] 和一个整数K时 ...
- 51nod1354 选数字
01背包tle. 解题报告(by System Message) 类似于背包的DP,以乘积为状态.先把等选数字里面不是K约数的去掉.然后找出K的约数,进行离散化.然后dp[i][j]表示前i个数字乘积 ...
- 【题解】选数字 [51nod1354]
[题解]选数字 [51nod1354] 传送门:选数字 \([51nod1354]\) [题目描述] 共 \(T\) 组测试点,每一组给定一个长度为 \(n\) 的序列和一个整数 \(K\),找出有多 ...
- 1232: 买不到的数目 [DP、数学]
1232: 买不到的数目 [DP.数学] 时间限制: 1 Sec 内存限制: 128 MB 提交: 21 解决: 10 统计 题目描述 小明开了一家糖果店.他别出心裁:把水果糖包成4颗一包和7颗一包的 ...
- 【bzoj1688】[USACO2005 Open]Disease Manangement 疾病管理 状态压缩dp+背包dp
题目描述 Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Far ...
- BZOJ_2467_[中山市选2010]生成树_数学
BZOJ_2467_[中山市选2010]生成树_数学 [Submit][Status][Discuss] Description 有一种图形叫做五角形圈.一个五角形圈的中心有1个由n个顶点和n条边组成 ...
- HDU - 4734 F(x) (2013成都网络游戏,数字DP)
意甲冠军:求0-B见面<=F[A]所有可能的 思维:数字DP,内存搜索 #include <iostream> #include <cstring> #include & ...
- Qbxt 模拟题 day3(am) T3 选数字 (select)(贪心)
选数字 (select Time Limit:3000ms Memory Limit:64MB 题目描述 LYK 找到了一个 n*m 的矩阵,这个矩阵上都填有一些数字,对于第 i 行第 j 列的位置上 ...
- URAL_1018 Binary Apple Tree 树形DP+背包
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...
随机推荐
- 【独立开发人员er Cocos2d-x实战 008】BMFont生成位图字体工具和Cocos2dx使用载入fnt文件
1.首先我们须要下载而且安装BMFont工具,下载地址例如以下:http://download.csdn.net/detail/chenqiai0/8899353(里面还有具体的使用文档,假设使用中有 ...
- css 滤镜之Gradient
CreateTime--2017年12月26日11:09:14 Author:Marydon ie滤镜特效之Gradient 作用: 用于设置渐变背景色 使用条件: IE9及以下版本不支持属性ba ...
- Oracle11g表空间导入dmp数据
如果你的表数据没有附带表空间和用户名,那么只要一句话 Imp {u_name}/{u_pwd}@{local_svrname} fromuser={from_user} touser={u_name} ...
- github的提交源码到服务器
github是现代的代码库,各种牛人,各种开源,也是现在大公司招聘的一个考察点, 这里介绍一下怎样把本地源码提交到github上. 首先我们需要在github上创建一个respository. 2,输 ...
- HTTP要点概述:三,客户端和服务器,请求和响应
一,客户端和服务器: HTTP协议主要用于客户端和服务器之间的通信. 1,客户端(client):请求访问资源的一端.(知道为啥用C表示客户端了吧) 2,服务器(server):提供资源响应的一端. ...
- Eclipse添加Qt插件
此文件仅为步骤操作作一个记录,以便以后方便查阅. 1.操作大体参考这个网站:http://blog.csdn.net/defonds/article/details/5013412 2.我的运行环境: ...
- 逆向分析一个完整的C++程序包含寄存器与参数传递详解
最近在分析C++ dump 文件的时候觉得有必要将一些必要的反汇编东西总结一下以备别人参考,自己有时间的时候也可以进行更多的改进.下面通过一个简单的C++代码转成汇编代码后的详细解释说明一下C++和汇 ...
- c#自动登录
c#自动登录 http://blog.csdn.net/eastmount/article/details/9703757
- Java Socket通信读取相关信息代码
转自:http://developer.51cto.com/art/201003/190206.htm Java Socket通信读取有不少需要我们注意的知识点.当我们在使用的时候有很多的问题摆在我们 ...
- java.lang.NoSuchMethodError: org.springframework.web.context.support.XmlWebApplicationContext.getEnv
转自:https://blog.csdn.net/u012941811/article/details/16960493 ava.lang.NoSuchMethodError: org.springf ...