波动数列

题意

长度为 n 和为 s 而且后一项总是比前一项增加 a 或者减少 b 的整数数列可能有多少种。
$1<=n<=1e3,-1e9<=s<=1e9,1<=a, b<=1e6$

题解

设数列首项为$a_1$,增量为$d_1,d_2,\cdots,d_{n-1}$,则$\large na_1+\sum\limits_{i=1}^{n-1}(n-i-1)d_i=s$
因为$a_1$可以取任意值,所以$\large \sum\limits_{i=1}^{n-1}(n-i-1)d_i\equiv s (mod n)$
$dp[i][j]$表示长度为$i$,$\large \sum\limits_{k=1}^{i-1}(n-k-1)d_k mod n=j$ 的方案数。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define oper(type) bool operator <(const type y)const
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef long double db;
typedef long long ll;
typedef int itn;
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
const ll mod = 1e8+7;
const int N = 1.1e3;
const db eps = 1e-8;
int sign(db a) { return a < -eps ? -1 : a > eps; }
int db_cmp(db a, db b){ return sign(a-b); }
void in(int &a,int &b,int &c,int &d){scanf("%d%d%d%d",&a,&b,&c,&d);}
void in(int &a,int &b,int &c){scanf("%d%d%d",&a,&b,&c);}
void in(int &a,int &b){scanf("%d%d",&a,&b);}
void in(ll &a){scanf("%lld",&a);}
void in(int &a){scanf("%d",&a);}
void in(char *s){scanf("%s",s);}
void in(db &a){scanf("%Lf",&a);}

ll dp[N][N];
int main() {
#ifdef PerpEternal
// freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin);
// freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);
#endif
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,s,a,b,nxt;
in(n,s,a,b);
s=(s%n+n)%n,a%=n,b%=n;
dp[1][0]=1;
for1(i,n-1){
for0(j,n){
nxt=(j+a*(n-i)%n)%n;
dp[i+1][nxt]=(dp[i+1][nxt]+dp[i][j])%mod;
nxt=(j-b*(n-i)%n+n)%n;
dp[i+1][nxt]=(dp[i+1][nxt]+dp[i][j])%mod;
}
}
printf("%lld\n",dp[n][s]);
return 0;
}