题解:CF2247B Yet Another Constructive

· · 题解

题解:CF2247B Yet Another Constructive 题解。

构造一个长为 n 的正整数序列,要求子序列之和能被 m 整除的最短子序列长度为 k。

我们可以用贪心解决此题。

由于序列中都是正整数,则 m<k 的情况是不可能的。

对于其他可行的情况,首先先往序列中添加 k-1 个 1,然后再添加 m-k+1,这样一来序列中就有 k 个元素,且和恰好为 m。接下来只要再添加 1 直到序列长度到 n 即可,显然,这样的序列能被 m 整除的最短子序列长度只能为 k。

那么这题就解决了。

//coder:shimizu_kiouka
//problem:#CF2247B
//2026/07/18
//And in that light,I find the deliverance.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t;
ll n,k,m;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        ll a[303030];
        ll cnt=0;
        cin>>n>>k>>m;
        if(k>m){
            cout<<"NO\n";
            continue;
        }
        cout<<"YES\n";
        for(int i=1;i<k;i++){
            a[++cnt]=1;
        }
        a[++cnt]=m-k+1;
        while(cnt<=n){
            a[++cnt]=1;
        }
        for(int i=1;i<cnt;i++){
            cout<<a[i]<<' ';
        }
        cout<<'\n';
    }
}