首先要满足三个条件1、该数组最大值不超过 k。2.该数组所有数都不相同。3. 数组所有数之和等于x。
可以先考虑构造一个所有数都不同的序列,1,2,3....n
接着考虑这个序列的和和x的差值,将差值除以n的数字就是每个数字应该加的增量。序列变为1+d,2+d,3+d....n+d
但是为了凑上和为x,要算出最后小于n的那个需要另外加上的数字ans(ans<n),将这个数字加到后面ans个数字上最后变成 1+d,2+d...n-ans+1+d,...n+d+1
为什么这么操作?
为了在数字不同的情况下,让所有数字尽可能小,来满足第一个条件 代码如下
#include <iostream>
#define ll long long
#define endl "\n"
using namespace std;
ll n, m, k,x; int T, ncase = 0; const int N = 1e5 + 3,mod=1e9+7;
ll a[N];
ll calc(ll l, ll r)
{
/*cout<< n*(l+r)/2<<endl;*/
return n*(l+r)/2;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n>>k>>x;
ll ans=calc(1,n);
ll d=(x-ans)/n;
/* cout<<d<<endl;*/
ans=x-ans-d*n;//比n小
if(ans==0){
if(k<n+d){
cout<<-1;return 0;
}
for(int i=1;i<=n;i++){
cout<<i+d<<" ";
}return 0;
}
if(k<n+d+1){
cout<<-1;return 0;
}
for(int i=1;i<=n;i++){
if(i>=n-ans+1){
cout<<i+d+1<<" ";continue;
}
cout<<i+d<<" ";
}
return 0;
}