B. MKnez‘s ConstructiveForces Task


原题 :

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值