这题思路还是有点巧的。。
首先要意识到每个数的平均值是1,那么只有首位取了0才有很多方案。。
然后考虑到这题的状态参数实在是太多了。。要好好选取。。不然tle。。
参数大概有这些。。非0区间的长度,区间总长度n,某个子区间的最后一位数以及子区间和。。。
其中除了子区间的最后一位数之外,其他参数均可达到n。。
分析一下最后一位数t最大的情况。。即直接单调递增。。其和显然为t*(t+1)/2。。这样可以看出这个t是在sqrt(n)级别的。。
考虑到n的范围。。所以要尽量取这个sqrt(n)。。
然后其实如果把区间和限制在n以内,把最后一位数也限制好了,那么区间长度是不可能超过n的。。这样就确定了状态
设d[i][j]为以i为尾数,j为区间长度和的方案数
d[i][j]=d[i-1][j-i]+d[i][j-i]
其实这个本质上就是个背包。。只是进入包的顺序有点小小的限制。。
实现上用滚动数组。。然后别开longlong。。会T。。
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 50005
#define nm 1000005
#define pi 3.1415926535897931
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,inf,d[NM],f[NM],ans;
int main(){
int _=read();inc(p,1,_){
n=read();inf=read();
inc(i,1,n)f[i]=1;ans=1;
for(int i=2;i*(i+1)<=2*n;i++){
mem(d);
inc(j,i*(i+1)/2,n){
d[j]=(f[j-i]+d[j-i])%inf;
}
inc(j,1,n)f[j]=d[j];
(ans+=f[n])%=inf;
}
printf("Case #%d: %d\n",p,ans);
}
return 0;
}
Equation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 744 Accepted Submission(s): 272
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
Source
Recommend
Statistic | Submit | Discuss | Note