洛谷P4933 大师

\(\Large\textbf{Description: } \large{有n个数字从左到右排列,每次你可以去掉若干个数字,如果去掉后,求有多少种方案使剩下的数字构成一个等差数列。(n \leq 1000)}\)

\(\Large\textbf{Solution: } \large{考虑dp。\text{f[i][j]}表示到第i个数字,公差为j的数量,那么容易想到n^2转移。不过公差可能有负数,把数组整体右移或者map即可。}\)

\(\Large\textbf{Code: }\)

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int s = 20000;
const int N = 1e3 + 5;
const int M = 2e4 + 10;
const int p = 998244353;
int n, h[N], f[N][M << 1];

inline int read() {
	int ans = 0, flag = 1;
	char ch = getchar();
	while (ch > '9' || ch < '0') {
		if (ch == '-') flag = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	return ans * flag; 
}

int main() {
	n = read();
	for (int i = 1; i <= n; ++i) h[i] = read();
	LL ans = 0;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= i - 1; ++j)
			f[i][h[i] - h[j] + s] = (f[i][h[i] - h[j] + s] + f[j][h[i] - h[j] + s] + 1) % p, ans = (ans + f[j][h[i] - h[j] + s] + 1) % p;
	ans = (ans + n) % p;
	printf("%d\n", ans);
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/Miraclys/p/12557975.html
今日推荐