NYOJ 1066 CO-PRIME(数论)
CO-PRIME
- 描写叙述
-
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
- 例子输出
-
- 3
题意:给出n个正整数,求这n个数中有多少对互素的数。
此题中,设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(数论)的更多相关文章
- UVA 11610 Reverse Prime (数论+树状数组+二分,难题)
参考链接http://blog.csdn.net/acm_cxlove/article/details/8264290http://blog.csdn.net/w00w12l/article/deta ...
- UVA 1415 - Gauss Prime(数论,高斯素数拓展)
UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或 ...
- 省常中模拟 Test4
prime 数论 题意:分别求 1*n.2*n.3*n.... n*n 关于模 p 的逆元.p 是质数,n < p. 初步解法:暴力枚举.因为 a 关于模 p 的逆元 b 满足 ab mod p ...
- 【HDU】2866:Special Prime【数论】
Special Prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- POJ 1365 Prime Land(数论)
题目链接: 传送门 Prime Land Time Limit: 1000MS Memory Limit: 10000K Description Everybody in the Prime ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- 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 ...
- Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
随机推荐
- Best Component for Bitmap Image
The best is to purchase ImageEn and use the latest version. Coz nothing compares to ImageEn.... But ...
- 引用 xp系统引导修复(转载)
引用 3592wangxiaoxi 的 xp系统引导修复(转载) 原文来自百度知道a12424106关于“急需xp系统引导方面的知识!”的回复. XP系统的引导过程 如果想学习排除计算机系统故障,首先 ...
- Tri_integral Summer Training 8 总结
比赛链接 题目 B C E F G I 这是孟加拉国的区域赛. 开场ss读懂了c发现是个水题,于是去敲,结果手贱wa了一炮,不过很快就修正了错误.B题过了不少,我去读,发现是个水题,意识让Moor敲. ...
- 使用adb签名并安装Android程序
首先需要准备Android SDK包,我是在windows上操作的,在PATH中配置 YOUT_SDK_PATH\android-sdk-windows\platform-tools 和 YOUT ...
- 动态规划——最长公共子序列(LCS)
/** * @brief longest common subsequence(LCS) * @author An * @data 2013.8.26 **/ #include <iostrea ...
- Python 第一篇:python简介和入门
一.python简介 1.python下载地址:https://www.python.org/downloads/ Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆 ...
- Learning Lua Programming (4) Cocos2d-x中Lua编程(一)
刚开始接触cocos2d-x 下的Lua编程,主要参看了李华明大神的博客中的介绍,http://blog.csdn.net/xiaominghimi/article/category/1155088 ...
- 【Bootstrap3.0建站笔记一】表单元素排版
1.文字和输入框前后排列: 代码: <div class="row"> <div class="col-lg-12"> <div ...
- ubuntu: root用户
ubuntu怎么设置root用户 http://blog.csdn.net/chenping314159/article/details/7561339 创建root帐号: 在安装系统时,root账 ...
- 操作3 mongodb和mysql 开启慢查询日志 ,以及mongodb从配置文件启动
1. mongodb从配置文件启动 创建配置文件:/usr/local/mongodb/etc/mongodb.conf 配置文件的内容为: #Directory and relavent set d ...