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

求n个不同的数(<=1e5)中有多少组三元组(a, b, c)两两不互质或者两两互质。

逆向求解,把所有不符合的情况求出来用总的情况数减去即可;

先用容斥求出和a[i] 互质的个数num ,然后不符合条件的 就是 num*(n-1-num);

求法见http://blog.csdn.net/u012774187/article/details/40399567

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 1e5+5;
int n,p[maxn],cnt[maxn];// 公约数计数
vector<int> q;
int main() {
int _;RD(_);while(_--){
RD(n);
for(int i = 1;i <= n;++i)
RD(p[i]);
clr0(cnt);
for(int i = 1;i <= n;++i){
for(int j = 1;j * j <= p[i];++j)if(p[i]%j == 0){
cnt[j]++;
if(p[i]/j != j)
cnt[p[i]/j]++;
}
}
LL sub = 0;
for(int i = 1;i <= n;++i){
q.clear();
int m = p[i];
for(int j = 2;j * j <= m;++j)if(m%j == 0){
q.push_back(j);
while(m%j == 0)
m/=j;
}
if(m != 1)
q.push_back(m);
int len = q.size();
LL sum = 0;
for(int j = 1;j < (1<<len);++j){
int cnt_fac = 0,u = 1;
for(int k = 0;k < len;++k)if(j & (1<<k)){
cnt_fac++;
u *= q[k];
}
if(cnt_fac & 1) sum += cnt[u];
else sum -= cnt[u];
}
if(sum) sum--;
sub += sum*(n - sum - 1);
}
printf("%I64d\n",(LL)n*(n-1)*(n-2)/6 - sub/2);
}
return 0;
}

hdu 5072 两两(不)互质个数逆向+容斥的更多相关文章

  1. 51 nod 1439 互质对(Moblus容斥)

    1439 互质对 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 有n个数字,a[1],a[2],…,a[n].有一个集合,刚开 ...

  2. C互质个数

    C互质个数 Time Limit:1000MS  Memory Limit:65536K Total Submit:55 Accepted:27 Description 贝贝.妞妞和康康都长大了,如今 ...

  3. hdu 1573 X问题 两两可能不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  4. hdu 3579 Hello Kiki 不互质的中国剩余定理

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  5. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  6. HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. HDU 1796 How many integers can you find 【容斥】

    <题目链接> 题目大意: 给你m个数,其中可能含有0,问有多少小于n的正数能整除这个m个数中的某一个. 解题分析: 容斥水题,直接对这m个数(除0以外)及其组合的倍数在[1,n)中的个数即 ...

  8. HDU 1796 How many integers can you find(容斥)题解

    思路:二进制解决容斥问题,就和昨天做的差不多.但是这里题目给的因子不是质因子,所以我们求多个因子相乘时要算最小公倍数.题目所给的因数为非负数,故可能有0,如果因子为0就要删除. 代码: #includ ...

  9. hdu 1573 X问题 (非互质的中国剩余定理)

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. C++11并发编程实战 免费书籍

    C++11 博客http://www.cnblogs.com/haippy/p/3284540.html 网上推荐的C++多线程基本都是C++ Concurrency in Action 英文版的,中 ...

  2. c10k C10M

    高性能网络编程(二):上一个10年,著名的C10K并发连接问题     阅读(22369) | 评论(9)收藏10 淘帖1 赞4   JackJiang Lv.9    1 年前 | |只看大图 1. ...

  3. linux内核中hlist_head和hlist_node结构解析

    hlist_head和hlist_node用于散列表,分表表示列表头(数组中的一项)和列表头所在双向链表中的某项,两者结构如下: 1 2 3 struct hlist_head { struct hl ...

  4. 搭建http静态网页服务器出现“Forbidden You don't have permission to access / on this server”

    部分参考链接: 2.4+ httpd最简单example.conf, 存放目录:/etc/httpd/conf.d/example.conf Alias /newstart-zte/ "/n ...

  5. zz Alex's BLOG 串口连接

    using System; using System.Collections.Generic;using System.ComponentModel;using System.Data;using S ...

  6. Spring相关知识点

    1.注解@qualifier 只能注在属性上 作用:当一个接口有多个实现类时,用Autowired装配时,因为Autowired是按类型装配的(Resource按名称),所以多个实现类会出现冲突,这是 ...

  7. openssl 连接 https(nginx)

    参考源码路径  demos\ssl #include <stdio.h> #include <string.h> #include <stdlib.h> #incl ...

  8. Git使用基础篇(zz)

    Git使用基础篇 您的评价:          收藏该经验       Git是一个分布式的版本控制工具,本篇文章从介绍Git开始,重点在于介绍Git的基本命令和使用技巧,让你尝试使用Git的同时,体 ...

  9. UD系统主定制界面

  10. 【轻松前端之旅】元素,标记,属性,<html>标签

    HTML文档是由HTML元素定义的. HTML元素(element)指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 有些元素会使用简写记法,如<img src=' ...