https://codeforces.com/problemset/problem/1779/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. Moreover, it must hold that s[1]+s[2]+...+s[n]=s[i]+s[i+1],for each 1<=i<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). 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.
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][−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.
Example
input
2 2 3
output
YES 9 5 NO
Note
In the first test case, [9,5][9,5] is a valid answer since 9+59+5 (the sum of the two adjacent elements s1+s2) is equal to 9+59+5 (the sum of all elements). Other solutions include [6,−9],[−1,−2],[−5000,5000],…[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][1,1,1] — s1+s2=1+1=2 and s1+s2+s3=1+1+1=3differ;
- [1,−1,1][1,−1,1] — s1+s2=1+(−1)=0and s1+s2+s3=1+(−1)+1=1differ;
- [0,0,0][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".
二、思路
1.对题意的理解:
对于数组s[1],s[2]...s[n]:
sum=s[1]+s[2]+...+s[n]
res=(s[1]+s[2])+(s[2]+s[3])+...+(s[n-1]+s[n]);
并且res==sum;
2.根据题目意思得出的条件:
因为 s[i]+s[i+1]=s[i]+s[i-1],所以s[i-1]=s[i+1];
3.利用数学知识进行构造:
1.当n为偶数的时候,使数组形成x,-x...-x,x的形式,为了方便输出将其统一为: 1,-1...-1,1;
2.当n为奇数的时候,假设第一个元素为a,第二个元素为b,令a+b=1;
sum=a+b+a+b+...+a=(n-1)/2+a;
res=(a+b)+(b+a)+...+(a+b)+(b+a)=(n-1);
又因为sum==res,所以(n-1)/2+a=(n-1),由此可以得到一个方程组
1.(n-1)/2+a=(n-1) 2.a+b=1;
解得:a=(n-1)/2 b=(3-n)/2;
因为题目要求n>=2,所以当n==3的时候为NO,当为>3的奇数的时候为yes
三、代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int N=2e5+10;
const int inf=0x3f3f3f3f;
int n;
int a[N];
void solve()
{
cin>>n;
int i;
if(n==2)
{
cout<<"YES"<<endl;
cout<<1<<' '<<-1<<endl;
}
else if(n==3)
cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
if(n%2==0)
{
for(i=1;i<=n;i++)
{
if(i%2==0)
a[i]=1;
else
a[i]=-1;
}
}
else
{
int l=(n-1)/2;
int r=(3-n)/2;
for(i=1;i<=n;i++)
{
if(i%2==0)
a[i]=l;
else
a[i]=r;
}
}
for(i=1;i<=n;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}
}
signed main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}