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个树枝的最大苹果数,但是在树形过 ...
随机推荐
- vue 自定义 移动端筛选条件
1.创建组件 components/FilterBar/FilterBar.vue <template> <div class="filterbar" :styl ...
- MyBatis -- sql映射文件具体解释
MyBatis 真正的力量是在映射语句中. 和对等功能的jdbc来比价,映射文件节省非常多的代码量. MyBatis的构建就是聚焦于sql的. sql映射文件有例如以下几个顶级元素:(按顺序) cac ...
- 我的package.json清单
{ "name": "lists", "version": "1.0.0", "main": &qu ...
- sql里的in对应linq的写法 及 IQueryable转化为Dictionary
string[] arID = { "0001", "0002" }; var dict = this.service.GetMyList(m => ar ...
- c/c++内存使用原则
1 no malloc no free 2 no new no delete 如果对象不是new出来的,那么这个对象在生命周期结束后会自动调用析构函数自己释放自己的内存,不需要delete. 但是如果 ...
- VS2010 根据WSDL文件(java Web Service)生成.cs文件
我们添加webService引用,一般是通过 添加服务引用完成的,其实 添加服务引用 在背后为我们生成了代理类. 我们手动生成代理类方法: 1.通过java Web Service,生成wsdl文件: ...
- cocos2dx笔记1:概述
1.核心的类和功能 CCDirector gameLoop,实现场景绘制.多个场景之间切换控制.控制游戏的停止,暂停,等生命周期. CCScene 场景类,每一个场景能够理解为一个游戏镜头.状态 CC ...
- NDK编程中如何在C文件中打印调试信息
1,在Android.mk文件中加上 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog LOCAL_PATH := $(call my-dir)include ...
- 演练:使用属性自定义 DataGrid 控件
演练:使用属性自定义 DataGrid 控件 Silverlight 此主题尚未评级 - 评价此主题 Silverlight DataGrid 控件支持常用表格式设置选项,例如交替显示不同的行 ...
- BZOJ 4815 数论
今年的重庆省选? 具体就是,对于每次修改,A[p,q]这个位置, 设d=gcd(p,q) ,则 gcd为d的每一个格子都会被修改,且他们之间有个不变的联系 A[p,q]/p/q==A[k,t]/k/ ...