洛谷 P4993 大师 (dp)

题目链接:https://www.luogu.com.cn/problem/P4933

题目大意: 给定一列数字 \(a[i]\),求有多少个等差子序列,其中一个数字和两个数字也算等差数列。

题解:设 \(dp[i][j]\) 表示以 \(a[i]\) 结尾,公差为 \(j\) 的等差数列数量,转移时枚举前一个数字。初始化时将所有的 \(dp[i][j]\) 赋为 \(1\),统计答案时再将所有 \(1\) 减掉,答案即为所有的 \(dp[i][j]\) 之和。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 1010;
const int M = 998244353;

int n;
int h[maxn];
int dp[maxn][100010];

ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
	n = read();
	for(int i = 1 ; i <= n ; ++i) h[i] = read();
	
	memset(dp, -1, sizeof(dp));
	
	for(int i = 1 ; i <= n ; ++i){
		for(int j = -40000 ; j <= 40000 ; ++j){
			dp[i][j+40000] = 1;
		}
		
		for(int j = 1 ; j < i ; ++j){
			dp[i][h[i]-h[j]+40000] = (dp[i][h[i]-h[j]+40000] + dp[j][h[i]-h[j]+40000]) % M;
		}
	}
	
	int ans = n;
	for(int i = 1 ; i <= n ; ++i){
		for(int j = -40000 ; j <= 40000 ; ++j){
			ans = (ans + (dp[i][j+40000] - 1)) % M;
		}
	}
	
	printf("%d\n", ans);
	
	return 0;
}
posted @ 2021-07-27 17:59  Tartarus_li  阅读(43)  评论(0编辑  收藏  举报