几个相似的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的每个约 ...
随机推荐
- Android拨打电话不弹出系统拨号界面总结
我在网上搜了一下,解决这个问题,有两种方式: 1.反射调用系统底层方法,并获取系统权限 反射调用的代码如下: Class phoneFactoryClass = Class.forName(" ...
- python项目中输出指定颜色的日志
起因 在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想 ...
- time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
- loj2057 「TJOI / HEOI2016」游戏
记横联通是一块横着的没有硬石头的地,把他们编号.竖联通同理. 对于一个空地,将其横联通编号和竖联通编号连边,二分图匹配,最大匹配为答案. #include <iostream> #incl ...
- laravel5.2总结--composer使用和自动加载介绍
首先看下phpcomposer官方的定义,composer是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer ...
- 42、通过ontouch检测expandableListview的单击、长按、列表滚动
一.在model定义变量: public boolean isExpandableListviewScroll = false;//这个是 首先监听expandableListview的滚动: Exp ...
- JMeter非GUI界面运行
JMeter是一款可以用于做接口可以用于作压力性能的应用程序,该程序是纯Java语音开发,所有对环境支持都比较好. JMeter可以运行模式有两种,一种是UI图形,另一种是命令模式运行也就是非GUI模 ...
- [oldboy-django][深入 rest framework] restframewok 教程: 分页功能
http://www.django-rest-framework.org/api-guide/pagination/ https://stackoverflow.com/questions/31785 ...
- SpringCloud Eureka参数配置项详解
SpringCloud Eureka参数配置项详解(转) Eureka涉及到的参数配置项数量众多,它的很多功能都是通过参数配置来实现的,了解这些参数的含义有助于我们更好的应用Eureka的各种功能,下 ...
- JAVA与VB通过SOCKET通讯
JAVA与VB通过SOCKET通讯 在做项目的过程中,本来是想使用JAVA的comm.jar工具实现串口通讯,不知道怎么回事,总是取不到电脑的串口.所以,改为现在的这种模式:java通过socket给 ...