hdu 4651 Partition(公式)
2015-10-30 23:29
435 查看
题目链接:hdu 4651 Partition
解题思路
本质上其实是一个完全背包,但是物品有1e5,所以复杂度太大。有一个五边形数的公式可以优化。代码
#include <cstdio> #include <cstring> #include <algorithm> /* * 五边形数:f(n) = sum( (-1)^(k+1) [ P(n-k(3k-1)/2) + P(n-k(3k+1)/2)] ) */ using namespace std; typedef long long ll; const int maxn = 1e5; const int mod = 1e9 + 7; ll f[maxn + 5]; void presolve() { memset(f, 0, sizeof(f)); f[0] = 1; for (int i = 1; i <= maxn; i++) { int bit = 1; for (int j = 1; j <= i; j++) { int v1 = i - j * (3 * j - 1) / 2; int v2 = i - j * (3 * j + 1) / 2; if (v1 < 0 && v2 < 0) break; if (v1 >= 0) f[i] = (f[i] + bit * f[v1]) % mod; if (v2 >= 0) f[i] = (f[i] + bit * f[v2]) % mod; bit *= -1; } f[i] = (f[i] + mod) % mod; } } int main () { presolve(); int cas, n; scanf("%d", &cas); while (cas--) { scanf("%d", &n); printf("%lld\n", f ); } return 0; }
相关文章推荐
- Java线程:创建与启动(二)
- 自定义函数的存储过程的区别
- 项目冲刺--第三天
- 完全yum安装LAMP环境并加载fastcgi模块(首发)
- 编译原理学习笔记一
- Python深入:setuptools简介
- UITableViewCell 的单元格重用机制
- 【我的安卓开发学习之旅】
- Linux学习之Makefile文件的编写
- UIGestureRecognizer手势
- hdu 4849 Professor Tian(dp)
- asp与php常用函数对照表(全)
- SQL exists not exists
- LeetCode -- Largest Number
- nginx:1、I/O模型及nginx简介
- UITabBarControloler 的简单使用
- 最大乘积
- sqlplus/RMAN/lsnrctl 等工具连接缓慢
- test04
- LeetCode -- Invert Binary Tree