(構造) CF1758D Range = √Sum

iscr發表於2024-06-27

題意:

思路:先欽定這個序列的和為4n^{2} ,那麼差值就是2n
考慮一個初始序列1,2,3,⋯,n−1,2n+1.現在我們要做的就是將這個序列變成合法的我可以進行整體都+x的操作使得這個序列的和儘量逼近4n^{2}。直接算出每個數應該加上的x,還會有一點剩餘,加到a[n-1]上即可,a[n-1]+n==a[n],而剩餘數<n,顯然他們不會相等,構造完成。

#include <bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using PII = pair<i64, i64>;
const int inf = 0x3f3f3f3f;
const i64 INF = 0x3f3f3f3f3f3f3f3f;
#define Z cout << "\n"
#define lb lower_bound
#define ub upper_bound
#define D(x) cerr << #x << ": " << (x) << "\n"
#define DV(v) cerr<<#v<<": ";for(int i=0;i<(v).size();i++)cerr<<((v)[i])<<",";cerr<<"\n"
#if 1
#define int i64
#endif
signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int>a(n + 5);
        for (int i = 1; i <= n - 1; i++)a[i] = i;
        a[n] = 2 * n + 1;
        int S = 4 * n * n;
        int sum = 0;
        for (int i = 1; i <= n; i++)sum += a[i];
        int d = (S - sum) / n;
        int s = (S - sum) - d * n;
        for (int i = 1; i <= n; i++)a[i] += d;
        a[n - 1] += s;
        for (int i = 1; i <= n; i++)cout << a[i] << ' ';
        Z;
    }

    return 0;
}

相關文章