几个相似的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的每个约 ...
随机推荐
- 水题:HDU1034-Candy Sharing Game
解题心得: 1.我是用的模拟的算法直接模拟的每一轮的分配方法的得出的答案,看到有些大神使用的链表做的,好像链表才是这道题的镇长的做法吧. 题目: Candy Sharing Game Time Lim ...
- [jzoj5233]概率博弈(树形DP)
Description 小A和小B在玩游戏.这个游戏是这样的: 有一棵
- 牛客练习赛42 A 字符串
题目描述 给定两个等长的由小写字母构成的串 A,BA,B,其中 |A|=|B|=n|A|=|B|=n. 现在你需要求出一个子区间 [l,r][l,r] 使得 LCP(A[l,r],B[l,r])×LC ...
- Leetcode 700. 二叉搜索树中的搜索
题目链接 https://leetcode.com/problems/search-in-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根节点 ...
- secureCRT中vim行号下划线问题
在vim中发现开启显示行号(set number)或语法高亮(syntax on)时,发现文档中很多地方都有下划线,对视觉产生极大干扰.开始还以为是vim的某个配置造成的,后来发现真正的元凶是secu ...
- Mysql密码加密方式
以Mysql 4.1版本为分界线,两种加密方式 Mysql323加密:(16位) select old_password('root'); //Mysql自带加密函数old_password(str ...
- Java面向对象---抽象类与接口
final关键字 1.final关键字在java中被称为完结器,表示最终的意思: 2.final能声明类.方法.属性: 3.使用final声明的类不能被继承,使用final声明的方法不能被重写,使用f ...
- TCP/IP网络编程之I/O流分离
分离I/O流 “分离I/O流”是一种常用表达,有I/O工具可以区分二者.无论使用何种办法,都可以认为分离I/O流.我们之前通过两种方法分离I/O流,第一种是TCP/IP网络编程之进程间通信中的“TCP ...
- opencv中的仿射变换
什么是仿射变换? 原理:1.一个任意的仿射变换都能表示为 乘以一个矩阵(线性变换) 接着再 加上一个向量(平移) 2.综上所述,我们能够用仿射变换来表示: 1)旋转(线性变换) 2)平移(向量加) 3 ...
- vrpie在Visio Studio 中无法调试的问题
最近这这几天一直在研究vrpie,之前不能调试,一调试就出问题,打开那个生成的htm文件是没问题的,最初的解决方法是不通过调试来打可那个htm页面,但是这样比较麻烦,因为经常需要和服务器交互,就只能用 ...