HDU 5185 Equation (线性dp 完全背包)
来源:互联网 发布:浙江师范学院行知学院 编辑:程序博客网 时间:2024/05/29 19:51
Equation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 64 Accepted Submission(s): 20
Problem DescriptionGorwin is very interested in equations. Nowadays she gets an equation like this
x1+x2+x3+⋯+xn=n , and here 0≤xi≤nfor1≤i≤nxi≤xi+1≤xi+1for1≤i≤n−1
For a certainn , Gorwin wants to know how many combinations of xi satisfies above condition.
For the answer may be very large, you are expected output the result after it modularm .
For a certain
For the answer may be very large, you are expected output the result after it modular
Input
Multi test cases. The first line of the file is an integerT indicates the number of test cases.
In the nextT lines, every line contain two integer n,m .
[Technical Specification]
1≤T<20
1≤n≤50000
1≤m≤1000000000
In the next
[Technical Specification]
Output
For each case output should occupies one line, the output format is Case #id: ans, here id is the data number starting from 1, ans is the result you are expected to output.
See the samples for more details.
See the samples for more details.
23 1005 100
Case #1: 2Case #2: 3
BestCoder Round #32
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5185
题目大意:问按照题目所给的公式,有多少种不同的方法得到n,方法数对m取余
题目分析:因为n比较大,直接背包,时间空间都不允许,考虑公式性质,最大的情况下获得n,即1~ma求和,ma * (ma + 1) / 2 == n
化简可以得到ma = (sqrt(8n + 1) - 1) / 2,时间空间复杂度均化为nsqrt(n),考虑dp[i][j]表示前i个数字合成数字j的种类数,则转移方程为
dp[i][j] = dp[i - 1][j - i] + dp[i][j - i],前i个数字合成j的种类数等于合成j-i时放了i和没放i两种情况的和,dp[0][0] = 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5185
题目大意:问按照题目所给的公式,有多少种不同的方法得到n,方法数对m取余
题目分析:因为n比较大,直接背包,时间空间都不允许,考虑公式性质,最大的情况下获得n,即1~ma求和,ma * (ma + 1) / 2 == n
化简可以得到ma = (sqrt(8n + 1) - 1) / 2,时间空间复杂度均化为nsqrt(n),考虑dp[i][j]表示前i个数字合成数字j的种类数,则转移方程为
dp[i][j] = dp[i - 1][j - i] + dp[i][j - i],前i个数字合成j的种类数等于合成j-i时放了i和没放i两种情况的和,dp[0][0] = 1
#include <cstdio>#include <cmath>#include <algorithm>using namespace std;int dp[317][50001];int main(){ int T, n, m; scanf("%d", &T); for(int ca = 1; ca <= T; ca++) { dp[0][0] = 1; scanf("%d %d", &n, &m); int ans = 0, ma = (sqrt(8 * n + 1) - 1) / 2; for(int j = 1; j <= n; j++) for(int i = 1; i <= min(j, ma); i++) dp[i][j] = (dp[i][j - i] + dp[i - 1][j - i]) % m; for(int i = 1; i <= ma; i++) ans = (ans + dp[i][n]) % m; printf("Case #%d: %d\n", ca, ans); }}
0 0
- HDU 5185 Equation (线性dp 完全背包)
- hdoj 5185 Equation (线性dp 完全背包)
- hdu 5185 Equation (完全背包变形)
- HDU 5185 Equation 完全背包变形.
- HDU 3127 WHUgirls (线性dp 完全背包)
- HDU 5185 Equation (DP)
- HDU 1069 Monkey and Banana (线性dp 完全背包)
- hdu 5185 dp(完全背包)+优化
- DP 完全背包 hdu 1114
- DP--完全背包--HDU - 4508
- DP完全背包 HDU 1248
- HDU 1114 完全背包dp
- [DP-完全背包] HDU 1114
- hdu 5185 Equation(DP,思路)
- hdu 5185 Equation(dp题)
- hdu 2159 dp(二维完全背包)
- HDU 1114 1248(DP,完全背包)
- 【DP|完全背包】HDU-1114 Piggy-Bank
- C++对象模型学习系列(一)
- CentOS yum安装Apache + PHP + Tomcat7 + MySQL
- 数据结构学习系列(一)
- C#动态操作DataTable(新增行、列、查询行、列等)
- 一个opencv程序的makefile模板
- HDU 5185 Equation (线性dp 完全背包)
- 安装aptana插件让myeclise编写css和js,jquery有提示!
- 【JavaScript】window对象_event事件对象
- 判断图片格式的方法
- 排序算法学习系列(一)
- 通过安装RSA密钥实现ssh免密码登陆
- 浅析CentOS和RedHat Linux的区别
- RSA 加密计算
- SSM + Shiro 整合 (2)- 实现 Spring 集成 MyBatis