hdu 5072 coprime不完整题解
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 ≤ 10
5), denoting the number of people. The next line contains n distinct integers a
1, a
2, . . . , a
n(1 ≤ a
i ≤ 10
5) separated by a single space, where a
i stands for the id number of the i-th person.
1
5
1 3 9 10 2
4
一个数a与另一个数b互质,a同时和另一个数c不互质的情况的个数这个问题还稍显复杂,我们想想两个数之间不就只有互质和不互质的关系吗(也就是最大公约数为1和最大公约数不为1)?好了,接下来是网上搜到的比较易懂的代码(有些大神的代码真是看得眼晕,凡是主函数低于5行的都转得我眼晕)。
#include <cstdio>
#include <cstring>
#include<iostream>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 100005;
int a[maxn], cnt[maxn];
vector <int> tmp;
int main() {
int T, n, x;
scanf("%d", &T);
while(T--) {
long long ans = 0;
scanf("%d", &n);
memset(a, 0, sizeof(a));
memset(cnt, 0, sizeof(cnt));
//a[x]表示x的个数有多少个
for(int i = 0; i < n; i++) {
scanf("%d", &x);
a[x]++;
}
//cnt[i]表示数组中约数为i的个数
for(int i = 1; i < maxn; i++) {
for(int j = i; j < maxn; j += i) {
cnt[i] += a[j];
}
}
//tmp用来储存一个数字的所有质因数(好专业的样子)
for(int i = 1; i < maxn; i++) {
if(a[i]) {
tmp.clear();
int t = i;
int u = (int)sqrt(i);
for(int j = 2; j <= u; j++) {
if(t % j == 0) {
tmp.push_back(j);
while(t % j == 0) t /= j;
}
}
if(t > 1) tmp.push_back(t);
if(tmp.size() == 0) continue;
//接下来这个循环我就看不懂了
int nop = 0;
int v = tmp.size();
int nn = 1 << v;
for(int j = 1; j < nn; j++) {
int flag = 0;
int mul = 1;
for(int k = 0; k < v && j >> k; k++) {
if((j >> k) & 1) {
flag++;
mul *= tmp[k];
}
}
if(flag & 1) nop += cnt[mul]; else nop -= cnt[mul];
}
//nop大概表示不互质的个数,因为一个数本身也会和自己不互质,所以要减1
//ans很明显就是一个数a与另一个数b互质,a同时和另一个数c不互质的情况的个数了
ans += (long long)max(0, nop - 1) * (n - nop);
}
}
/*有些人会奇怪ans要除以2,其实ans实际上里面包括位置的不同的情况,就正如有三个位置,
a的位置已经固定在第一个不能改变,b和c因为两个不同的位置要算两种情况,但是我们并不需要
计较位置不同的情况,所以要除以2
*/
printf("%I64d\n", (long long)n * (n - 1) * (n - 2) / 6 - ans / 2);
}
return 0;
}
/*
1
5
1 3 9 10 2
ans:4
*/
hdu 5072 coprime不完整题解的更多相关文章
- HDU 5072 Coprime (单色三角形+容斥原理)
题目链接:Coprime pid=5072"> 题面: Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdu 5072 Coprime
http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出 n 个互不相同的数,求满足以下条件的三元无序组的个数:要么两两互质要么两两不互质. 思路:根据 ...
- hdu 5072 Coprime(同色三角形+容斥)
pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜 ...
- hdu 5072 Coprime 容斥原理
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...
- hdu 5072 Coprime (容斥)
Problem Description There are n people standing in a line. Each of them has a unique id number. Now ...
- 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(容斥+同色三角形)
原题链接 题意选出三个数,要求两两互质或是两两不互质.求有多少组这样的三个数. 分析 同色三角形n个点 每两个点连一条边(可以为红色或者黑色),求形成的三条边颜色相同的三角形的个数反面考虑这个问题,只 ...
- HDU 5072 Coprime 同色三角形问题
好吧,我承认就算当时再给我五个小时我也做不出来. 首先解释同色三角形问题: 给出n(n >= 3)个点,这些点中的一些被涂上了红色,剩下的被涂上了黑色.然后将这些点两两相连.于是每三个点都会组成 ...
- 题解报告:hdu 4135 Co-prime(容斥定理入门)
Problem Description Given a number N, you are asked to count the number of integers between A and B ...
- HDU 4135 Co-prime(容斥:二进制解法)题解
题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...
随机推荐
- 记 Win8.1 某应用渲染抛出 OutOfMemoryException 异常及修复方法
本文记录某个应用在某台 Windows 8.1 x86 系统上,运行时抛出 OutOfMemoryException 异常,启动失败.应用程序能启动,但是在第一次碰到渲染时,就发现渲染初始化失败,从而 ...
- Ubuntu VNC 远程桌面及常见问题
一.Ubuntu 远程桌面开启 在ubuntu 设置中打开远程桌面 **注意:如果没有共享桌面选项也不要谎,只需要安装 vino 即可 sudo apt update sudo apt install ...
- 第8讲 browse命令的使用技巧
第8讲 browse命令的使用技巧 1.浏览所有parts,使用技巧 选中工程文件*.dsn/Edit/Browse/Parts.列出工程中用到的所有元件,方便在画完原理图后,查看哪些元件没有编号或数 ...
- html+jq实现全国省的单选,弹框输入input
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- 文件上传--php user.ini详解
文件上传 参考文档:https://www.php.net/manual/zh/configuration.file.per-user.php 如果你的 PHP 以模块化运行在 Apache 里,则用 ...
- Swift实现判断目录下是否存在指定文件功能
本文主要讲解以下这段名为 isDataJsonFilePathExists 的私有函数的 Swift 代码实现细节,该函数的作用是检查指定文件或文件夹是否存在,其返回值类型为 Bool 类型,如果存在 ...
- WEB服务与NGINX(7)-实现自定义错误页面
1. 自定义错误页面 error_page code ... [=[response]] uri; 定义错误页,以指定的响应状态码进行响应,此指令由ngx_http_index_module模块提供 ...
- 【项目学习】Anchor:一种提供稳定币存款低波动收益率的去中心化的储蓄协议
简介 基于稳定币的获利产品. 贷方人放出稳定币以供借款.借方通过抵押资产(base asset)的方式,以低于协议定义的借贷比率借入稳定币.Anchor 协议使用抵押资产进行质押以获得奖励,然后将质押 ...
- ༺$Musique$༻
往期链接在文末 最近好喜欢听一些有年代感的歌啊. ~~头图~~ <$ On\ \And \ On $> Hold me close til I get up Time is barely ...
- 从油猴脚本管理器的角度审视Chrome扩展
从油猴脚本管理器的角度审视Chrome扩展 在之前一段时间,我需要借助Chrome扩展来完成一个需求,当时还在使用油猴脚本与浏览器扩展之间调研了一波,而此时恰好我又有一些做的还可以的油猴脚本 TKSc ...