hdu5072 Coprime (2014鞍山区域赛C题)(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5072
题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质。
题解:
http://dtyfc.com/acm/980 我看的这个学会的……
可以先求不满足要求的三元组数量,也就是abc,a和b互质,b和c不互质。
这样就要找这n个数中,和某个数不互质的数的个数。
可以质因数分解+容斥原理,求出和某个数不互质的数的个数(也就是和这个数有相同因数的数的个数)。
还要先预处理以某个数x为因子的数的个数,cnt[x]。
具体看代码。
其实我也不是很熟,醉了。
不信你看这个博客都没有数论分类
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
using namespace std;
#define mz(array) memset(array, 0, sizeof(array))
#define mf1(array) memset(array, -1, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#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 WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("huzhi.txt","w",stdout)
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppf pop_front
#define ppb pop_back
typedef long long ll;
typedef unsigned long long ull; const double pi=acos(-1.0);
const double eps=1e-; const int maxn=; int a[maxn];
int cnt[maxn];
int n; int b[maxn],bn; int att,me;
inline void attack(const int &now,const int &sum,const int &flag) {
if(now==bn) {
if(sum==)return;
// printf("sum=%d cnt[]=%d flag=%d\n",sum ,(cnt[sum]), flag);
att+=flag*(cnt[sum]);
return;
}
attack(now+,sum,flag);
attack(now+,sum*b[now],-flag);
} ll farm() {
int i,j,k;
mz(cnt);
FOR(i,,)
for(j=i; j<=; j+=i)
cnt[i]+=a[j];
ll sm=;
FOR(i,,) {
if(a[i]) {
int s=(int)sqrt((double)i);
int t=i;
bn=;
for(j=; j<=s && t>; j++){
if(t%j==){
b[bn++]=j;
while(t%j==)t/=j;
}
}
if(t>)b[bn++]=t;
att=;
me=i;
attack(,,-);
// REP(j,bn)printf("%d,",b[j]);
// printf("(i=%d , att= %d re+=%d)\n",i,att,max(0 , att-1) * (n-att));
sm+=(ll)max( , att-) * (n-att);
}
}
return (ll)n*(n-)*(n-)/ - sm/;
} int main() {
int T,i,x;
RD(T);
while(T--) {
RD(n);
mz(a);
REP(i,n) {
RD(x);
a[x]++;
}
printf("%I64d\n",farm());
}
return ;
}
hdu5072 Coprime (2014鞍山区域赛C题)(数论)的更多相关文章
- hdu5080:几何+polya计数(鞍山区域赛K题)
/* 鞍山区域赛的K题..当时比赛都没来得及看(反正看了也不会) 学了polya定理之后就赶紧跑来补这个题.. 由于几何比较烂写了又丑又长的代码,还debug了很久.. 比较感动的是竟然1Y了.. * ...
- 2014鞍山现场赛C题HDU5072(素筛+容斥原理)
Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- hdu 5071(2014鞍山现场赛B题,大模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...
- hdu 5003 模拟水题 (2014鞍山网赛G题)
你的一系列得分 先降序排列 再按0.95^(i-1)*ai 这个公式计算你的每一个得分 最后求和 Sample Input12530 478Sample Output984.1000000000 # ...
- hdu 5078(2014鞍山现场赛 I题)
数据 表示每次到达某个位置的坐标和时间 计算出每对相邻点之间转移的速度(两点间距离距离/相隔时间) 输出最大值 Sample Input252 1 9//t x y3 7 25 9 06 6 37 6 ...
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...
- zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)
3799567 2014-10-14 10:13:59 Acce ...
- hdu 5078 2014鞍山现场赛 水题
http://acm.hdu.edu.cn/showproblem.php?pid=5078 现场最水的一道题 连排序都不用,由于说了ti<ti+1 //#pragma comment(link ...
随机推荐
- Matlab以特殊分隔符写入txt(dlmwrite)
clc clear all load('E:\RSWeb\mahoyt数据集\movielens\u.data'); dlmwrite('E:\RSWeb\mahoyt数据集\movielens\ne ...
- BZOJ1012 [JSOI2008]最大数maxnumber
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- JeeSite环境搭建及运行和打包(master20161117)
涉及的软件: 1.phpStudy(主要用MySql) 2.maven3(用于依赖包,下面我将上传已经下载好所有依赖包的版本,保证运行正常) 具体操作: 0.前言 由于GitHub上的Release版 ...
- install docker on xubuntu
ref: https://docs.docker.com/engine/installation/linux/ubuntulinux/#/install-the-latest-version ps: ...
- springMVC 基于注解的controller
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- python模块xlrd安装-处理excel文件必须
我安装了很久,网上查了很多资料,但都不太适合,综合 了一下,再写一写,希望有用... 官网下载xlrd:官网xlrd下载地址, 真的很难下,我用讯雷,有时候断断续续 下面是我的百度网盘地址,分享出来, ...
- JSP内置对象-request
JSP内置对象即无需声明就可以直接使用的对象实例,在实际的开发过程中,比较常用的JSP对象有request,response,session,out和application等,笔者在本文章中将简单介绍 ...
- nginx跨域设置
nginx跨域问题例子:访问http://10.0.0.10/ 需要能实现跨域 操作:http://10.0.0.10/项目是部署在tomcat里面,tomcat跨域暂时还不会,按照网上的方法操作也没 ...
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
http://blog.csdn.net/cjjky/article/details/6441104 在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSeri ...
- java中"与"和"或"
在java中"与"和"或"都具有短路功能. 关于"或"运算如 if(mc == null || mc.getReceive()==0){ / ...