题目描述:
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. Moreover, it must hold that s1+s2+⋯+sn=si+s(i+1) for each 1≤i<n.
Help MKnez to construct an array with these properties or determine that it does not exist.
大意:
MKnez希望构造满足以下条件的数组s1、s2、…、sn:
每个元素都是不同于0的整数;
对于每对相邻元素,它们的和等于整个阵列的和。
更正式地说,si≠0必须适用于每1≤i≤n。此外,对于每1≤i<n,它必须保持s1+s2+…+sn=si+si+1。
帮助MKnez构造具有这些特性的数组,或者确定它不存在。
输入格式:
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). The description of the test cases follows.
The only line of each test case contains a single integer n (2≤n≤1000) — the length of the array.
每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤100)。测试用例的描述如下。
每个测试用例的唯一一行包含一个整数n(2≤n≤1000)-数组的长度。
输出格式:
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≤si≤5000 and si≠0 should hold for each 1≤i≤n.
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.
对于每个测试用例,如果存在满足条件的长度为n的数组,则打印“是”。否则,打印“否”。如果答案为“是”,则在下一行打印满足条件的序列s1、s2、…、sn。每个元素都应该是[−5000,5000]范围内的非零整数,即−5000≤si≤5000,si≠0应适用于每个1≤i≤n。
可以证明,如果存在一个解,那么也存在一个满足范围上的附加约束的解。
如果有几个正确答案,请打印其中任何一个。
输入输出样例
输入 #1
2
2
3
输出 #1
YES
9 5
NO
思路:
通过分析可知,奇数个的值都相同,偶数个的值都相同,如果是奇数个则必然有0,与题意相悖。
代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
while(n>0){
int a;
cin>>a;
if(a%2==0){
cout<<"YES"<<endl;
while(a>0){
if(a%2==0){
cout<<"-1 ";
}
else{
cout<<"1 ";
}
a--;
}
}
else if(a==3){
cout<<"NO";
}
else{
cout<<"YES"<<endl;
int b=a-2;
b/=2;
while(a>0){
if(a%2==1){
cout<<b<<" ";
}
else{
cout<<-(b+1)<<" ";
}
a--;
}
}
cout<<endl;
n--;
}
return 0;
}