题目
Description
这天,小A得到了一个序列a[1],a[2]…a[n],他想知道有多少个二元组(i,j)满足i!=j且a[i]是a[j]的因数。
Input
第一行有一个正整数n,第二行包含n个整数。
Output
输出一行,表示满足条件的二元组个数。
Sample Input
5
2 4 5 2 6
Sample Output
6
Hint
对于前30%的数据,n,k<=1000.
对于100%的数据,n<=2000000,a[i]<=2000000。
思路
一开始我用gcd爆了,后来就改成了先用一个桶装进去,然后在累加上去的办法就好了
代码
#include <cmath>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define N 2000000
using namespace std;
int a[N];
int n,ans,maxx;
int main()
{
scanf("%d" ,&n);
for (int i=1;i<=n;i++)
{
int x;
scanf("%d" ,&x);
a[x]++;
maxx=max(x,maxx);
}
for (int i=1;i<=maxx;i++)
{
int tot;
if (a[i]>0)
tot=-1;
for (int j=1;j<=maxx / i;j++)
tot=tot+a[i*j];
ans+=tot*a[i];
}
printf("%d" ,ans);
}