Equation
来源:互联网 发布:淘宝的官换机靠谱吗 编辑:程序博客网 时间:2024/05/01 16:30
题目链接
Equation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 49 Accepted Submission(s): 18
Problem Description
Gorwin is very interested in equations. Nowadays she gets an equation like thisx1+x2+x3+⋯+xn=n , and here0≤xi≤nfor1≤i≤nxi≤xi+1≤xi+1for1≤i≤n−1
For a certainn , Gorwin wants to know how many combinations ofxi satisfies above condition.
For the answer may be very large, you are expected output the result after it modularm .
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 integern,m .
[Technical Specification]1≤T<20 1≤n≤50000 1≤m≤1000000000
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.
Sample Input
23 1005 100
Sample Output
Case #1: 2Case #2: 3
/*这可以看成是一个完全背包问题,但是由于数字比较多直接DP会超时。其实可以发现数字的种类最多是sqrt(n)级别的。那么就可以把复杂度降到nsqrt(n);Dp[i][j]代表前i个数字放好之后能组成j的和数是多少。递推方程是Dp[i][j]=dp[i][j-i]+dp[i-1][j-i]; 表示第i种数字放的时候,前面要么放了i,要么放了i-1边界条件是dp[i][j]=0 for i < jDp[0][0]=1;所以最后总的复杂度是n*sqrt(n)*/#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;int n,M,tt;int dp[50005][305];int main() { //freopen("D://imput.txt","r",stdin); int cur=1; scanf("%d",&tt); while(tt--) { scanf("%d%d",&n,&M); for(int i=1; i<=n; i++) for(int j=1; j*(j+1)/2<=i; j++) dp[i][j]=0; dp[1][1]=1; for(int i=1; i<=n; i++) { for(int j=1; j*(1+j)/2<=i&&i+j<=n; j++) { dp[i+j][j]=(dp[i+j][j]+dp[i][j])%M; if(i+j+1<=n) dp[i+j+1][j+1]=(dp[i+j+1][j+1]+dp[i][j])%M; } } int res=0; for(int i=1; i*(i+1)/2<=n; i++) { res=(res+dp[n][i])%M; } printf("Case #%d: %d\n",cur++,res); } return 0;}
0 0
- Equation
- Equation
- Difference Equation
- Batman Equation
- Heat equation
- plane equation
- Create equation
- Plane Equation
- Solve equation
- Plane Equation
- Stiff equation
- Solve equation
- Solve equation
- hdu5185 Equation
- Turing equation
- Turing equation
- FZU2102Solve equation
- Solve equation
- 云课堂-linux内核分析:通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- 大三了,该做努力了
- android监听Home键
- 拖延
- 怎样在Win7系统建立并开启Wifi热点
- Equation
- hdu 5040 bfs + 优先队列
- 轻松使用Hadoop RPC
- 通过反汇编代码探究计算机运行过程
- FileReader & BufferedReader
- pat 1028. 人口普查(20)
- 时钟与定时函数的使用
- nodejs(数据查询功能6)
- 软件项目管理过程