题解:CF2125E Sets of Complementary Sums
发现
我们不妨假设:
-
在
a 末尾加一个1 ,s'=s+1 ,原数不变,因此Q 中每个数都会加一。 -
给
a 每个位置的值都加一,s'=s+m ,原数加一,因此每个值都会加上m-1 。
容易发现第二种情况一定会被第一种情况覆盖,因此若
然后我们再在
显然,我们必须保证初始的
现在,问题转化为对长度为
对于
考虑 dp,定义
-
给原序列所有数加一,然后在末尾加
1 。 -
给原序列所有数加一。
容易得到转移
容易想到,答案为
注意到
总时间复杂度
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5,mod=998244353;
int T,n,x,f[2][N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>T;
while(T--){
cin>>n>>x;
if(n==1){
cout<<x<<endl;
continue;
}
if(x+1<n*(n+1)/2){
cout<<0<<endl;
continue;
}
int lim=x-n+1,ans=0;
for(int j=1;j<=lim;++j) f[0][j]=0;
f[0][0]=1;
for(int i=1;i<n;++i){
for(int j=0;j<=lim;++j) f[i&1][j]=0;
for(int j=i;j<=lim;++j) f[i&1][j]=(f[i&1][j-i]+f[i-1&1][j-i])%mod;
}
for(int s=n-1;s<=lim;++s) ans=(ans+(x-(s+n-1)+1)*f[n-1&1][s])%mod;
cout<<ans<<endl;
}
return 0;
}