几个相似的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的每个约 ...
随机推荐
- VBA连接MySQL数据库以及ODBC的配置(ODBC版本和MySQL版本如果不匹配会出现驱动和应用程序的错误)
db_connected = False '获取数据库连接设置dsn_name = Trim(Worksheets("加载策略").Cells(2, 5).Value) ---- ...
- error LNK2001: unresolved external symbol ___CxxFrameHandler3
Q: VS2005编译的静态库, 在vc++6.0中连接出现错误 error LNK2001: unresolved external symbol ___CxxFrameHandler3 A: ...
- MongoDB快速入门学习笔记2 MongoDB的概念及简单操作
1.以下列举普通的关系型数据库和MongoDB数据库简单概念上的区别: 关系型数据库 MongoDB数据库 说明 database database 数据库 table collection 数据库表 ...
- Android SDK 目录详解(转)
Android SDK目录结构和工具介绍是本文要介绍的内容,主要是来了解并学习Android SDK的内容,具体关于Android SDK内容的详解来看本文. Android SDK目录下有很多文件夹 ...
- Adaboost和GBDT的区别以及xgboost和GBDT的区别
Adaboost和GBDT的区别以及xgboost和GBDT的区别 以下内容转自 https://blog.csdn.net/chengfulukou/article/details/76906710 ...
- sqlserver操作各种文件方法
******* 导出到excelEXEC master..xp_cmdshell ''bcp SettleDB.dbo.shanghu out c:\temp1.xls -c -q -S"G ...
- 用例UML图
用例图主要用来描述“用户.需求.系统功能单元”之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图中涉及的关系有:关联.泛 ...
- sqlserver创建存储过程返回table
--创建存储过程test create procedure [dbo].[test] ( @I_MTR NVARCHAR (MAX), @I_TYPE NVARCHAR (MAX), @I_FAC N ...
- 项目记事【StreamAPI】:使用 StreamAPI 简化对 Collection 的操作
最近项目里有这么一段代码,我在做 code-review 的时候,觉得可以使用 Java8 StreamAPI 简化一下. 这里先看一下代码(不是源码,一些敏感信息被我用其他类替代了): privat ...
- git 远程仓库回滚
git branch backup #创建备份分支 git push origin backup:backup #push到远程 git reset --hard commit_id #本地分支回滚 ...