hdu 5072 Coprime 容斥原理
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1509 Accepted Submission(s): 592
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.
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.
题目原形是同色三角形, 引用:就是求同色三角形的个数。总的三角形的个数是C(n,3),只需减去不同色的三角形即可。对于每个点(数),与它互质的连红边,不互质的连蓝边,那么对于该点不同色三角形个数为蓝边数*红边数/2,因为同一个三角形被计算了两次。那么同色三角形个数为C(n,3) - ∑蓝边数*红边数/2。
问题是:如何求 原来序列里面的n个数跟某个数k不互质的个数(互质的就是n-k了)?
可以将原来的n个数,每一个都把他们的不同的质因数都求出来,然后枚举它们能够组合的数(1 << cnt),用一个数组num记录,每枚举到一个数,那么数组对应的就+1
对于数k,也把它的不同质因数求出来,同样枚举它能够组合的所有数t,然后奇加偶减num
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N = ; int p[N][], vis[N], a[N], num[N];
int n;
void Prime()
{
memset(vis, , sizeof vis);
for(int i = ; i < N; ++i) p[i][] = ;
for(int i = ; i < N; ++i) if(!vis[i]) {
p[i][ ++p[i][] ] = i;
for(int j = i + i; j < N; j += i) {
vis[j] = ;
p[j][ ++p[j][] ] = i;
}
}
p[][ ++p[][] ] = ; //考虑0的情况
} void init()
{
memset(num, , sizeof num);
for(int k = ; k < n; ++k)
{
int now = a[k];
int cnt = p[ now ][];
for(int i = ; i < ( << cnt); ++i)
{
int t = ;
for(int j = ; j < cnt; ++j) if(( << j) & i) {
t *= p[ now ][j + ];
}
num[t]++;
}
}
} void solve()
{
ll ans = , res, sum = ;
ans = (ll)n * (n - ) * (n - ) / ; //类型转换一下,避免爆掉
int tot = ;
for(int k = ; k < n; ++k)
{
int now = a[k];
int cnt = p[now][];
res = ;
for(int i = ; i < ( << cnt); ++i)
{
int t = , g = ;
for(int j = ; j < cnt; ++j) if(( << j) & i) {
t *= p[ now ][j + ];
g++;
}
if(g & ) res += num[t];
else res -= num[t];
} if(res == ) continue;
sum += (res - ) * (n - res);
}
printf("%lld\n", ans - sum / ); }
int main()
{
// freopen("in", "r", stdin);
int _;
scanf("%d", &_);
Prime();
while(_ --)
{
scanf("%d", &n);
for(int i = ; i < n; ++i) scanf("%d", &a[i]);
init();
solve();
}
}
hdu 5072 Coprime 容斥原理的更多相关文章
- HDU 5072 Coprime (单色三角形+容斥原理)
题目链接:Coprime pid=5072"> 题面: Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- ACM学习历程—HDU 5072 Coprime(容斥原理)
Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...
- hdu 5072 Coprime(同色三角形+容斥)
pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜 ...
- HDU 4135 Co-prime(容斥原理)
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...
- hdu 5072 Coprime
http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出 n 个互不相同的数,求满足以下条件的三元无序组的个数:要么两两互质要么两两不互质. 思路:根据 ...
- hdu 5072 Coprime (容斥)
Problem Description There are n people standing in a line. Each of them has a unique id number. Now ...
- Hdu 5072 Coprime(容斥+同色三角形)
原题链接 题意选出三个数,要求两两互质或是两两不互质.求有多少组这样的三个数. 分析 同色三角形n个点 每两个点连一条边(可以为红色或者黑色),求形成的三条边颜色相同的三角形的个数反面考虑这个问题,只 ...
- hdu 5072 计数+容斥原理
/* 题意: 给出n个数(n<100000), 每个数都不大于100000,数字不会有重复.现在随意抽出3个,问三个彼此互质 或者 三个彼此不互质的数目有多少. 思路: 这道题反着想,就是三个数 ...
- HDU 5072 Coprime 同色三角形问题
好吧,我承认就算当时再给我五个小时我也做不出来. 首先解释同色三角形问题: 给出n(n >= 3)个点,这些点中的一些被涂上了红色,剩下的被涂上了黑色.然后将这些点两两相连.于是每三个点都会组成 ...
随机推荐
- curl命令 抓取网络数据相应头
curl --verbose --data "Password=123&Username=158101068&url=http://m.vancl.com/" & ...
- Quatre 2D的绘图功能的三个步骤(上下文,绘图,渲染)
一.qurza2d是怎么将绘图信息和绘图的属性绘制到图形上下文中去的? 说明: 新建一个项目,自定义一个view类和storyboard关联后,重写该类中的drowrect方法. 画线的三个步骤: ( ...
- 详解web.xml中元素的加载顺序
一.背景 最近在项目中遇到了启动时出现加载service注解注入失败的问题,后来经过不懈努力发现了是因为web.xml配置文件中的元素加载顺序导致的,那么就抽空研究了以下tomcat在启动时web.x ...
- 在web.xml中classpath和classpath*的区别
classpath 和 classpath* 区别: classpath:只会到你指定的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中(class路径) ...
- FragmentHelper
package com.icitymobile.anda.util; import java.lang.ref.SoftReference; import java.util.ArrayList; i ...
- 假定CSomething是一个类,执行下面这些语句之后,内存里创建了____个CSomething对象。
CSomething a(); CSomething b(2); CSomething c[3]; CSomething &ra = b; CSomething d=b; CSomething ...
- hdu1071(抛物线弓形面积阿基米德算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1071 题意:给出抛物线的顶点和它与一直线的两交点,求他们围成的面积: 思路: 可以直接求出他们的方程式 ...
- Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息 import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...
- php 简易购物习题
1.货物界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- Q3 2016 State of the Internet – Security Report
https://content.akamai.com/PG7476-Q3-2016-SOTI-Security-Report.html?utm_source=GoogleSearch&gcli ...