P4933大师

算法相关的学习笔记与代码记录。

P4933大师 的统一封面

P4933

用f(i,d)f(i,d)表示第ii位置保留且当前公差为d的方案数

f(i,d)=f(j,d)+1f(i,d)=f(j,d)+1

这里的+1代表直接由j转移而来,由于对于i和j来讲,公差已经固定,即

d=ai−ajd = a_{i} - a_{j} 对于负的公差,整体加个v即可

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 1e3 + 10, D = 2e4 + 10, v = 2e4, P = 998244353;

int f[N][2 * D], n, a[N], ans = 0;

int es(int x){
	return x - v;
}
int cp(int x){
	return x + v;
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i ++)
		scanf("%d", &a[i]);
	
	for(int i = 1; i <= n; i ++){
		for(int j = 1; j < i; j ++){
			f[i][cp(a[i] - a[j])] += f[j][cp(a[i] - a[j])] + 1;
			f[i][cp(a[i] - a[j])] %= P;
			
			ans = (ans + f[j][cp(a[i] - a[j])] + 1) % P;
		}
	}
	printf("%d", ans + n);
	
	return 0;
}

互动

0 人点赞 · 0 条评论


© 2026 By Catkin

Powered by Astro