D. Many Perfect Squares
D. Many Perfect Squares
You are given a set of distinct positive integers.
We define the squareness of an integer as the number of perfect squares among the numbers .
Find the maximum squareness among all integers between and , inclusive.
Perfect squares are integers of the form , where is a non-negative integer. The smallest perfect squares are .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer () — the size of the set.
The second line contains distinct integers in increasing order () — the set itself.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print a single integer — the largest possible number of perfect squares among , for some .
Example
input
4 5 1 2 3 4 5 5 1 6 13 22 97 1 100 5 2 5 10 17 26
output
2 5 1 2
Note
In the first test case, for the set contains two perfect squares: and . It is impossible to obtain more than two perfect squares.
In the second test case, for the set looks like , that is, all its elements are perfect squares.
解题思路
首先对于任意一个都一定存在一个使得是一个完全平方数,因此答案至少是。然后接下来考虑答案至少是的情况,如果存在和均是完全平方数,那么如果最终至少存在个完全平方数,就必定会包含和,可以发现此时已经固定了,因此我们只需要枚举数组看看有多少个是完全平方数就可以了。
最大为,因此可以枚举所有和的组合,这里假设。那么假设有,,这里求解并不是直接枚举,而是两式做差得到,再把因式分解看作,其中。即有,这时就可以用试除法来求出的因子,从而得到。
因此有
解方程得到,,有解条件是以及,即和的奇偶性要相同。所以,这里的还要满足。然后再枚举一遍序列统计有多少个是完全平方数就可以了。
这里再补充个细节。实际上只需要判断是否满足就行了,不需要再判断是否满足,因为是一定不会超过,注意到,而,因此。
当,,那么有,即,即。而当,那么,因此。注意这里。
在不超过的数中,一个数最多有个因子,因此时间复杂度为。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 6 const int N = 60; 7 8 int a[N]; 9 10 int check(LL x) { 11 LL t = sqrt(x); 12 return t * t == x; 13 } 14 15 void solve() { 16 int n; 17 scanf("%d", &n); 18 for (int i = 0; i < n; i++) { 19 scanf("%d", a + i); 20 } 21 sort(a, a + n); 22 int ret = 1; 23 for (int i = 0; i < n; i++) { 24 for (int j = 0; j < i; j++) { 25 int s = a[i] - a[j]; 26 for (int k = 1; k <= s / k; k++) { // 求s的因子 27 if (s % k == 0) { 28 int p = k, q = s / k; 29 if (p % 2 == q % 2) { // p和q要同奇偶性才有解 30 LL x = 1ll * (p + q) * (p + q) / 4 - a[i]; 31 if (x < 0 || x > 1e18) continue; // x要在满足的范围内 32 int t = 0; 33 for (int u = 0; u < n; u++) { // 统计有多少个数加上x后是完全平方数 34 t += check(a[u] + x); 35 } 36 ret = max(ret, t); 37 } 38 } 39 } 40 } 41 } 42 printf("%d\n", ret); 43 } 44 45 int main() { 46 int t; 47 scanf("%d", &t); 48 while (t--) { 49 solve(); 50 } 51 52 return 0; 53 }
参考资料
Codeforces Round #844 (Div. 1 + Div. 2, based on VK Cup 2022 - Elimination Round) A-D:https://www.cnblogs.com/BlankYang/p/17056807.html
Codeforces Round #844 (Div. 1 + Div. 2, based on VK Cup 2022 - Elimination Round) D (数论+思维/DP+map):https://zhuanlan.zhihu.com/p/599387918
力扣第324场周赛:https://www.bilibili.com/BV1wD4y1h7Vz/
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17061544.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效