HDU Coprime

Coprime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 433    Accepted Submission(s): 192

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

Source

2014 Asia AnShan Regional Contest

题意:求n个数字中取出3个数字,统计   都互质  或  都不互质  的对数。

思路:取出的3个数字a,b,c 有8种情况。

其中题目要求的只是其中两种情况,还有6种情况,要么就是1对互质对,要么有2队互质对存在。

如果能求得 ai 在n个数字中 有 k 个数字与其不互质   k2个数字与其互质。

那么题目就能转化为求所有的   sum(k*k2);

对于数字ai来说,我们知道已经有一对互质对必然存在了,但是也可能会存在第2对,因为k个中和k2中。

所以,就相当于对ai来说,有至少一对互质对,最多2个互质对的情况。 统计所有。

我们会发现,会多算了一次,/2即可。

下面就是关于如何求ai 有多少个数字与ai互质的问题了。

容斥。

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<vector>
 6 using namespace std;
 7 const int maxn = 1e5+3;
 8
 9
10 int a[maxn];
11 bool s[maxn];
12 int prime[maxn],len;
13 vector<int>Q[maxn];
14 vector<int>X[maxn];
15 int Hash[maxn];
16
17 int H[maxn],hlen;
18 void init()
19 {
20     memset(s,false,sizeof(s));
21     s[1]=true;
22     len = 0;
23     for(int i=2; i<maxn; i++)
24     {
25         if(s[i]==true)continue;
26         prime[++len]=i;
27         for(int j=i+i; j<maxn; j=j+i)
28             s[j] = true;
29     }
30     /**筛选i素因子**/
31     for(int i=1; i<=len; i++)
32     {
33         for(int j=prime[i]; j<maxn; j=j+prime[i])
34             Q[j].push_back(prime[i]);
35     }
36     int k,tmp;
37     for(int i=2; i<maxn; i++){
38         hlen = 0;
39         H[0]=-1;
40         k = Q[i].size();
41         for(int j=0; j<k; j++){
42             tmp = hlen;
43             for(int ss=0; ss<=tmp; ss++){
44                 H[++hlen]=-1*H[ss]*Q[i][j];
45                 X[i].push_back(H[hlen]);
46             }
47         }
48     }
49 }
50 int main()
51 {
52     int T,n,k;
53     init();
54     scanf("%d",&T);
55     while(T--)
56     {
57         scanf("%d",&n);
58         memset(Hash,0,sizeof(Hash));
59         for(int i=1; i<=n; i++)
60         {
61             scanf("%d",&a[i]);
62             k = X[a[i]].size();
63             for(int j=0; j<k; j++)
64                 if(X[a[i]][j]<0)
65                     Hash[-X[a[i]][j]]++;
66                 else
67                     Hash[X[a[i]][j]]++;
68         }
69         int sum;
70         __int64 tom = 0;
71         for(int i=1; i<=n; i++)
72         {
73             if(a[i]==1) continue;
74             sum = 0;
75             k = X[a[i]].size();
76             /**sum 与ai 不互质的个数**/
77             for(int j=0; j<k; j++)
78             {
79                 if(X[a[i]][j]<0)
80                     sum=sum-Hash[-X[a[i]][j]];
81                 else sum=sum+Hash[X[a[i]][j]];
82             }
83             sum --; //减去本身
84             tom = tom + sum*(__int64)(n-1-sum);
85         }
86         printf("%I64d\n",(n*(__int64)(n-1)*(n-2))/6-tom/2);
87     }
88     return 0;
89 }
时间: 2024-10-14 16:09:47

HDU Coprime的相关文章

hdu Co-prime

题意:求出在一个区间[A,B]内与N互质的个数 . 思路: 先求出n的质因子,然后求出与N的质因子不互质的个数然后总个数减去就是.用位运算二进制表示那个因子用到过,实现容斥原理.在1到n之间是c倍数的个数为n/c: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define LL __int64 5 using namespace std; 6 7 int t; 8 LL a,b,n

[容斥原理] hdu 4135 Co-prime

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1176    Accepted Submission(s): 427 Problem Description Given a number N, you are a

hdu 6025 Coprime Sequence (前后缀GCD)

题目链接:hdu 6025 Coprime Sequence 题意: 给你n个数,让你删掉一个数,使得剩下的数的gcd最大 题解: 先将这一列数的前缀后缀gcd预处理一下. 然后挨着for一下就行了 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 const int N=1e5+7; 6 int t,pre[N],suf[N],n,a[N]; 7 8

HDU 4135 Co-prime

Co-prime Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 413564-bit integer IO format: %I64d      Java class name: Main Given a number N, you are asked to count the number of integers between A and B inclusiv

hdu 5072 Coprime(数论)

题目链接:hdu 5072 Coprime 题目大意:给定N个数,问能选出多少个3元组,要么[(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1]. 解题思路:这题可以换个角度想,可以将三个数看做三角形的三条边,互质即边的颜色为1,否则为0,那么要求的即为 三条边颜色相同的三角形有多少个. 总的三角形的个数可求,那么如果求出三条边不完全相同的三角形个数,相减一下即可. 枚举顶点,然后确定以该点形成的

容斥 - HDU 4135 Co-prime

Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一个数n,求[l,r]中有多少个数与n互素. analyse: 经典的容斥原理题. 如果题目是说求1~n中有多少个数与n互质,我们一定反应应该是欧拉函数. 但是如果n特别大或者说是求某个给定区间与n互素的个数,这时用欧拉函数就行不通. 容斥做法:首先我们可以在O(sqrt(n))内求出n的所有质因数p

HDU 4135 Co-prime (分解质因数 + 容斥)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2314    Accepted Submission(s): 865 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU 5072 Coprime (莫比乌斯反演+容斥+同色三角形)

Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1469    Accepted Submission(s): 579 Problem Description There are n people standing in a line. Each of them has a unique id number. Now

HDU 5072 Coprime (单色三角形+容斥原理)

题目链接:Coprime 题面: Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1181    Accepted Submission(s): 471 Problem Description There are n people standing in a line. Each of them has a uniq