F. Array Beauty
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s call beauty of an array
b
1
,
b
2
,
⋯
 
,
b
n
(
n
>
1
)
—
min
1
≤
i
≤
j
≤
n
∣
b
i
−
b
j
∣
b_1,b_2,\cdots,b_n (n>1) — \underset{1\le i\le j\le n}{\min} |b_i-b_j|
b1,b2,⋯,bn(n>1)—1≤i≤j≤nmin∣bi−bj∣
You’re given an array
a
1
,
a
2
,
⋯
 
,
a
n
a_1,a_2,\cdots, a_n
a1,a2,⋯,an and a number k. Calculate the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353.
A sequence a is a subsequence of an array
b
b
b if a can be obtained from
b
b
b by deletion of several (possibly, zero or all) elements.
Input
The first line contains integers
n
,
k
(
2
≤
k
≤
n
≤
1000
)
n,k (2≤k≤n≤1000)
n,k(2≤k≤n≤1000).
The second line contains n integers
a
1
,
a
2
,
⋯
 
,
a
n
(
0
≤
a
i
≤
1
0
5
)
a_1,a_2,\cdots,a_n (0≤ai≤10^5)
a1,a2,⋯,an(0≤ai≤105).
Output
Output one integer — the sum of beauty over all subsequences of the array of length exactly k k k. As this number can be very large, output it modulo 998244353.
Example
input
4 3
1 7 3 5
output
8
input
5 5
1 10 100 1000 10000
output
9
Note
In the first example, there are 4 subsequences of length 3 — [1,7,3], [1,3,5], [7,3,5], [1,7,5], each of which has beauty 2, so answer is 8.
In the second example, there is only one subsequence of length 5 — the whole array, which has the beauty equal to |10−1|=9.
题解
给定一个数组,对于长度为
k
k
k的子序列(任意组合),定义序列的值为
min
1
≤
i
≤
j
≤
n
∣
b
i
−
b
j
∣
\underset{1\le i\le j\le n}{\min} |b_i-b_j|
1≤i≤j≤nmin∣bi−bj∣,就是序列中相连数字差最小的位序列的值,问所有长度为
k
k
k的子序列的值的和为多少。
对于给定的数组,由于不是有序的,所有相连的值最小,肯定要先进行排序(从小到大),得到一个有序的数组,这样得到的子序列也是有序的。
假设一个序列的为
x
x
x,那么意味着
a
r
r
[
i
]
−
a
r
r
[
i
−
1
]
≥
x
arr[i]-arr[i-1]\ge x
arr[i]−arr[i−1]≥x,即所有的相连数字之差都大于等于
x
x
x。
如果一个我们要找序列的值为1的个数为
N
N
N,那么和就是
1
×
N
1\times N
1×N,如果序列的的值为
2
2
2的个数为
M
M
M,那么和就是
2
×
M
2\times M
2×M,但是在
N
N
N个序列中肯定包含值为2的
M
M
M个序列,那么总的和就是
N
+
2
×
M
−
M
×
1
=
N
+
M
N+2\times M -M\times 1=N+M
N+2×M−M×1=N+M(
M
×
1
M\times 1
M×1就是在值为1中重复的个数),这样我们就可以发现我们从小到大连续 的序列的值,就是将他们的序列个数相加。
用动态规划的思想。
d
p
[
i
n
d
e
x
]
[
l
e
n
]
dp[index][len]
dp[index][len]
其中:**
i
n
d
e
x
index
index**表示第index数组加入序列,
l
e
n
len
len表示此时序列的长度为len,那么
d
p
[
i
n
d
e
x
]
[
l
e
n
]
dp[index][len]
dp[index][len]就表示第index个数字加入,此时长度为len的子序列的个数。
假设此时序列的值为
x
x
x
那么对于第index个数字,如果它不加入序列,那么此时就可以表示为
d
p
[
i
n
d
e
x
]
[
l
e
n
]
+
=
d
p
[
i
n
d
e
x
−
1
]
[
l
e
n
]
dp[index][len] += dp[index-1][len]
dp[index][len]+=dp[index−1][len]
如果加入序列,那么我们需要找到一个位置让
a
r
r
[
i
n
d
e
x
]
−
a
r
r
[
i
]
≥
x
1
≤
i
≤
n
o
w
arr[index]-arr[i]\ge x\quad 1\le i\le now
arr[index]−arr[i]≥x1≤i≤now
d
p
[
i
n
d
e
x
]
[
l
e
n
]
+
=
d
p
[
n
o
w
]
[
l
e
n
−
1
]
dp[index][len] += dp[now][len-1]
dp[index][len]+=dp[now][len−1]
综合起来就是
d
p
[
i
n
d
e
x
]
[
l
e
n
[
=
d
p
[
i
n
d
e
x
−
1
]
[
l
e
n
]
+
d
p
[
n
o
w
]
[
l
e
n
−
1
]
dp[index][len[ = dp[index-1][len] + dp[now][len-1]
dp[index][len[=dp[index−1][len]+dp[now][len−1]
最后对于序列值为
x
x
x,得到的序列个数就是
d
p
[
n
]
[
k
]
dp[n][k]
dp[n][k],由于这个值和
x
x
x有关,记为
f
(
x
)
f(x)
f(x)
那么最终的结构就是
a
n
s
=
∑
x
=
1
1
0
5
k
−
1
f
(
x
)
ans = \sum_{x=1}^{\frac{10^5}{k-1}}f(x)
ans=x=1∑k−1105f(x)
#include <bits/stdc++.h>
using namespace std;
const int MaxM = 1e5+100;
const int MaxN = 1100;
const int MOD = 998244353;
int dp[MaxN][MaxN];
int arr[MaxN];
int n,k;
int Solve(int x) {
dp[0][0] = 1;
int now = 0;
for(int i = 1;i<=n;i++) {
while(arr[i]-arr[now+1] >= x) now++;
dp[i][0] = dp[i-1][0];
for(int j = 1;j<=k;j++) {
dp[i][j] = (dp[i-1][j]+dp[now][j-1])%MOD;
}
}
return dp[n][k];
}
int main() {
scanf("%d %d",&n,&k);
for(int i = 1;i<=n;i++) {
scanf("%d",&arr[i]);
}
sort(arr+1,arr+n+1);
int ans = 0;
for(int i = 1;i*(k-1)<=MaxM;i++) {
ans = (ans+Solve(i))%MOD;
}
printf("%d\n",ans);
return 0;
}