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. Docker 安装命令

    curl -sSL https://get.daocloud.io/docker | sh

  2. linux c 得到文件大小

    #include <sys/stat.h> unsigned long get_file_size(const char *path) { unsigned long filesize = ...

  3. 基于visual Studio2013解决算法导论之030二叉查找树

     题目 二叉查找树 解决代码及点评 // 12二叉查找树.cpp : 定义控制台应用程序的入口点. // // 3 - 10二叉搜索树查找.cpp : 定义控制台应用程序的入口点. // #in ...

  4. Linux看门狗脚本 1.4

    近期项目的看门狗经历了三个版本号. 第一个版本号: 用ps -ef,假设程序挂了就启动 第二个版本号: 程序因为执行时会出现不再监听7901port,所以不能简单推断机器是不是挂了,而是推断此port ...

  5. Linux下arp用法

    [功能] 管理系统的arp缓存. [描述] 用来管理系统的arp缓存,常用的命令包括: arp: 显示所有的表项. arp  -d  address: 删除一个arp表项. arp  -s addre ...

  6. InheritableThreadLocal

    InheritableThreadLocal继承自ThreadLocal,但比ThreadLocal多一个特性: 子线程可以继承父亲线程上下文中的信息 但是,有两个点需要注意的: 只有子线程创建之前的 ...

  7. 第1章 Lua基础

    1.1 全局变量 全局变量不需要声明,给一个变量赋值后即创建了这个全局变量,访问一个没有初始化的全局变量也不会出错,只不过得到的结果是:nil. 如果你想删除一个全局变量,只需要将变量负值为 nil ...

  8. Mac OSX的开机启动配置

    Login Items Mac OSX的当前用户成功登录后启动的程序,该类别的启动项配置文件存放在~/Library/Preferences/com.apple.loginitems.plist,所以 ...

  9. Mac编程的官方文档(类似MSDN)

    https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammin ...

  10. two sets of Qt binaries into the same process的解决办法

    突然出现了这样问题,吓死我,然后只是把原来编译好的app里面所有的东西删除再编译就好了. 如果删除后不行,可以试试后面的截图所说,反正我是没有试过的 Starting /Qtwork/build-te ...