【bzoj4146】[AMPPZ2014]Divisors

*题目描述:
给定一个序列a[1],a[2],…,a[n]。求满足i!=j且a[i]|a[j]的二元组(i,j)的个数。

*输入:
第一行包含一个正整数n(1<=n<=2000000),表示序列长度。
第二行包含n个正整数,依次表示a[1],a[2],…,a[n] (1<=a[i]<=2000000)。

*输出:
一个整数,即满足条件的二元组的个数。

*样例输入:
5
2 4 5 2 6

*样例输出:
6

*提示:
满足条件的6组分别为(1,2),(1,4),(1,5),(4,1),(4,2),(4,5)。

*题解:
因为之前校内训练时做过的一道类似的题而导致看到这道题时被误导了。。。(之前那道题是和前后位置有关的,这题没有关系)计cnti 为i这个数的出现次数。然后对于每个数枚举它的倍数,答案为cnticntj,如果倍数是自己的话,答案为cnti(cnti1)

*代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

#ifdef WIN32
    #define LL "%I64d"
#else
    #define LL "%lld"
#endif

#ifdef CT
    #define debug(...) printf(__VA_ARGS__)
    #define setfile() 
#else
    #define debug(...)
    #define filename ""
    #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout)
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
#define cabs(_x) ((_x) < 0 ? (- (_x)) : (_x))
char B[1 << 15], *S = B, *T = B;
inline int F()
{
    R char ch; R int cnt = 0; R bool minus = 0;
    while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
    ch == '-' ? minus = 1 : cnt = ch - '0';
    while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
    return minus ? -cnt : cnt;
}
#define maxn 2000010
int cnt[maxn];
int main()
{
//  setfile();
    R int n = F(), maxx = 0;
    for (R int i = 1; i <= n; ++i)
    {
        R int a = F();
        ++cnt[a]; cmax(maxx, a);
    }
    R long long ans = 0;
    for (R int i = 1; i <= maxx; ++i)
    {
        if (!cnt[i]) continue;
        cnt[i] > 1 ? ans += 1ll * (cnt[i] - 1) * cnt[i] : 0;
        for (R int j = i << 1; j <= maxx; j += i)
            cnt[j] ? ans += 1ll * cnt[i] * cnt[j] : 0;
    }
    printf("%lld\n", ans );
    return 0;
}
posted @ 2016-09-11 17:23  cot  阅读(155)  评论(0编辑  收藏  举报