Problem Description
1 5 1 3 9 10 2
4
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; int const MAX = 1e5 + 5; int mob[MAX], p[MAX]; int a[MAX], num[MAX], has[MAX], cp[MAX]; bool prime[MAX]; int ma, n; int Mobius() { int pnum = 0; memset(prime, true, sizeof(prime)); mob[1] = 1; for(int i = 2; i < MAX; i++) { if(prime[i]) { p[pnum ++] = i; mob[i] = -1; } for(int j = 0; j < pnum && i * p[j] < MAX; j++) { prime[i * p[j]] = false; if(i % p[j] == 0) { mob[i * p[j]] = 0; break; } mob[i * p[j]] = -mob[i]; } } } ll cal() { ll all = (ll) n * (n - 1) * (n - 2) / 6; memset(cp, 0, sizeof(cp)); memset(num, 0, sizeof(num)); for(int i = 1; i <= ma; i++) { for(int j = i; j <= ma; j += i) num[i] += has[j]; for(int j = i; j <= ma; j += i) cp[j] += mob[i] * num[i]; } ll no = 0; for(int i = 0; i < n; i++) if(a[i] != 1) no += (ll) cp[a[i]] * (n - cp[a[i]] - 1); return all - no / 2; } int main() { Mobius(); int T; scanf("%d", &T); while(T--) { memset(has, 0, sizeof(has)); scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); has[a[i]] ++; ma = max(a[i], ma); } printf("%I64d\n", cal()); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5072 Coprime (莫比乌斯反演+容斥+同色三角形)
原文地址:http://blog.csdn.net/tc_to_top/article/details/47784863