P4933 大师

题目:
链接:https://www.luogu.com.cn/problem/P4933
这题本来的思路大体上是对的,就是根据已有的往后面推就行:
以i号元素结尾,公差为j的等差数列的数量 = 遍历k∈[1,i-1],dp[k][j]+1的和。
和这个大佬想的差不多,不过刚开始有点细节错误qAq

噢对了,公差为负数那就加上一个NNN = 2e4+5,这样所有公差都是正数
代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef unsigned long long ll;
using namespace std;



const int NN =998244353;
const int N = 1e3 + 2;
const int NNN = 2e4 + 5;
int h[N];
int n;
ll dpp[N][NNN*2];//前i个元素,(公差为j)+2e4的数目
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> h[i];//1-n
	ll ans = 0;
	
	for (int i = 1; i <= n; i++)
	{
		dpp[i][0] = 1;//特判:公差为0:就是只有一座塔的时候
		ans++;
		for (int j = 1; j < i; j++)
		{
			dpp[i][h[i] - h[j] + NNN] += (dpp[j][h[i] - h[j] + NNN] + 1)%NN;
			ans += dpp[j][h[i] - h[j] + NNN] + 1;
			ans %= NN;
		}
	}
	cout << ans;
	return 0;

}

posted on 2024-04-05 19:39  WHUStar  阅读(22)  评论(0编辑  收藏  举报