Fluctuation sequence of previous exam questions (dp)

Fluctuation sequence of previous exam questions 
Time limit: 1.0s Memory limit: 256.0MB 
Submit this question 
Problem description 
  Observe this sequence: 
  1 3 0 2 -1 1 -2 …

/*
Idea: Let the first element be a0: if you want the last sum to be s
Then sum = a[0]+a[1]+...+a[n-1] = s
Since a[k] is obtained from a[k-1] by +a or -b
So any i < k if a[i] will affect him

For example: if each time is an operation of adding a, the value of a[] is as follows
a0 a0
a1 a0 + a
a2 a0 + a + a
a3  a0 + a + a + a
.....

That is to say, the first time you add a, then sum will add n-1 a
Then decrease one by one, and decrease b is similar

But regardless of adding a or subtracting b, the total number of operations is max = (n-1)*n/2 times;
The number of times +a may be any value from 0 to max. Once the number of +a is determined, it may be set to numa,
 The number of -b is then determined, and because s = N*a0 + numa*a - (max-numa)*b;
 If the number of enumeration + a, only a0 is unknown, if a0 has an integer solution, it is satisfied, add
 The number of options in this case.
 
 The last step is to calculate the number of solutions. Once numa is given, how many total possible solutions are there?
 In fact, as soon as the problem is transformed, it becomes an arbitrary number from 1, 2... n-1, which can form numa
 Number of solutions, let the problem be dp[n-1][numa]; for general dp[i][j], it means to choose from 1-i
 Arbitrary number, so that the sum is j. Special consideration for the ith number, there is the ith number existing in j, there are dp[i-1][ji] kinds
 plan. If not, there are dp[i-1][j] schemes.
 所以 : dp[i][j] = dp[i-1][j] + dp[i-1][j-i];
 If a rolling array is used, one dimension can be omitted.
 
*/
#include <iostream>
using namespace std;
typedef long long ll;

const ll mod = 100000007;
ll dp[10000006];

intmain()
{
    ll n,s,a,b;
	cin>>n>>s>>a>>b;
	ll ans = 0;
	
	dp[0] = 1;
	for(ll i = 1; i < n; i++)
        for(ll j = i*(i+1)/2; j >= i; j--)
            dp[j] = (dp[j-i] + dp[j])%mod;
        
	for(ll i = 0; i <= (n-1)*n/2; i++)
	{
		ll tmp = s + i*a - ((n-1)*n/2 - i) * b;
		if(tmp % n == 0) ans = (ans+dp[i])%mod;  
	}
	cout << ans <<endl;
    return 0;
}

  The last item in this sequence is always 2 or 3 less than the previous item.

  Dongdong is very curious about this kind of sequence. He wants to know how many integer sequences of length n and s are possible, and the latter term always increases by a or decreases by b compared to the previous term? 
Input Format 
  The first line of input contains four integers nsab, with the meanings described above. 
The output format 
  outputs a line containing an integer representing the number of scenarios that satisfy the condition. Since this number is very large, please output the remainder of dividing the scheme number by 100000007. 
Example Input 
4 10 2 3 
Example Output 

Example 
  The two sequences are 2 4 1 3 and 7 4 1 -2. 
Data scale and conventions 
  For 10% of the data, 1<=n<=5, 0<=s<=5, 1<=a, b<=5; 
  for 30% of the data, 1<=n<=30, 0<=s<=30, 1<=a,b<=30; 
  for 50% of the data, 1<=n<=50, 0<=s<=50, 1<=a,b<=50; 
  For 70% of the data, 1<=n<=100, 0<=s<=500, 1<=a, b<=50; 
  for 100% of the data, 1<=n<=1000, -1,000,000,000<= s<=1,000,000,000, 1<=a, b<=1,000,000.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812906&siteId=291194637