传送门
Problem Description
There are n people standing in a line. Each of them has a unique id number.
Now the Ragnarok is coming. We should choose 3 people to defend the evil. As a group, the 3 people should be able to communicate. They are able to communicate if and only if their id numbers are pairwise coprime or pairwise not coprime. In other words, if their id numbers are a, b, c, then they can communicate if and only if [(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1], where (x, y) denotes the greatest common divisor of x and y.
We want to know how many 3-people-groups can be chosen from the n people.
Input
The first line contains an integer T (T ≤ 5), denoting the number of the test cases.
For each test case, the first line contains an integer n(3 ≤ n ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by a single space, where ai stands for the id number of the i-th person.
Output
For each test case, output the answer in a line.
Sample Input
1 5 1 3 9 10 2
Sample Output
4
#include
#include
#include
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
int mob[maxn], p[maxn];
int a[maxn], num[maxn], has[maxn], cp[maxn];
bool prime[maxn];
int ma, n;
/*
莫比乌斯,emmm,都是抄的
*/
void Mobius()
{
int pnum = 0;
memset(prime, true, sizeof(prime));
mob[1] = 1;
for(int i = 2; i < maxn; i++)
{
if(prime[i])
{
p[pnum ++] = i;
mob[i] = -1;
}
for(int j = 0; j < pnum && i * p[j] < maxn; 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 ans = 0;
for(int i = 0; i < n; i++)
if(a[i] != 1)
ans += (ll) cp[a[i]] * (n - cp[a[i]] - 1);
return all - ans / 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());
}
}
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i=a;i--)
const int maxn=1e5+10;
vector vec;
int cnt[maxn],val[maxn],n;
/*
真真切切的体验到了,卡常数(当然是在我的算法不够优化的前提下)
1.能用int,别用long long
2.对于找到sqrt(n),我们还是直接算 i*i吧,太难受了,一下子200ms下去了
*/
/*
题意:问我们在n个数中,有多少 三元组是 都互质,或者都不互质
思路:
考虑白书上的,单色三角形的计数法则(逆向考虑有多少个不满足条件的,然后C(n,3)减掉即可
1.每个顶点,假设 与M个数互质,则有n-1-m个数不互质,那么就会产生 m*(n-1-m)个 非单色三角形
2.每个非单色三角形都会有两个顶点,连着互质和不互质的两条边,所以算了两次,/2就好了
所以问题变成了,怎样计算与n个数中与某个数字互质的个数?
考虑这个数的素因子,然后容斥定理
*/
inline void get_prime(int X){
// printf("xxx:%lld\n",X);
// int m=sqrt(X+0.5);
for(int i=1;i*i<=X;i++){
if(X%i==0){
// printf("x:%ll i:%lld\n",X,i);
cnt[i]++;
if(X!=i*i){
cnt[X/i]++;
}
}
}
}
//计算n个数中与val[id]互质的数量
inline ll get_ans(int id){
vec.clear();
//int m=sqrt(val[id]+0.5),v=val[id];
int v=val[id];
for(int i=2;i*i<=v;i++){
if(v%i==0){
vec.push_back(i);
while(v%i==0)v/=i;
}
}
if(v>1) vec.push_back(v);
ll ans=n;
int sz=vec.size();ll ma=1ll<