几个相似的DP题
HDU1398
题意:把一个整数分拆成1、4、9、16、……、256、289(注意:只到289)这17个完全平方数的和,有几种方法。
解法不用说自然是DP,因为搜索显然超时。
(这样的题我一般不敢开int,怕爆……)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional> using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i)
#define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define dec(i,a,b) for(int i(a); i >= (b); --i)
#define for_edge(i,x) for(int i = H[x]; i; i = X[i]) #define LL long long
#define ULL unsigned long long
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
#define INF 1 << 30 const int N = + ;
const int M = + ;
const int Q = + ;
const int A = + ; LL f[Q];
int n; int main(){
#ifndef ONLINE_JUDGE
freopen("test.txt", "r", stdin);
freopen("test.out", "w", stdout);
#endif memset(f, , sizeof f);
f[] = 1LL;
rep(i, , ) rep(j, , )
f[j + i * i] += f[j]; while (~scanf("%d", &n), n) printf("%lld\n", f[n]); return ; }
HDU1028 自然数无序拆分
恩,经典的DP题
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional> using namespace std; #define REP(i,n) for (int i(0); i < (n); ++i)
#define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) #define LL long long
#define ULL unsigned long long
#define MP make_pair
#define FI first
#define SE second const int INF = ;
const int M = + ;
const int N = + ;
const int A = + ; int f[N][N];
int n; int main(){
freopen("1test.in", "r", stdin);
freopen("1test.out", "w", stdout); rep(i, , ) f[][i] = , f[i][] = ;
rep(i, , ) rep(j, , ){
if (j > i) f[i][j] = f[i][i]; else
if (i == j) f[i][j] = f[i][j - ] + ; else
f[i][j] = f[i][j - ] + f[i - j][j];
} while (~scanf("%d ", &n)) printf("%d\n", f[n][n]); return ; }
二维的方法……但是我以前用一维的方法AC过,总觉得该回忆起来……
HDU5890 去掉给定编号的三个数(如果有编号重复那就相当于去掉了两个数甚至一个数)任意选10个数,相加,是否可以得到87?
当初青岛网赛的题,题意不难理解就是A不掉。
后来查了很多资料,并且看了很多巨屌的博客。我终于得出一个结论:此题要用bitset优化。
思想也是背包,但是和前两道比起来就比较复杂了。
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define dec(i,a,b) for(int i(a); i >= (b); --i) const int N = + ; int T;
int q;
int f[N][N][N];
int n;
int c[];
int a[N];
int Q; bitset <> A[]; int check(int i, int j, int k){
rep(h, , ) A[h].reset(); A[][] = ;
rep(h, , n) if (h != i && h != j && h != k && a[h] <= ) dec(p, , ){
A[p + ] |= A[p] << a[h];
if (A[][]) return ;
} return ;
} int main(){
#ifndef ONLINE_JUDGE
freopen("test.txt", "r", stdin);
freopen("test.out", "w", stdout);
#endif scanf("%d", &T);
while (T--){
scanf("%d", &n);
rep(i, , n) scanf("%d", a + i);
rep(i, , n) rep(j, i, n) rep(k, j, n) f[i][j][k] = check(i, j, k);
scanf("%d", &Q);
while (Q--){
scanf("%d%d%d", c + , c + , c + );
sort(c + , c + );
puts(f[c[]][c[]][c[]] ? "Yes" : "No");
}
} return ; }
继续努力吧!!
几个相似的DP题的更多相关文章
- Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...
- 4817 江哥的dp题d
4817 江哥的dp题d 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 已知1-N的排列P的LIS(最长上 ...
- 4809 江哥的dp题c
4809 江哥的dp题c 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有两个数x,y,一开始x=1,y= ...
- 4816 江哥的dp题b
4816 江哥的dp题b 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给出两个1-N的随机排列A,B.若 ...
- 4815 江哥的dp题a
4815 江哥的dp题a 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给出一个长度为N的序列A(A1,A ...
- HDU 2577 How to Type(dp题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...
- codevs4817 江哥的dp题d
4817 江哥的dp题d 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold [题目描述] Description 已知1-N的排列P的LIS(最长上升子序列)不超 ...
- 古韵之乞巧 题解 dp题
[noip模拟赛1]古韵之乞巧 描述 闺女求天女,更阑意未阑. 玉庭开粉席,罗袖捧金盘. 向月穿针易,临风整线难. 不知谁得巧,明旦试相看. ——祖咏<七夕> 女子乞巧,是七夕的重头戏 ...
- cf1061c 普通dp题
题解见https://blog.csdn.net/godleaf/article/details/84402128 这一类dp题是可以压缩掉一维空间的,本题枚举a1到an,枚举到ai时枚举ai的每个约 ...
随机推荐
- 51nod 1105 二分答案法标准题目
二分答案法例题,用于练习二分答案的基本思想非常合适,包括了思维方式转换的内容(以前我们所做的一直是利用二分法求得数组元素对应指针之类,但是现在是直接对答案进行枚举). 思路是:首先对输入数组进行排序, ...
- Android TV 开发(4)
本文来自网易云社区 作者:孙有军 最后我们再来看看好友界面,改界面本地是没有xml的,因此我们直接来看看代码: 这里将使用到数据bean,与数据源的代码也贴出来如下: public class Con ...
- LFTP下载远程文件
拓展阅读: https://linux.cn/article-5460-1.html
- struts ValueStack 详解
一.ValueStack 1.ValueStack是一个接口,在struts2中使用OGNL(Object-Graph Navigation Language)表达式实际上是使用 ...
- IO Streams:数据流
数据流支持原始数据类型值(布尔型,字符型,字节型,短型,长整型,浮点型和双倍型)的二进制I / O以及字符串值.所有数据流都实现了DataInput接口或DataOutput接口.本节重点介绍这些接口 ...
- Unity --yield return
1.yield return null; //暂停协同程序,下一帧再继续往下执行 yield new WaitForFixedUpdate (); //暂停协同程序,等到下一次调用FixedUpdat ...
- rsync 使用小记
工作中遇到了有关rsync使用的问题,在这里记录下供有同样需求的人参考一下 先说下环境 服务端配置 pid file = /rsyncdata/rsyncd.pid port = 873 addres ...
- 关于Android应用中图片占用内存浅谈
从事过移动端应用开发的童鞋应该都清楚,内存是非常宝贵的资源.如果能很好的利用有限的内存,对应用性能的提升会有很大的帮助.在实际应用开发中图片内存占整个应用非常大的比重,我们只有了解图片是如何加载到内存 ...
- 【bzoj1531】[POI2005]Bank notes 多重背包dp
题目描述 Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值 ...
- BZOJ-3231 [SDOI2008]递归数列
转成矩阵连乘后,矩阵快速幂加速解决. 一开始没把需要longlong的变量补全..而且没初始化2333 #include <cstdlib> #include <cstdio> ...