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. 微信小程序之 Tabbar(底部选项卡)

    1.项目目录 2.在app.json里填写:tab个数范围2-5个 app.json { "pages": [ "pages/index/index", &qu ...

  2. 鸟哥的Linux私房菜-----13、账号管理

  3. [Unity-22] Coroutine协程浅析

    1.概念解释 协程并非一个独立的线程.在Unity中.全部的语句都是在一个线程中运行的,也就是说.Unity是单线程的(详细的能够參见http://blog.csdn.net/alexander_xf ...

  4. 【Mongodb教程 第一课 】 MongoDB下载安装

    MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.以window平台 ...

  5. 1 Angular 2 简介与 AngularJS 1.x 历史对比

    Angular 2 是一款JavaScript的开源框架,用于协助单一页面应用程序运行.Angular 2 是 AngularJS 1.x 的升级版本,应Web的进化和前端开发的变革还有从Angula ...

  6. 【内存数据库】OracleTimesten连接DSN创建用户

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...

  7. java8--IO工具类(java疯狂讲义3复习笔记)

    Paths类 public static void pathTest(){ Path path = Paths.get("~"); System.out.println(path) ...

  8. accept()函数用来告诉Qt,事件处理函数“接收”了这个事件,不要再传递;ignore()函数则告诉Qt,事件处理函数“忽略”了这个事件,需要继续传递(看一下QWidget::mousePressEvent的实现,最为典型。如果希望忽略事件,只要调用父类的响应函数即可)

    QEvent的accept()和ignore()一般不会用到,因为不如直接调用QWidget类的事件处理函数直接,而且作用是一样的,见下面的例子. 推荐直接调用QWidget的事件处理函数.而不是调用 ...

  9. “Invalid configuration file. File "I:/My Virtual Machines/Windows XP english Professional/Windows XP Professional.vmx" was created by a VMware product

    “Invalid configuration file. File "I:/My Virtual Machines/Windows XP english Professional/Windo ...

  10. caioj1270: 概率期望值1:小象涂色

    DP深似海,得其得天下.——题记 叕叕叕叕叕叕叕叕叕叕叕(第∞次学DP内容)被D飞了,真的被DP(pa)了.这次D我的是大叫着第二题比较难(小象涂色傻b题)的Mocha(zzz)大佬,表示搞个概率DP ...