CO-PRIME

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描写叙述

This problem is so easy! Can you solve it?

You are given a sequence which contains n integers a1,a2……an, your task is to find how many pair(ai, aj)(i < j) that ai and aj is co-prime.

输入
There are multiple test cases.

Each test case conatains two line,the first line contains a single integer n,the second line contains n integers.

All the integer is not greater than 10^5.
输出
For each test case, you should output one line that contains the answer.
例子输入
3
1 2 3
例子输出
3

题意:给出n个正整数,求这n个数中有多少对互素的数。

分析:

fr=aladdin">莫比乌斯反演。

此题中,设F(d)表示n个数中gcd为d的倍数的数有多少对,f(d)表示n个数中gcd恰好为d的数有多少对,

则F(d)=∑f(n) (n % d == 0)

f(d)=∑mu[n / d] * F(n) (n %d == 0)

上面两个式子是莫比乌斯反演中的式子。

所以要求互素的数有多少对。就是求f(1)。

而依据上面的式子能够得出f(1)=∑mu[n] * F(n)。

所以把mu[]求出来,枚举n即可了。当中mu[i]为i的莫比乌斯函数。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAXN = 1e5 + 10;
typedef long long LL;
int cnt[MAXN], pri[MAXN], num[MAXN], pri_num, mu[MAXN], vis[MAXN], a[MAXN]; void mobius(int n) //筛法求莫比乌斯函数
{
pri_num = 0;
memset(vis, 0, sizeof(vis));
vis[1] = mu[1] = 1;
for(int i = 2; i <= n; i++) {
if(!vis[i]) {
pri[pri_num++] = i;
mu[i] = -1;
}
for(int j = 0; j < pri_num; j++) {
if(i * pri[j] > n) break;
vis[i*pri[j]] = 1;
if(i % pri[j] == 0) {
mu[i*pri[j]] = 0;
break;
}
mu[i*pri[j]] = -mu[i];
}
}
} LL get(int x)
{
return (LL)x * (x-1) / 2;
} int main()
{
mobius(100005);
int n;
while(~scanf("%d",&n)) {
int mmax = 0;
for(int i = 1; i <= n; i++) {
scanf("%d",&a[i]);
mmax = max(mmax, a[i]);
}
memset(cnt, 0, sizeof(cnt));
memset(num, 0, sizeof(num));
for(int i = 1; i <= n; i++) num[a[i]]++;
for(int i = 1; i <= mmax; i++)
for(int j = i; j <= mmax; j += i)
cnt[i] += num[j];
LL ans = 0;
for(int i = 1; i <= mmax; i++)
ans += get(cnt[i]) * mu[i];
printf("%lld\n", ans);
}
return 0;
}

NYOJ 1066 CO-PRIME(数论)的更多相关文章

  1. UVA 11610 Reverse Prime (数论+树状数组+二分,难题)

    参考链接http://blog.csdn.net/acm_cxlove/article/details/8264290http://blog.csdn.net/w00w12l/article/deta ...

  2. UVA 1415 - Gauss Prime(数论,高斯素数拓展)

    UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或 ...

  3. 省常中模拟 Test4

    prime 数论 题意:分别求 1*n.2*n.3*n.... n*n 关于模 p 的逆元.p 是质数,n < p. 初步解法:暴力枚举.因为 a 关于模 p 的逆元 b 满足 ab mod p ...

  4. 【HDU】2866:Special Prime【数论】

    Special Prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. POJ 1365 Prime Land(数论)

    题目链接: 传送门 Prime Land Time Limit: 1000MS     Memory Limit: 10000K Description Everybody in the Prime ...

  6. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  7. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  8. codeforces 680C C. Bear and Prime 100(数论)

    题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...

  9. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

    Prime Ring Problem Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

随机推荐

  1. mvc中的几个数据传递

    1.ViewData对象 ViewBagData是一种字典集合数据同时属于视图基类和控制器基类的属性. 实例: //控制器 public class HomeController:Controller ...

  2. 集合判断null

    Java 引用和指针差不多,null 引用 相当于 C++的空指针. isEmpty() 用于判断List内容是否为空,即表里一个元素也没有, 但是必须在 List<MallNews> g ...

  3. 快的打车 技术部 在 杭州 招聘 #年前面试 年后入职#架构师 - 内推网(neitui.me)

    快的打车 技术部 在 杭州 招聘 #年前面试 年后入职#架构师 - 内推网(neitui.me) 陈丹 (cd**@kuaidadi.com) 01-18 发布了内推 #年前面试 年后入职#架构师 • ...

  4. C++常变量

    在定义变量时,如果加上关键字const,则变量的值在程序运行期间不能改变,这种变量称为常变量(constant variable).例如:    const int a=3;  //用const来声明 ...

  5. BNU Questions and answers

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=2490 这个题是先输入一个整数n,说明有几个数据,然后输入n个整数,然后用三个#分开,后面输入整数k, ...

  6. meanShift算法介绍

    meanShift,均值漂移,在聚类.图像平滑.切割.跟踪等方面有着广泛的应用.meanShift这个概念最早是由Fukunage在1975年提出的,其最初的含义正如其名:偏移的均值向量:但随着理论的 ...

  7. Qt读取ANSI格式文件——利用QTextCodec将其他编码格式的QByteArray转换为Unicode格式,或者从文件中读出后直接做转换

    t使用Unicode来表示字符串.但是通常需要访问一些非Unicode格式的字符串,例如打开一个GBK编码的中文文本文件,甚至一些非Unicode编码的日文,俄文等. Qt提供了QTextCodec类 ...

  8. hdu2492 Ping pong

    hdu2492 Ping pong 题意:一群乒乓爱好者居住在一条直线上,如果两个人想打比赛需要一个裁判,裁判的 位置 必须在两者之间 ,裁判的能力也必须不大于 参赛者最大的,不小于参赛者最小的 白皮 ...

  9. c语言, objective code(new 2)

    参考: 1. C中的继承和多态 http://www.cnblogs.com/skynet/archive/2010/09/23/1833217.html

  10. 删除workspace下的vss的scc文件

    public class DeleteAA { public static void main(String[] args) { DeleteAA aa=new DeleteAA(); aa.dele ...