首先感谢这篇文章,让我清楚的明白思路http://blog.csdn.net/wr132/article/details/43861145
在这篇文章的基础上,我主要做了一些空间和时间上的优化。一下solve2()就是我优化后的代码,思路我就不再造无用的轮子了。
#include<iostream>
#include<string>
using namespace std;
//int dp[2][500001] = {0};
int dp[500001] = {0};
/*
void solve1()
{
int a,b,n,s;
int i,j;
int res = 0;
cin >> n >> s >> a >> b;
dp[0][0] = 1;
for(i = 1;i < n;i++)
{
for(j = 0;j <= i*(i+1)/2;j++)
{
if(j < i)
{
dp[i%2][j] = dp[(i+1)%2][j];
}
else if(j >= i)
{
dp[i%2][j] = (dp[(i+1)%2][j-i] + dp[(i+1)%2][j])%100000007;
}
}
}
int k = (i+1)%2;
int div = n*(n-1)/2;
for(j = 0;j <= div;j++)
{
if((s - (j)*a + (div-j)*b) %n == 0)
{
res = (res+dp[k][j])%100000007;
}
}
cout << res << endl;
system("pause");
}
*/
void solve2()
{
int a,b,n;
long long int s;
int i,j;
int res = 0;
cin >> n >> s >> a >> b;
dp[0] = 1;
for(i = 1;i < n;i++)
{
for(j = i*(i+1)/2;j >= 0;j--)//从右到左可用一维
{
if(j >= i)
{
dp[j] = (dp[j-i] + dp[j])%100000007;
}
}
}
long long int div = n*(n-1)/2;
long long int k;
long long int t;
//以下两行进行时间优化
k = s + div*b;
t = a+b;
for(j = 0;j <= div;j++)
{
if(k %n == 0)
{
res = (res+dp[j])%100000007;
}
k -= t;//优化
}
cout << res << endl;
system("pause");
}
int main()
{
solve2();
return 0;
}