dp(hdu5185-Equation)
来源:互联网 发布:淘宝购物评级一颗星 编辑:程序博客网 时间:2024/06/05 14:13
Problem Description
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 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.
Sample Input
23 1005 100
Sample Output
Case #1: 2Case #2: 3
题意:给出n个数字,数字满足 x[i]<=x[i+1]<=x[i]+1;要使得这n个数的和为n,有多少种方案。
思路:dp[i][j]表示以i为结尾的组成和为j的数量是多少
转移方程:dp[i][j]=dp[i-1][j-i]+dp[i][j-i];dp[i-1][j-i]表示以i-1结尾的和为j-i(为什么要是i-1结尾那?因为要求x(i)<=x(i+1)<=x(i)+1),加上i和为j;dp[i][j-1]表示以i结尾的组成和为j-i,这次还可以用i
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=50010;const int maxm=320;int dp[maxm][maxn];int N,M;int main(){ int T,cas=1; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&M); memset(dp,0,sizeof(dp)); int ans=0; dp[0][0]=1; for(int i=1;i<=N;i++) { int cnt=min((int)sqrt(2*i),i); for(int j=1;j<=cnt;j++) dp[j][i]=(dp[j-1][i-j]+dp[j][i-j])%M; } int cnt=min((int)sqrt(2*N),N); for(int i=1;i<=cnt;i++) ans=(ans+dp[i][N])%M; printf("Case #%d: %d\n",cas++,ans); } return 0;}
0 0
- dp(hdu5185-Equation)
- hdu5185 Equation DP
- hdu5185 Equation
- 【DP】 HDOJ 5185 Equation
- HDU 5185 Equation (DP)
- 【DP】 hdu4249 A Famous Equation
- HDOJ 4249 A Famous Equation DP
- HDU-4249-A Famous Equation(DP)
- HDU 5185 Equation (线性dp 完全背包)
- hdoj 5185 Equation (线性dp 完全背包)
- HDU:4249 A Famous Equation(数位DP)
- hdu 5185 Equation(DP,思路)
- hdu 5185 Equation(dp题)
- Equation
- Equation
- hdu 4249 A Famous Equation (dp 细节)
- HDU 4249 A Famous Equation(数位DP)
- hdu 4249 A Famous Equation【DP】【Fudan Local Programming Contest 2012 E】
- C++连接出错问题解决 DCI 数据库
- 二级指针的用法
- HDU 1002 A+B problem
- 数值计算
- 算法 quick sort
- dp(hdu5185-Equation)
- 新功能:Azure Traffic Manager 嵌套配置文件
- tomcat Win64/Linux64 下的安装
- linux命令ps aux|grep xxx详解
- 我比较喜欢编程,也喜欢自学一些新的东西
- Augmented reality
- 算法 Heap sort
- 有关ASM和CGLIB的介绍
- SQLite数据库框架ORMLite与GreenDao的简单比较