D. Many Perfect Squares

D. Many Perfect Squares

You are given a set a1,a2,,an of distinct positive integers.

We define the squareness of an integer x as the number of perfect squares among the numbers a1+x,a2+x,,an+x.

Find the maximum squareness among all integers x between 0 and 1018, inclusive.

Perfect squares are integers of the form t2, where t is a non-negative integer. The smallest perfect squares are 0,1,4,9,16,.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t50). The description of the test cases follows.

The first line of each test case contains a single integer n (1n50) — the size of the set.

The second line contains n distinct integers a1,a2,,an in increasing order (1a1<a2<<an109) — the set itself.

It is guaranteed that the sum of n over all test cases does not exceed 50.

Output

For each test case, print a single integer — the largest possible number of perfect squares among a1+x,a2+x,,an+x, for some 0x1018.

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 x=0 the set contains two perfect squares: 1 and 4. It is impossible to obtain more than two perfect squares.

In the second test case, for x=3 the set looks like 4,9,16,25,100, that is, all its elements are perfect squares.

 

解题思路

  首先对于任意一个ai都一定存在一个x使得ai+x是一个完全平方数,因此答案至少是1。然后接下来考虑答案至少是2的情况,如果存在ai+xaj+x均是完全平方数,那么如果最终至少存在3个完全平方数,就必定会包含ai+xaj+x,可以发现此时x已经固定了,因此我们只需要枚举数组看看有多少个ak+x是完全平方数就可以了。

  n最大为50,因此可以枚举所有aiaj的组合,这里假设ai>aj。那么假设有ai+x=n2aj+x=m2,这里求解x并不是直接枚举x,而是两式做差得到aiaj=n2m2=(nm)(n+m),再把因式分解看作(nm)(n+m)=pq,其中p<q。即有aiaj=pq,这时就可以用试除法来求出aiaj的因子p,从而得到q=aiajp

  因此有{nm=pn+m=q

  解方程得到n=p+q2m=qp2,有解条件是2p+q以及2qp,即pq的奇偶性要相同。所以x=n2ai=(p+q2)2ai,这里的x还要满足0x1018。然后再枚举一遍序列统计有多少个ak+x是完全平方数就可以了。

  这里再补充个细节。实际上x只需要判断是否满足x0就行了,不需要再判断是否满足x1018,因为x是一定不会超过1018,注意到x=(p+q2)2ai<(p+q)24<(p+q)2,而p+qpq+1=ajai+1109,因此x<(109)2=1018

  当p>1q>1,那么有(p1)(q1)1,即pqpq+11,即pqp+q。而当p=1,那么q=aiaj,因此p+q=ajai+1。注意这里aiaj[1,109)

  在不超过109的数中,一个数最多有1344个因子,因此时间复杂度为O(n2109+1344n3)

  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/

posted @   onlyblues  阅读(128)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示