Missing Numbers

Missing Numbers

Created by LXC on Sun May 5 19:47:40 2024

https://codeforces.com/problemset/problem/1081/E

ranting: 1900

tag: binary search, constructive algorithms, greedy, math, number theory

problem

给定长度为n(n为偶数)的数列x的偶数项,求出数列x的奇数项,使得对于任意t∈[1,n],x1+x2+…+xt为平方数,若无解输出No,否则先输出一行Yes,再输出x1,x2,x3,…,xn,若有多解输出任意一组解。

$x$ 数组奇数项不超过 ${10}^{13}$,偶数项不超过 $2 \times {10}^5$,$n \le 10^5$。

solution

设$y_i = \sum \limits_{j=1}^i a_j$,所有$y_i$均为平方数$y_i = y_i’^2$。

$y_i-y_{i-1} = (y_i’-y_{i-1}’)(y_i’+y_{i-1}’) = x_i$

设$x_i = a\times b $且$a<b, (a+b) \equiv 1 \pmod 2$,则$y_{i-1}’ = \frac{a+b}{2}-a, y_{i}’ = \frac{a+b}{2}$

我们需要枚举$x_i$的因子,便可得到$y_i$以及$y_{i-1}$

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

#include <bits/stdc++.h>
#define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
#define MOD 998244353
using namespace std;

random_device seed;
ranlux48 engine(seed());
int random(int l, int r) {
uniform_int_distribution<> distrib(l, r);
return distrib(engine);
}
template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p) {
return os<<'['<<p.first<<", "<<p.second<<']';
}
template<class t> ostream& operator<<(ostream& os,const vector<t>& v) {
os<<'['; int s = 1;
for(auto e:v) { if (s) s = 0; else os << ", "; os << e; }
return os<<']';
}
template<class t,class u> ostream& operator<<(ostream& os,const map<t,u>& mp){
os<<'{'; int s = 1;
for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; }
return os<<'}';
}

void sol() {
int n;
cin >> n;
vector<ll> a(n, -1);
for (int i=1; i<n; i+=2) {
cin >> a[i];
}
ll ps = 0;
for (int i=1; i<n; i+=2) {
ll j = 1;
while ((j+1)*(j+1)<=a[i]) j++;
for (;j>0; j--) {
if (a[i]%j == 0 && (j+a[i]/j)%2 == 0 && ((j+a[i]/j)/2-j)*((j+a[i]/j)/2-j) > ps) {
// cout << j << " " << a[i]/j << "\n";
a[i-1] = ((j+a[i]/j)/2-j)*((j+a[i]/j)/2-j) - ps;
ps += a[i-1] + a[i];
break;
}
}
}
for (ll i:a) {
if (i == -1) {
cout << "No\n";
return ;
}
}
cout << "Yes\n";
for (ll i:a) {
cout << i << " ";
}
cout << "\n";
}

int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}