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背包 + 数学剪枝的更多相关文章

  1. 51 Nod 1354 选数字(体现动态规划的本质)

    1354 选数字  基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 当给定一个序列a[0],a[1],a[2],...,a[n-1] 和一个整数K时 ...

  2. 51nod1354 选数字

    01背包tle. 解题报告(by System Message) 类似于背包的DP,以乘积为状态.先把等选数字里面不是K约数的去掉.然后找出K的约数,进行离散化.然后dp[i][j]表示前i个数字乘积 ...

  3. 【题解】选数字 [51nod1354]

    [题解]选数字 [51nod1354] 传送门:选数字 \([51nod1354]\) [题目描述] 共 \(T\) 组测试点,每一组给定一个长度为 \(n\) 的序列和一个整数 \(K\),找出有多 ...

  4. 1232: 买不到的数目 [DP、数学]

    1232: 买不到的数目 [DP.数学] 时间限制: 1 Sec 内存限制: 128 MB 提交: 21 解决: 10 统计 题目描述 小明开了一家糖果店.他别出心裁:把水果糖包成4颗一包和7颗一包的 ...

  5. 【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 ...

  6. BZOJ_2467_[中山市选2010]生成树_数学

    BZOJ_2467_[中山市选2010]生成树_数学 [Submit][Status][Discuss] Description 有一种图形叫做五角形圈.一个五角形圈的中心有1个由n个顶点和n条边组成 ...

  7. HDU - 4734 F(x) (2013成都网络游戏,数字DP)

    意甲冠军:求0-B见面<=F[A]所有可能的 思维:数字DP,内存搜索 #include <iostream> #include <cstring> #include & ...

  8. Qbxt 模拟题 day3(am) T3 选数字 (select)(贪心)

    选数字 (select Time Limit:3000ms Memory Limit:64MB 题目描述 LYK 找到了一个 n*m 的矩阵,这个矩阵上都填有一些数字,对于第 i 行第 j 列的位置上 ...

  9. URAL_1018 Binary Apple Tree 树形DP+背包

    这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...

随机推荐

  1. Deepin-安装node

    点击下载:Linux x64 文件解压: 方式1$xz -d file.tar.xz $tar -xvf file.tar 方式2 $tar xvJf file.tar.xz 解压后,把它移动到:/u ...

  2. Centos java 安装

    第一步:查看Linux自带的JDK是否已安装 (卸载centOS已安装的1.4) 安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java versi ...

  3. 开源yYmVc项目,邀您和我一起开发:)

    打算在闲暇时间写个MVC框架,要有什么功能一步一步边写边加,仿照struts 2 和 spring mvc.假设您感兴趣的话,能够私密我,给您加入key:). 欢迎您的到来~ 项目放在基于GIT的CS ...

  4. 2016/04/29 smarty模板 1, 初步 目标 : 变量 运算符 表达式 流程控制 函数

    ① 从配置文件中读取配置: 1,在模板页面加载配置文件 html页面 不是php页面 <{config_load file='fo.conf'}> 2,在需要用到配置的地方加 <{# ...

  5. mysql 查询语句去重 通过sql去重

    url 在表中具有唯一性: 由于定时任务的多进程/代码的多线程/定时任务的调度/脚本的执行时间,导致,破坏了唯一性: SELECT COUNT(1) FROM test_error_tmpUNION ...

  6. HDU1151 Air Raid —— 最小路径覆盖

    题目链接:https://vjudge.net/problem/HDU-1151 Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  7. maven配置篇

    1,windows A)安装maven之前,确认已正确安装JDK B)下载maven http://maven.apache.org/download.html C)将压缩包解压到指定目录,E:\ap ...

  8. Python下的LibSVM的使用

    突然觉的笔记真的很重要,给自己省去了很多麻烦,之前在Python 3 中装过libsvm 每一步都是自己百度上面搜寻的,花费了很长时间,但是并没有记录方法.这次换了电脑,又开始重新搜寻方法,觉得太浪费 ...

  9. SPOJ:Just One Swap(统计&思维)

    You are given an array of size N. How many distinct arrays can you generate by swapping two numbers ...

  10. 869C

    dp 我好像很zz... 想了好长好长时间,然后没想出来,怒掉rating... 其实我们可以吧三种颜色两两计算,因为这样加入第三种颜色不会影响之前的方案,那么我们跑一个dp,计算数量分别为a,b的方 ...