D. Game With Array

https://codeforces.com/problemset/problem/1355/D

We have a constructive problem where we need to construce an array with a required sum value using n elements.
Our task is to determine if there exists a value k and s - k that cannot be the sum of any subsequence from the array.
If n * 2 > s, there is no solution.
However, if n * 2 <= s, we can populate the array with (n - 1) '1' and assign (s - (n - 1)) to the last position of the array.

void solve(){
    int n, s;
    cin >> n >> s;

    if (n * 2 > s){
        cout << "No\n";
    }
    else{
        cout << "YES\n";
        for (int i = 0; i < n; ++i){
            cout << (i < n - 1 ? 1 : (s - n + 1)) << " \n"[i == n - 1];
        }
        cout << n << '\n';
    }
}
posted @ 2024-03-13 21:35  _Yxc  阅读(8)  评论(0编辑  收藏  举报