constructive algorithms math

Please click on ads to support us..

Python Code:

def solve():
    n = int(input())
    if n == 3:
        out = "NO"
    else:
        out = "YES\n"
        a, b = 1, -1
        if n%2 != 0:
            a = int((n-1)/2-1)
            b = int((1-n)/2)
        for i in range(0, n):
            if i%2 == 0:
                out += str(a)
            else:
                out += str(b)
            out += ' '
    print(out)

t = int(input())
for i in range(0, t):
    solve()

C++ Code:

#include <bits/stdc++.h>

using namespace std;

void solve(){
	int n;
	cin >> n;
	if (n == 2){
		cout << "YES" << '\n';
		cout << -1 << ' ' << 1 << '\n';
		return;
	}
	if (n == 3) {
		cout << "NO" << '\n';
		return;
	}
	cout << "YES" << '\n';
	int ans[2];
	ans[0] = (2 - n) / 2;
	ans[1] = - ans[0] + (n & 1);
	for (int i = 0; i < n; ++i)
		cout << ans[i & 1] << ' ';
	cout << '\n';
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; 
	cin >> t;

	while(t--){
		solve();
	}

	return 0;
}


Comments

Submit
0 Comments
More Questions

1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping
57C - Array
1713D - Tournament Countdown
33A - What is for dinner
810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement