zznuli多校---1902

Description

985有n个正整数,他想知道存在多少个不同的因子对(a[i], a[j])使得
1 <= i, j <= n && i != j && a[j] % a[i] == 0,其中i和j是元素的下标。
特别地,他认为(a[i],a[j])与(a[j],a[i])是一样的因子对。

Input

第一行输入一个整数t,代表有t组测试数据。
每组数据占两行,第一行输入一个n代表元素个数,下面一行输入n个整数a[]。
注:1 <= t <= 30,1 <= n <= 1e5,1 <= a[] <= 1e6。
 

Output

一个整数代表最后的答案。

Sample Input

2
5
1 2 3 4 5
5
2 2 2 2 2

Sample Output

5
10

HINT

 

Source

hpu

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
typedef long long LL;

const int maxn=500009;
const int INF=0x3f3f3f3f;
const int mod=2009;

int cnt[maxn], fact[maxn];
int main()
{
    int T;
    scanf("%d", &T);

    while(T--)
    {
        int n, num, maxx=-1;
        scanf("%d", &n);

        memset(cnt, 0, sizeof(cnt));
        memset(fact, 0, sizeof(fact));
        for(int i=0; i<n; i++)
        {
            scanf("%d", &num);
            cnt[num]++;///统计每个数出现的次数
            maxx=max(maxx, num);
        }

        for(int i=1; i<=maxx; i++)///打印出质因数的个数
        {
            if(cnt[i])
            {
                for(int j=i+i; j<=maxx; j+=i)
                    fact[j]+=cnt[i];
            }
        }

        LL ans=0;
        for(int i=1; i<=maxx; i++)
        {
            if(cnt[i]>1)
                ans+=cnt[i]*(cnt[i]-1)/2;///相同的数出现了好多次就要先考虑
            ans+=cnt[i]*fact[i];///一个数出现的次数与其因子的个数相乘
        }
        printf("%lld
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/w-y-1/p/5796022.html