LuoGu: CF1081E Missing Numbers
CF: E. Missing Numbers
考虑前面 $i - 1$ 个数都是合法的,现在加入第 $i$ 个数 $w$ 。
那么有 $a ^ 2 - b ^ 2 = w$ ,即 $(a + b) (a - b) = w$ 。将 $w$ 分解质因数即可解得两个答案,注意保证答案合法。因为对答案有大小限制,所有尽量保证答案尽可能小,所以分解质因数时优先考虑两个因数差更小的情况,这样平方会更小。
查看代码
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
| #include <cstdio> #include <cmath> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef long long LL; const int N = 1e5 + 10; LL w[N], v[N]; int n; int main () { read(n); for (int i = 2; i <= n; i += 2) { read(w[i]); bool flag = false; for (int j = sqrt(w[i]) + 0.5; j && !flag; j--) { if (w[i] % j || (j & 1) ^ (w[i] / j & 1)) continue; v[i] = w[i] / j + j >> 1; v[i - 1] = w[i] / j - j >> 1; flag = v[i - 1] > v[i - 2] && v[i] * v[i] > w[i]; } if (!flag) return puts("No"), 0; } for (int i = 1; i <= n; i += 2) w[i] = v[i] * v[i] - v[i - 1] * v[i - 1]; puts("Yes"); for (int i = 1; i <= n; i++) write(w[i]), putchar(' '); return 0; }
|