CodeForces Hello 2023 B

题目链接

B.MKnez’s ConstructiveForces Task

题目描述

MKnez wants to construct an array s1,s2,…,sn satisfying the following conditions:

  • Each element is an integer number different from 0;
  • For each pair of adjacent elements their sum is equal to the sum of the whole array.
    More formally, si≠0 must hold for each 1 ≤ i ≤ n 1≤i≤n 1in. Moreover, it must hold that s 1 + s 2 + ⋯ + s n = s i + s i + 1 s1+s2+⋯+sn=si+si+1 s1+s2++sn=si+si+1 for each 1 ≤ i < n 1≤i<n 1i<n.

Help MKnez to construct an array with these properties or determine that it does not exist.

Input

Each test contains multiple test cases. The first line contains the number of test cases t ( 1 ≤ t ≤ 100 1≤t≤100 1t100). The description of the test cases follows.

The only line of each test case contains a single integer n ( 2 ≤ n ≤ 1000 2≤n≤1000 2n1000) — the length of the array.

Output

For each test case, print “YES” if an array of length n satisfying the conditions exists. Otherwise, print “NO”. If the answer is “YES”, on the next line print a sequence s1,s2,…,sn satisfying the conditions. Each element should be a non-zero integer in the range [−5000,5000], i. e. − 5000 ≤ s i ≤ 5000 −5000≤si≤5000 5000si5000 and s i ≠ 0 si≠0 si=0 should hold for each 1 ≤ i ≤ n 1≤i≤n 1in.

It can be proved that if a solution exists then there also exists one which satisfies the additional constraints on the range.

If there are several correct answers, print any of them.

Example

inputCopy

2
2
3

outputCopy

YES
9 5
NO

Note

In the first test case, [9,5] is a valid answer since 9+5 (the sum of the two adjacent elements s1+s2) is equal to 9+5 (the sum of all elements). Other solutions include [6,−9],[−1,−2],[−5000,5000],…
For the second test case, let us show why some arrays do not satisfy the constraints:

[1,1,1] — s1+s2=1+1=2 and s1+s2+s3=1+1+1=3 differ;
[1,−1,1] — s1+s2=1+(−1)=0 and s1+s2+s3=1+(−1)+1=1 differ;
[0,0,0] — The array s cannot contain a 0.
This is not a proof, but it can be shown that the answer is “NO”.

题目翻译:

给你一个长度 n,构造一个数组,数组的元素是 s 1 , s 2 , s 3 , . . s n s1,s2,s3 , ..sn s1,s2,s3,..sn,其中任意两个相连元素的和 s i + s i + 1 s_i + s_{i+1} si+si+1 等于整个数组的和,并且数组中的每一个元素都不能为0.

如果可以构造这样的一个数组,输出 YES,并输出这个数组。
不可以就输出 NO。

题目分析

需要对数组的长度 n 分类讨论

  • n 是偶数时,我们只需要让数组中任意两个相连元素为相反数即可满足要求,例如 [ 1 , -1 , 1 , -1 ] , [ -1 , 1 , -1 , 1 , -1 , 1 ] 都满足要求。
  • n 是奇数时,只有 n > = 5 n >= 5 n>=5 才能满足要求。因为 n = 3时( 2 < = n < = 1000 2 <= n <= 1000 2<=n<=1000),[ s1 , s2 , s3] , s 1 + s 2 = s 1 + s 2 + s 3 s1 + s2 = s1 + s2 + s3 s1+s2=s1+s2+s3 得出 s 3 = 0 s3 = 0 s3=0不满足题目要求。
    • n > = 5 n >= 5 n>=5时,[ s1 , s2 , s3 , … , si-1 , si , si+1 , … , sn] , s i − 1 + s i = s i + s i + 1 s_{i-1} + s_i = s_i + s_{i+1} si1+si=si+si+1 , 即 s i − 1 = s i + 1 s_{i-1} = s_{i+1} si1=si+1。所以数组的形式就为 [ a , b , a , b , a , …, b , a ]。又因为 n = 2 k + 1 n = 2k + 1 n=2k+1 (n 是 奇数),所以整个数组的和为 a + b = ( k + 1 ) a + k b a + b = (k + 1)a + kb a+b=(k+1)a+kb , 再得出 k a + ( k − 1 ) b = 0 ka + (k-1)b = 0 ka+(k1)b=0, 最后得出 a = k − 1 , b = − k a = k - 1 , b = -k a=k1,b=k
    • 举例:当 n = 5 n = 5 n=5 时,构造的数组为 [ 1 , -2 , 1 , -2 ,1 ] ; 当 n = 7 n = 7 n=7 时,构造的数组为 [2 , -3 , 2 , -3 , 2 , -3 , 2 ]

代码:

#include<iostream>
using namespace std;

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n ;
        scanf("%d",&n);
        bool odd = false;
        if(n % 2){
            if(n < 5){
                puts("NO");
                continue;
            }
            else odd = true;
        }
        puts("YES");
        if(odd){
            int k = n / 2;
            for(int i = 1;i <= n;i++){
                if(i % 2) printf("%d ",k-1);
                else printf("%d ",-k);
            }
        }
        else{
            for(int i = 1;i <= n;i++){
                if(i % 2) printf("-1 ");
                else printf("1 ");
            }
        }
        puts("");
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值