hdu 5185(动态规划)
2016-07-21 11:38
211 查看
Equation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 500 Accepted Submission(s): 168
[align=left]Problem Description[/align]
Gorwin 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 certain n, 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 modular m.
[align=left]Input[/align]
Multi test cases. The first line of the file is an integer T indicates the number of test cases.
In the next T lines, every line contain two integer n,m.
[Technical Specification]
1≤T<20
1≤n≤50000
1≤m≤1000000000
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
3 100
5 100
[align=left]Sample Output[/align]
Case #1: 2
Case #2: 3
[align=left]Source[/align]
BestCoder Round #32
题意:求解满足条件的 x1+x2+ ... +xn = n 的种类数。
题解:设计状态为 dp[i][j] 1- i 种数组成 j 的方案数。首先,我们可以确定 xi 的最大值为 1+2...+k = n 里面这个 k ,这样就可以将 dp[i][j]中的 i 确定成 k 了.然后 dp方程是 dp[i][j] = dp[i][j-i]+dp[i-1][j-i] 代表当选择第 i 种数的时候,前面要么选择 i 要么选择 i-1 。。然后最后再求一次和就可以了。
#include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <algorithm> #include <math.h> using namespace std; int dp[320][50005]; int main() { int tcase,t=1; scanf("%d",&tcase); while(tcase--){ int n,m; scanf("%d%d",&n,&m); int k = 0; while(k*(k+1)<=2*n) k++; memset(dp,0,sizeof(dp)); dp[0][0] = 1; for(int i=1;i<=k;i++){ for(int j=i;j<=n;j++){ dp[i][j] = (dp[i][j-i]+dp[i-1][j-i])%m; } } int ans = 0; for(int i=1;i<=k;i++){ ans = (ans+dp[i] )%m; } printf("Case #%d: %d\n",t++,ans); } return 0; }
相关文章推荐
- Log日志工具类
- jQuery.Callbacks之demo
- 移动页面HTML5自适应手机屏幕宽度
- php_lang_ref:classes and objects >> object interface
- 基于HTML5+WebSocket+JAVA的棋牌游戏开发,从入门到放弃(二)
- 百练 02 滑雪
- Android AsyncTask完全解析FutureTask 深度解析 -Java并发编程:Callable、Future和FutureTask一个使用DownloadManager下载文件的小例
- __declspec(dllexport)、__declspec(dllimport)详解
- HW3000、SI4432、A7139、SX1278、CC1120无线模块对比数据
- OpenResty学习笔记(十一) nginx做负载均衡
- Eclipse去除js(JavaScript)验证错误
- Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 nable 3ff8 to create nati
- 网页特殊符号HTML代码大全
- Codeforces Round #363 (Div. 2)->B. One Bomb
- 知识点2
- PHP中参数校验和取值
- java Lambda exp Aggregate聚合总体 Operations
- oracle创建用户,分配表空间,表空间维护
- L2范数防止过拟合以及矩阵特征值分解的几何意义
- (Plugin with id com.android.application not found)问题解决