ABCDE

题目连接:

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

Description

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 \(9\).

For example, in \(\\{8,4,2,1\\}\) encoding, \(1111\) is \(15\), exceed \(9\), so \(\\{8,4,2,1\\}\) encoding is BCD but not ABCD. In \(\\{2,4,2,1\\}\) encoding, any number represented by corresponding binary value won't exceed \(9\), so \(\\{2,4,2,1\\}\) encoding is ABCD.

Now, let's talk about ABCDE (Awesome Binary-Coded Decimal Extension).

An n-ABCDE is such a encoding that can only represent \(0\) to \(n\), and every number from \(0\) to \(n\) can be represented by one or more binary values. So \(\\{2,4,2,1\\}\) is a \(9\)-ABCDE and \(\\{8,4,2,1\\}\) is a \(15\)-ABCDE as we mentioned above. In addition, \(\\{16,8,4,2,1\\}\) is a \(31\)-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\\}\) encoding is the same with the \(\\{1,2,2,4\\}\) encoding, but it is different from \(\\{2,4,4,1\\}\).

Now, given a number \(n\), can you tell me how many different \(n\)-ABCDEs?

Input

There is an integer \(T\) in the first line, indicates the number of test cases.

For each test, the only line contains a integer \(n\).

\(1\leq T\leq 5000\)

\(1\leq n \leq 5000\)

Output

For each test, output an integer in one line, which is the number of different \(n\)-ABCDEs. As the answer may be too large, output it modulo \((10^9+7)\) (i.e. if the answer is \(X\), you should output \(X\ \%\ (10^9+7)\)).

Sample Input

5

1

2

3

4

5

Sample Output

1

1

2

2

4

Hint

题意

在数电中,有一种码,类似BCD码这种玩意儿

第i位如果为1的话,那么ans+=a[i],a[i]是这一位的位权

然后现在给你一个n,问你一共有多少种码可以表示1~n的所有数呢?

1,1,2和2,1,1视作一样。

题解:

首先考虑这个东西,如果不视作一样的话,就很简单了

dp[i]表示当前和为i的方案数,显然这个玩意儿能够一直转移到2i-1去。

由于视作一样,那么我们只要维护一个当前的最大值就好了,保证这个序列是递增的,这样就都不会一样了。

dp[i][j]表示现在和为i,最大值为j的方案数有多少。

这个转移发现可以用前缀和优化,这样就是n^2的了。

代码

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 5005;
int dp[maxn][maxn];
int sum[maxn][maxn];
void init()
{
dp[0][0]=1;
for(int i=0;i<=5000;i++)sum[0][i]=1;
for(int i=1;i<=5000;i++)
{
for(int j=1;j<=5000;j++)
if(j*2-1<=i)dp[i][j]=(dp[i][j]+sum[i-j][j])%mod;
sum[i][0]=dp[i][0];
for(int j=1;j<=5000;j++)
sum[i][j]=(sum[i][j-1]+dp[i][j])%mod;
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
printf("%d\n",sum[n][5000]);
}
}

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

  1. LOJ 6089 小Y的背包计数问题 —— 前缀和优化DP

    题目:https://loj.ac/problem/6089 对于 i <= √n ,设 f[i][j] 表示前 i 种,体积为 j 的方案数,那么 f[i][j] = ∑(1 <= k ...

  2. P5241 序列(滚动数组+前缀和优化dp)

    P5241 序列 挺神仙的一题 看看除了dp好像没什么其他办法了 想着怎么构个具体的图出来,然鹅不太现实. 于是我们想办法用几个参数来表示dp数组 加了几条边肯定要的吧,于是加个参数$i$表示已加了$ ...

  3. bzoj 1044 [HAOI2008]木棍分割——前缀和优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1044 前缀和优化. 但开成long long会T.(仔细一看不用开long long) #i ...

  4. bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...

  5. bzoj2431: [HAOI2009]逆序对数列(前缀和优化dp)

    2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 2312  Solved: 1330[Submit][Stat ...

  6. CF601C Kleofáš and the n-thlon(期望+前缀和优化dp)

    传送门 解题思路 要求这个人的排名,我们可以先求出某个人比他排名靠前的概率,然后再乘上\(m-1\)即为答案.求某个人比他排名靠前可以用\(dp\),设\(f[i][j]\)表示前\(i\)场比赛某人 ...

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

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

  8. Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

    C. Riding in a Lift Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/pr ...

  9. Topcoder 15405 - PrettyLiar(可删除背包+前缀和优化 dp)

    题面传送门 题意: 给出两个长度为 \(n\) 的数组 \(a,b\) 和一个整数 \(s\). 你可以任意重排数组 \(a,b\),总共 \((n!)^2\) 种方案. 现在又两个人 A,B 来玩游 ...

随机推荐

  1. 深入理解Spring系列之十一:SpringMVC-@RequestBody接收json数据报415

    转载 https://mp.weixin.qq.com/s/beRttZyxM3IBJJSXsLzh5g 问题原因 报错原因可能有两种情况: 请求头中没有设置Content-Type参数,或Conte ...

  2. gpk-update-icon占用CPU及清除【原创】

    发现服务器有个gpk-update-icon一直占用CPU进程 网上查看相关信息比较少. gpk-update-icon是gnome的更新图标进程 俩种处理方法: 1.杀掉gpk-update-ico ...

  3. 解决word2016鼠标每点击一下就出现一个保存的圆圈

    问题描述:今天打开word2016时,点击鼠标,随着鼠标会出现一个圆圈,让人看着很不习惯,通过查阅资料和亲自实践,记录在博客中. 由于自己之前装了PowerDesigner,PowerDesigner ...

  4. spring中的任务调度Quartz

    Spring 整合 Quartz 任务调度 主要有两种方式. Quartz的官网:http://www.quartz-scheduler.org/ 这两种只是一些配置文件简单配置就OK了,但是根本无法 ...

  5. ubuntu查看mysql版本的几种方法

    ubuntu查看mysql版本的几种方法 mysql 1:在终端下:mysql -V(大写) //代码 $ mysql -V mysql Ver 14.14 Distrib 5.5.46, for d ...

  6. openssh升级步骤

    1下载openssh最新版本 2 configure ./configure --prefix= /ssh先配置一下 再在本地安装. make &makeinstall 3 按照/ssh包含内 ...

  7. struct中长度为0的数组用途与原理

    前言 在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNUC中,存在一个非常奇怪的用法,那就是长度为0的数组,比如Array[0]; 很多人可能觉得不可思议,长度为0的数组是没有什么意义的, ...

  8. HDU 2053 Switch Game(开灯问题,完全平方数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2053 题目大意:灯开始是关着的,有n盏灯,i从1数到n每当灯的序号是i的倍数的时候就对灯进行一次操作( ...

  9. fastdfs5.11+centos7.2 按照部署(三)【转载】

    1.测试 前面两篇博文已对FastDFS的安装和配置,做了比较详细的讲解.FastDFS的基础模块都搭好了,现在开始测试下载. 1.1 配置客户端 同样的,需要修改客户端的配置文件: vim /etc ...

  10. Download failed : Oracle JDK 7 is NOT installed,解决oracle jdk7的问题

    先了解下概念: jdk(java development kit),就是java的开发工具集,顾名思义就是做开发用的,其中包括javac,也就是java compiler等.jre(java runt ...