1081E - Missing Numbers - CodeForces Solution


binary search constructive algorithms greedy math number theory *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int t , n , a[N];
void solve()
{
	cin >> n;int sum = 0;
	for(int i = 2;i <= n;i += 2) cin >> a[i];
	for(int i = 2;i <= n;i += 2)
	{
		int ansa = -1;
		if(!(a[i] & 1) && !(a[i] % 4 == 0))
		{
			puts("No");
			return;
		}
		for(int j = 1;j * j <= a[i];j ++)
		{
			if(a[i] % j) continue;
			int x = j , y = a[i] / j;
			if((x & 1) ^ (y & 1)) continue;
			int A = (y - x) / 2 , B = (x + y) / 2;
			if(A * A <= sum || B * B <= sum + a[i]) continue;
			ansa = A * A;  
		}
		if(ansa == -1)
		{
			puts("No");
			return ;
		}
		a[i - 1] = ansa - sum;
		sum += a[i - 1] + a[i];
	}
	puts("Yes");
	for(int i = 1;i <= n;i ++) cout << a[i] << ' ';
}
signed main()
{
	t = 1;
	while(t --) solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag
1056A - Determine Line
670B - Game of Robots
1418C - Mortal Kombat Tower
1382B - Sequential Nim
1272C - Yet Another Broken Keyboard
808A - Lucky Year
1245A - Good ol' Numbers Coloring
58B - Coins
1041C - Coffee Break
507A - Amr and Music
1041D - Glider
1486A - Shifting Stacks
1389B - Array Walk
71B - Progress Bar
701A - Cards