[hdu1023]递推
http://acm.hdu.edu.cn/showproblem.php?pid=1023
如果把栈里面的元素个数表示成状态,每一步(共2 * n步)的状态构成的状态序列的种数就是答案,令dp[i][j]表示第i步栈的状态为j的方案数,则有:
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j + 1],+1、-1相当于进栈和出栈,需考虑边界条件,详见代码(答案太大,需用大数):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <string>
#include <ctime>
#include <queue>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define eps 0.0000001
#define lowbit(x) ((x) & -(x))
#define memc(a, b) memcpy(a, b, sizeof(b))
#define x_x(a) ((a) * (a))
#define LL __int64
#define DB double
#define pi 3.14159265359
#define MD 10000007
#define INF (int)1e9
using namespace std;
struct BigNum{
#define maxlen 10
#define memc(a, b) memcpy(a, b, sizeof(b))
#define mem0(a) memset(a, 0, sizeof(a))
typedef __int64 Num[maxlen + ];
Num num;
char s[maxlen + ];
BigNum operator+(BigNum num2) {
BigNum ans;
mem0(ans.num);
for(int i = ; i <= maxlen; i++) {
ans.num[i] += num[i] + num2.num[i];
ans.num[i + ] += ans.num[i] / (int)1e9;
ans.num[i] %= (int)1e9;
}
return ans;
}
BigNum operator*(BigNum num2) {
BigNum ans;
mem0(ans.num);
for(int i = ; i <= maxlen; i++) {
for(int j = ; j <= maxlen; j++) {
if(i + j - <= maxlen) {
ans.num[i + j - ] += num[i] * num2.num[j];
ans.num[i + j] += ans.num[i + j - ] / (int)1e9;
ans.num[i + j - ] %= (int)1e9;
}
}
}
return ans;
}
void convert() {
int len = strlen(s), cnt = ;
for(int i = len - ; i >= ; i -= ) {
int p = , x = , t = ;
while(i - p >= && p < ) {
x += t * (s[i - p] - '');
p++;
t *= ;
}
num[++cnt] = x;
}
}
void inp() {
mem0(num);
scanf("%s", s);
convert();
}
void outp() {
int p = ;
for(int i = maxlen; i >= ; i--) {
if(num[i]) {
p = i;
break;
}
}
cout<< num[p];
while(--p) {
int a[] = {}, x = num[p];
for(int i = ; i < ; i++) {
a[i] = x % ;
x /= ;
}
for(int i = ; i >= ; i--) {
printf("%d", a[i]);
}
}
}
BigNum(char str[]) {
strcpy(s, str);
mem0(num);
convert();
}
BigNum(){}
};
BigNum f[][];
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
while(~scanf("%d", &n)) {
mem0(f);
f[][] = BigNum("");
for(int i = ; i <= * n; i++) {
for(int j = ; j <= n; j++) {
f[i][j] = f[i - ][j + ];
if(j) f[i][j] = f[i][j] + f[i - ][j - ];
}
}
f[ * n][].outp();
cout<< endl;
}
return ;
}
[hdu1023]递推的更多相关文章
- 【BZOJ-2476】战场的数目 矩阵乘法 + 递推
2476: 战场的数目 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 58 Solved: 38[Submit][Status][Discuss] D ...
- 从一道NOI练习题说递推和递归
一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...
- Flags-Ural1225简单递推
Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...
- 利用Cayley-Hamilton theorem 优化矩阵线性递推
平时有关线性递推的题,很多都可以利用矩阵乘法来解决. 时间复杂度一般是O(K3logn)因此对矩阵的规模限制比较大. 下面介绍一种利用利用Cayley-Hamilton theorem加速矩阵乘法的方 ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 简单递推 HDU-2108
要成为一个ACMer,就是要不断学习,不断刷题...最近写了一些递推,发现递推规律还是挺明显的,最简单的斐波那契函数(爬楼梯问题),这个大家应该都会,看一点稍微进阶了一点的,不是简单的v[i] = v ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
- openjudge1768 最大子矩阵[二维前缀和or递推|DP]
总时间限制: 1000ms 内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...
随机推荐
- 再接再厉,JSONViewer现已支持Firefox、Microsoft Edge、360浏览器,可能是最好用的JSON格式化工具
之前写的JSONViewer,截至目前在谷歌商店里已经有1000+的自然下载量了 为什么开发JSONViewer? 日常开发中,拿到接口输出的JSON一般会去在线的JSON格式化网站查看,但是在线格式 ...
- Joomla 3.4.6 RCE 分析
Joomla 3.4.6 RCE 漏洞分析,首发先知社区: https://xz.aliyun.com/t/6522 漏洞环境及利用 Joomla 3.4.6 : https://downloads. ...
- JS流程图解决方案GoJS
GoJs简介 一个实现交互类图表(比如流程图,树图,关系图,力导图等等)的JS库 GoJS依赖于HTML5,所以请保证您的浏览器版本支持HTML5,当然还要加载这个库. 首先个人建议先下载官方实例的 ...
- Python自学从入门到就业之函数基础(小白必看)
函数介绍 <1>什么是函数 请看如下代码: print(" _ooOoo_ ") print(" o8888888o ") print(" ...
- 【转帖】Python 重复造轮子/造轮子找模子,你都应该熟读该文
Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...
- mysql面试(1)
一一个 SQL 执行行行的很慢,我们要分两种情况讨论:1.大大多数情况下很正常,偶尔很慢,则有如下原因(1).数据库在刷新脏⻚页,例例如 redo log 写满了了需要同步到磁盘.(2).执行行行的时 ...
- Windows 自动登录
https://serverfault.com/questions/840557/auto-login-a-user-at-boot-on-windows-server-2016 Use Sysint ...
- c++ 更新 performance counter 数据,错误码 87
ERROR_INVALID_PARAMETER 87 (0x57) The parameter is incorrect. 很可能是该送 ULONG 的送了 ULONGLONG,vise versa
- Linux网络服务第五章NFS共享服务
1.笔记 NFS一般用在局域网中,网络文件系统c/s格式 服务端s:设置一个共享目录 客户端c:挂载使用这个共享目录 rpc:111远程过程调用机制 Showmount -e:查看共享目录信息 def ...
- Eclipse Mac OS 安装 最新版 Subversion插件subclipse
subclipse 目前全部转移到github 官方地址 https://github.com/subclipse/subclipse/wiki Eclipse mac版 安装 最新版svn插件sub ...