hdu5072 Coprime 2014鞍山现场赛C题 计数+容斥

http://acm.hdu.edu.cn/showproblem.php?pid=5072

Coprime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 354    Accepted Submission(s): 154

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个数,问三个数两两互质或者两两不互质的三元组有多少种。。

分析:当时做重现的真心不会233,今天翻大白发现竟然有这个模型233。(p105 问题6)

就是经典的单色三角形模型,从反面考虑,求出所以非单色三角形即可。对于每一个数,求与其互质和不互质的个数是多少个,记与其互质的是ai个,那么包括第i个数的就有ai*(n-1-ai)种,最后求和,注意到每个非单色三角形出现了两次,所以还要除2.最后用总的情况C(n,3)减去就好。

现在的问题是如何求与每个数互质的个数。求1到k与x互质的数的个数用容斥搞,详见poj1142.

那么这道题求n个数与x互质的数个数也用容斥。由于数的范围不大,只有10^5,所以我们可以预处理出1到10^5每个数作为是这n个数中多少个数的因子。然后求出x的质因子,然后再容斥计算就好。。。。

唉。。还是见识短浅做题少。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x7fffffff;
const LL inf=(((LL)1)<<61)+5;
const int N=100005;
const int M=500;
bool isprime[M];
int prime[M];
int tot=0,n;
int fac[50];
bool have[N];
int a[N];
int s[N];  //sum[i]表示a[i]中能整除i的个数
void getprime()
{
    for(int i=2;i<=M;i++)
    {
        if(isprime[i]) continue;
        prime[tot++]=i;
        for(int j=i*i;j<=M;j+=i)
            isprime[i]=true;
    }
}
LL gao()
{
    LL ans=0;
    for(int i=0;i<n;i++)
    {
        int m=a[i],num=0;
        LL sum=0;
        for(int j=0;j<tot&&prime[j]*prime[j]<=m;j++)
        {
            if(m%prime[j]==0)
            {
                fac[num++]=prime[j];
                while(m%prime[j]==0)
                    m/=prime[j];
            }
        }
        if(m!=1) fac[num++]=m;
        for(int j=1;j<(1<<num);j++)
        {
            int op=0,tmp=1;
            for(int k=0;k<num;k++)
            {
                if((1<<k)&j)
                {
                    tmp*=fac[k];
                    op++;
                }
            }
            if(op&1) sum+=s[tmp];
            else sum-=s[tmp];
        }
        if(sum==0) continue;
        ans+=(sum-1)*(n-sum);  //sum不互质的数包括了a[i]自身故减1
    }
    return ans/2;
}
int main()
{
    int t;
    scanf("%d",&t);
    getprime();
    while(t--)
    {
        scanf("%d",&n);
        clr(have);
        clr(s);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            have[a[i]]=true;
        }
        for(int i=2;i<N;i++)
            for(int j=i;j<N;j+=i)
                if(have[j]) s[i]++;
        LL ans=(LL)n*(n-1)*(n-2)/6;
        ans-=gao();
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-12-25 11:37:56

hdu5072 Coprime 2014鞍山现场赛C题 计数+容斥的相关文章

2014鞍山现场赛C题HDU5072(素筛+容斥原理)

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

hdu5074 Hatsune Miku 2014鞍山现场赛E题 水dp

http://acm.hdu.edu.cn/showproblem.php?pid=5074 Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 325    Accepted Submission(s): 243 Problem Description Hatsune Miku is a popular vi

hdu 5078 2014鞍山现场赛 水题

http://acm.hdu.edu.cn/showproblem.php?pid=5078 现场最水的一道题 连排序都不用,因为说了ti<ti+1 //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include &l

hdu5073 2014鞍山现场赛D题

http://acm.hdu.edu.cn/showproblem.php?pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1421    Accepted Submission(s): 324 Special Judge Problem Description Good news for us: t

2014鞍山现场赛H题HDU5077(DFS减枝+打表)

NAND Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 65    Accepted Submission(s): 14 Problem Description Xiaoqiang entered the "shortest code" challenge organized by some self-claimed a

hdu 5078(2014鞍山现场赛 I题)

数据 表示每次到达某个位置的坐标和时间 计算出每对相邻点之间转移的速度(两点间距离距离/相隔时间) 输出最大值 Sample Input252 1 9//t x y3 7 25 9 06 6 37 6 01011 35 6723 2 2929 58 2230 67 6936 56 9362 42 1167 73 2968 19 2172 37 8482 24 98 Sample Output9.219544457354.5893762558 1 # include <iostream> 2 #

HDU 5073 Galaxy (2014鞍山现场赛D题)

题目链接:HDU 5073 Galaxy 题意:在一维的坐标系里,给出N个点坐标,转动K个点,使转动后这个星系的的惯性最小(根据题意惯性最小也就是 求所有星星到星系中心的距离最小,这个可以理解成方差最小).求最小的惯性. 思路: 先对序列排序,再求出算N-K个点惯性的递推式. 以三个为例: 预处理是 平均数和各项的平方和, 注意:n==k的特判 AC代码: #include <stdio.h> #include <algorithm> using namespace std; do

2014西安现场赛F题 UVALA 7040

地址 题意:求在m种颜色中挑选k种颜色,给n个花朵涂色有几种方法. 分析:画图可以发现,基本的公式就是k ×(k-1)^(n-1).但这仅保证了相邻颜色不同,总颜色数不超过k种,并没有保证恰好出现k种颜色:接着就是一个容斥问题,上述计算方法中包含了只含有2.3.….(k-1)种颜色的情况,需要通过容斥原理去除.假设出现p (2 <= p <= k-1)种颜色,从k种颜色中选取p种进行涂色,方案数为C(k,p) × p × (p-1)^(n-1) :总的方案数就是C(m,k) × ( k × (

HDU - 6513 Reverse It (SYSU校赛C题)(组合数学+容斥)

题目链接 题意:给定一个n*m的矩阵,可以选择至多两个子矩阵将其反转,求能形成多少种不同的矩阵. 任选一个矩阵有$C_{n+1}^{2}C_{m+1}^{2}$种方法,任选两个不同的矩阵有$C_{C_{n+1}^{2}C_{m+1}^{2}}^{2}$种方法,但其中有重复的,需要去重. 重复的情况一共有以下四种: 第一种,两矩阵拼合成一个矩阵,这样的图形有$C_{n+1}^{2}C_{m+1}^{2}$个,重复度(出现的次数)为(n+m-2) 第二种,形成的两个矩阵在同一行或同一列,有$C_{n