原题 :
MKnez wants to construct an array s1,s2,…,sns1,s2,…,sn satisfying the following conditions:
- Each element is an integer number different from 00;
- For each pair of adjacent elements their sum is equal to the sum of the whole array.
More formally, si≠0si≠0 must hold for each 1≤i≤n1≤i≤n. Moreover, it must hold that s1+s2+⋯+sn=si+si+1s1+s2+⋯+sn=si+si+1 for each 1≤i<n1≤i<n.
Help MKnez to construct an array with these properties or determine that it does not exist.
整体思路:
如果是偶数 1 -1 输出即可
如果是奇数,3是不行的
然后其他的n的话,把两项看成一个整体,两项的和为x,那么对于前n-1项来说就是差m=(n-1)/2*x,然后最后一项就是要实现所有和为x,就是说最后一项就是x-m,然后奇数项就是x-m然后偶数项就是m。代码里x=2代替。
#include <bits/stdc++.h>
using namespace std;
long long n;
void solve(){
if(n%2==0)
{
cout<<"YES"<<endl;
for(int i=0;i<n;i++)
{
if(i%2==0) cout<<"1 ";
else cout<<"-1 ";
}
cout<<endl;
return;
}
else if(n==3)
{
cout<<"NO"<<endl;
return;
}
else
{
int a=n-3;
int b=1-n;
cout<<"YES"<<endl;
for(int i=0;i<n;i++)
{
if(i%2==0) cout<<a<<" ";
else cout<<b<<" ";
}
cout<<endl;
return ;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
solve();
}
return 0;
}