题目
http://oj.daimayuan.top/course/10/problem/607
分析
a^2+b=c^2
b =(c+a)(c-a)
=(x+2a)(x)
对每个数b,枚举约束 x,检查是否存在对应的a即可。
一百万以内的数,最大约数个数是240。
代码
大常数选手
// http://oj.daimayuan.top/course/10/problem/607
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
int n;
int a[MAXN];
int cnt[MAXN];
int used[MAXN];
int P[200]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211,
223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461,
463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563,
569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643,
647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739,
743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937,
941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021,
1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093,
1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181,
1187, 1193, 1201, 1213, 1217, 1223};
long long ans;
int tmpp[200];
void rd(int &X)
{
X=0;
char ch=getchar();
while (ch<'0' || ch>'9') ch=getchar();
while (ch>='0' && ch<='9') X=X*10+ch-'0', ch=getchar();
}
int A;
void dfs(int x, int p, int flag)
{
int y=A/x;
if (y>x) return;
if (flag && (x-y)%2==0) {
//printf("%d\n",x);
ans+=cnt[A]*cnt[(x-y)/2];
}
if (p<0) return;
dfs(x, p-1, 0);
while (x%tmpp[p]==0) {
x/=tmpp[p];
dfs(x, p-1, 1);
}
}
void solve()
{
rd(n);
for (int i=1; i<=n; i++) {
rd(a[i]);
cnt[a[i]]++;
}
for (int i=1, ta; i<=n; i++) if (!used[a[i]]) {
used[a[i]]=1;
A=ta=a[i];
int top=-1;
for (int i=0; P[i]*P[i]<=ta; i++) if (ta%P[i]==0) {
tmpp[++top]=P[i];
while (ta%P[i]==0) ta/=P[i];
}
if (ta>1) tmpp[++top]=ta;
dfs(a[i], top, 1);
//puts("");
}
printf("%lld\n",ans);
}
int main()
{
solve();
return 0;
}
/*
int k=1, t=a[i], x1, x2;
while (k<t) {
if (t%k==0) {
x1=k;
x2=B/k;
if (x2<x1) break;
if ((x2-x1)&1) continue;
ans += cnt[(x2-x1)/2];
}
k=t/(t/(k+1));
}
for (int i=1; i<=n; i++) if (!used[a[i]]) {
used[a[i]]=1;
int B=a[i];
int L=1, R=B, x1, x2;
while (L<R) {
if (B%L == 0) {
x1=L;
x2=B/L;
if (x2<x1) break;
if ((x2-x1)%2==0) ans += cnt[(x2-x1)/2];
}
R=B/(L+1);
L=B/R;
}
if (L==R && n%L==0) ans+=cnt[0];
}
*/
这个约数枚举好像是假的,有毒
// http://oj.daimayuan.top/course/10/problem/607
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
int n;
int a[MAXN];
int cnt[MAXN];
int used[MAXN];
void solve()
{
long long ans=0;
scanf("%d",&n);
for (int i=1; i<=n; i++) {
scanf("%d",&a[i]);
cnt[a[i]]++;
}
for (int i=1; i<=n; i++) if (!used[a[i]]) {
used[a[i]]=1;
int B=a[i];
int k=1, t=a[i], x1, x2;
while (k<t) { // 枚举约数
if (t%k==0) {
x1=k;
x2=B/k;
if ((x2-x1)%2==0) ans += cnt[(x2-x1)/2];
}
k=t/(t/(k+1));
}
if (k==t) ans+= cnt[0];
}
printf("%lld\n",ans);
}
int main()
{
solve();
return 0;
}
/*
int k=1, t=a[i], x1, x2;
while (k<t) {
if (t%k==0) {
x1=k;
x2=B/k;
if (x2<x1) break;
if ((x2-x1)&1) continue;
ans += cnt[(x2-x1)/2];
}
k=t/(t/(k+1));
}
for (int i=1; i<=n; i++) if (!used[a[i]]) {
used[a[i]]=1;
int B=a[i];
int L=1, R=B, x1, x2;
while (L<R) {
if (B%L == 0) {
x1=L;
x2=B/L;
if (x2<x1) break;
if ((x2-x1)%2==0) ans += cnt[(x2-x1)/2];
}
R=B/(L+1);
L=B/R;
}
if (L==R && n%L==0) ans+=cnt[0];
}
*/