题目链接:

http://acm.uestc.edu.cn/#/problem/show/1307

ABCDE

Time Limit: 1000/1000MS (Java/Others)
Memory Limit: 262144/262144KB (Java/Others)
#### 问题描述
> Binary-coded decimal (BCD) is a kind of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits.
>
> Awesome Binary-Coded Decimal (ABCD) is, under the above conditions, any number represented by corresponding binary value won't exceed 99.
>
> For example, in {8,4,2,1}{8,4,2,1} encoding, 11111111 is 1515, exceed 99, so {8,4,2,1}{8,4,2,1} encoding is BCD but not ABCD. In {2,4,2,1}{2,4,2,1} encoding, any number represented by corresponding binary value won't exceed 99, so {2,4,2,1}{2,4,2,1} encoding is ABCD.
>
> title
>
> Now, let's talk about ABCDE (Awesome Binary-Coded Decimal Extension).
>
> An n-ABCDE is such a encoding that can only represent 00 to nn, and every number from 00 to nn can be represented by one or more binary values. So {2,4,2,1}{2,4,2,1} is a 99-ABCDE and {8,4,2,1}{8,4,2,1} is a 1515-ABCDE as we mentioned above. In addition, {16,8,4,2,1}{16,8,4,2,1} is a 3131-ABCDE.
>
> Two encoding will be considered different if they have different length, or they have different number set, with the number of occurrence of each number considered. More precisely, two different coding will have such a number that occur different times.
>
> So, {2,4,2,1}{2,4,2,1} encoding is the same with the {1,2,2,4}{1,2,2,4} encoding, but it is different from {2,4,4,1}{2,4,4,1}.
>
> Now, given a number nn, can you tell me how many different nn-ABCDEs?
#### 输入
> There is an integer TT in the first line, indicates the number of test cases.
>
> For each test, the only line contains a integer nn.
>
> 1≤T≤50001≤T≤5000
> 1≤n≤5000
#### 输出
> For each test, output an integer in one line, which is the number of different nn-ABCDEs. As the answer may be too large, output it modulo (109+7)(109+7) (i.e. if the answer is XX, you should output X % (109+7)X % (109+7)).
####样例输入
> 5
> 1
> 2
> 3
> 4
> 5

样例输出

1

1

2

2

4

题意

求子集能够表示1、2、...、n所有数的集合种数。

比如n=5:{1,1,1,1,1},{1,1,1,2},{1,2,2},{1,1,3}总共4种

题解

dp[i][j]表示和为i,且集合里面最大的数为j的总数,则有dp[i][j]=sigma(dp[i-j][k])其中k<=j,j*2-1<=i。 当然这样转移会n3超时,不过我们可以处理出sumv[i][j]=sig(dp[i][k])其中k<=j。然后O(n2)就能跑。

为什么j*2-1<=i?假设我们已知和为s且满足条件的集合数,那么我们考虑再加一个数x,那么这个数肯定要<=s+1,否则s+1就会无法表示!也就是说如果j能够属于和为i的集合里面的最大数,那么必然就有j+j-1<=i。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=5001;
const int mod=1e9+7; LL dp[maxn][maxn];
void pre(){
clr(dp,0);
dp[1][1]=1;
for(int j=1;j<maxn;j++) dp[1][j]=(dp[1][j-1]+dp[1][j])%mod;
for(int i=2;i<maxn;i++){
for(int j=1;j*2-1<=i;j++)
dp[i][j]=dp[i-j][j];//这里的dp[i-j][j]已经变成是sum[i-j][j]了,但是由于空间比较紧,就没另开
//预处理出前缀和。
for(int j=1;j<maxn;j++)
dp[i][j]=(dp[i][j-1]+dp[i][j])%mod;
}
} int main() {
pre();
int tc,kase=0;
scanf("%d",&tc);
while(tc--){
int x; scf("%d",&x);
prf("%lld\n",dp[x][5000]);
}
return 0;
} //end-----------------------------------------------------------------------

CDOJ ABCDE dp(前缀和优化)的更多相关文章

  1. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  2. [Codeforces712D] Memory and Scores(DP+前缀和优化)(不用单调队列)

    [Codeforces712D] Memory and Scores(DP+前缀和优化)(不用单调队列) 题面 两个人玩游戏,共进行t轮,每人每轮从[-k,k]中选出一个数字,将其加到自己的总分中.已 ...

  3. T2988 删除数字【状压Dp+前缀和优化】

    Online Judge:从Topcoder搬过来,具体哪一题不清楚 Label:状压Dp+前缀和优化 题目描述 给定两个数A和N,形成一个长度为N+1的序列,(A,A+1,A+2,...,A+N-1 ...

  4. Codeforces 479E. Riding in a Lift (dp + 前缀和优化)

    题目链接:http://codeforces.com/contest/479/problem/E 题意:         给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...

  5. 2018多校第九场 HDU 6416 (DP+前缀和优化)

    转自:https://blog.csdn.net/CatDsy/article/details/81876341 #include <bits/stdc++.h> using namesp ...

  6. Student's Camp CodeForces - 708E (dp,前缀和优化)

    大意: $n$行$m$列砖, 白天左侧边界每块砖有$p$概率被摧毁, 晚上右侧边界有$p$概率被摧毁, 求最后上下边界连通的概率. 记${dp}_{i,l,r}$为遍历到第$t$行时, 第$t$行砖块 ...

  7. BZOJ 1044: [HAOI2008]木棍分割 DP 前缀和优化

    题目链接 咳咳咳,第一次没大看题解做DP 以前的我应该是这样的 哇咔咔,这tm咋做,不管了,先看个题解,再写代码 终于看懂了,卧槽咋写啊,算了还是抄吧 第一问类似于noip的那个跳房子,随便做 这里重 ...

  8. hihocoder1475 数组分拆【DP+前缀和优化】

    思路: DP[ i ] 代表以 i 结尾的方案数. dp[i] += sum[i] - sum[j - 1] != 0 ? dp[j] : 0 ; 对于100%的数据,满足1<=N<=10 ...

  9. 5.19 省选模拟赛 小B的夏令营 概率 dp 前缀和优化dp

    LINK:小B的夏令营 这道题是以前从没见过的优化dp的方法 不过也在情理之中. 注意读题 千万不要像我这个sb一样 考完连题意都不知道是啥. 一个长方形 要求从上到下联通的概率. 容易发现 K天只是 ...

随机推荐

  1. Fpm启动机制及流程分析———详细

    FPM(FastCGI Process Manager)是PHP FastCGI运行模式的一个进程管理器,从它的定义可以看出,FPM的核心功能是进程管理,那么它用来管理什么进程呢?这个问题就需要从Fa ...

  2. html input file accept

    *.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video*.ac3 audio/ac3 AC3 Audio*.asf allpication/vnd.ms-asf ...

  3. maven添加本地jar

    maven有时需要添加了一些本地jar,记录下流程 1.在项目名下创建一个文件夹,起名为lib吧,放要的jar放进去 2.然后打开jar在的路径,打开命令窗口,执行 mvn install:insta ...

  4. LFS搭建第一天补充

    1.选择lfs的iso 2.启动虚拟机,进入以下界面直接按回车键 3. 选择下面的时间 4.直接默认本地时间 5.语言格式选择UTF-8 6.默认 7.直接回车,进入系统 8.对hda盘进行分区,我分 ...

  5. Angular 7 版本

    Angular 7 版本 这是跨整个平台的主要版本,更新包括核心框架,Angular Material和CLI. 如何更新到v7 可以访问update.angular.io以获取有关更新应用程序的详细 ...

  6. 20155306 实验一《Java开发环境的熟悉》实验报告

    实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1. ...

  7. 20155315实验三 敏捷开发与XP实践

    实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程: 2.完成实验.撰 ...

  8. 20155315 2016-2017-2 实验二《Java面向对象程序设计》实验报告

    实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验知识点 1.参考Intellj I ...

  9. 2017-2018-1 20155327 《信息安全系统设计基础》课堂测试&课下作业

    2017-2018-1 20155327 <信息安全系统设计基础>课堂测试&课下作业 学习使用stat(1),并用C语言实现 提交学习stat(1)的截图 man -k ,grep ...

  10. # C语言假期学习笔记——6

    C语言假期学习笔记--6 数组 主要学习了一位数组和二维数组. 数组是一组具有相同类型的变量的集合.C语言程序通过元素的下标来访问数组中的元素.(下标从0开始) 一位数组 类型 数组名[元素个数] 二 ...