Equation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 270 Accepted Submission(s): 78
Problem Description
Gorwin is very interested in equations. Nowadays she gets an equation like this
x1+x2+x3+⋯+xn=n , and here
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 .
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 .
Input
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
In the next T lines, every line contain two integer n,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.
See the samples for more details.
Sample Input
2 3 100 5 100
Sample Output
Case #1: 2 Case #2: 3
<pre name="code" class="cpp">#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
//f[j][i]表示当前放到了第i个(也即使当前总容量为i),并且第i个重量最大为j
int f[320][50010]; //第一维不可太大,否则超内存
int main(){
int T; scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
int n,m;
scanf("%d%d",&n,&m);
memset(f,0,sizeof(f));
f[0][0]=1;
int k;
for(int i=1;i<=n;i++){
k=(int)(sqrt(8*n+1.0)-1)/2;
for(int j=1;j<=k;j++){
f[j][i]=(f[j-1][i-j]+f[j][i-j])%m; //第i个重量可能和第i-1重量相同均为j
}
}
int ans=0;
for(int i=1;i<=k;i++){
ans=(ans+f[i][n])%m;
}
printf("Case #%d: %d\n",cas,ans);
}
return 0;
}