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.
例子输入
  1. 3
  2. 1 2 3
例子输出
  1. 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的莫比乌斯函数。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. const int MAXN = 1e5 + 10;
  7. typedef long long LL;
  8. int cnt[MAXN], pri[MAXN], num[MAXN], pri_num, mu[MAXN], vis[MAXN], a[MAXN];
  9.  
  10. void mobius(int n) //筛法求莫比乌斯函数
  11. {
  12. pri_num = 0;
  13. memset(vis, 0, sizeof(vis));
  14. vis[1] = mu[1] = 1;
  15. for(int i = 2; i <= n; i++) {
  16. if(!vis[i]) {
  17. pri[pri_num++] = i;
  18. mu[i] = -1;
  19. }
  20. for(int j = 0; j < pri_num; j++) {
  21. if(i * pri[j] > n) break;
  22. vis[i*pri[j]] = 1;
  23. if(i % pri[j] == 0) {
  24. mu[i*pri[j]] = 0;
  25. break;
  26. }
  27. mu[i*pri[j]] = -mu[i];
  28. }
  29. }
  30. }
  31.  
  32. LL get(int x)
  33. {
  34. return (LL)x * (x-1) / 2;
  35. }
  36.  
  37. int main()
  38. {
  39. mobius(100005);
  40. int n;
  41. while(~scanf("%d",&n)) {
  42. int mmax = 0;
  43. for(int i = 1; i <= n; i++) {
  44. scanf("%d",&a[i]);
  45. mmax = max(mmax, a[i]);
  46. }
  47. memset(cnt, 0, sizeof(cnt));
  48. memset(num, 0, sizeof(num));
  49. for(int i = 1; i <= n; i++) num[a[i]]++;
  50. for(int i = 1; i <= mmax; i++)
  51. for(int j = i; j <= mmax; j += i)
  52. cnt[i] += num[j];
  53. LL ans = 0;
  54. for(int i = 1; i <= mmax; i++)
  55. ans += get(cnt[i]) * mu[i];
  56. printf("%lld\n", ans);
  57. }
  58. return 0;
  59. }

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. Best Component for Bitmap Image

    The best is to purchase ImageEn and use the latest version. Coz nothing compares to ImageEn.... But ...

  2. 引用 xp系统引导修复(转载)

    引用 3592wangxiaoxi 的 xp系统引导修复(转载) 原文来自百度知道a12424106关于“急需xp系统引导方面的知识!”的回复. XP系统的引导过程 如果想学习排除计算机系统故障,首先 ...

  3. Tri_integral Summer Training 8 总结

    比赛链接 题目 B C E F G I 这是孟加拉国的区域赛. 开场ss读懂了c发现是个水题,于是去敲,结果手贱wa了一炮,不过很快就修正了错误.B题过了不少,我去读,发现是个水题,意识让Moor敲. ...

  4. 使用adb签名并安装Android程序

    首先需要准备Android SDK包,我是在windows上操作的,在PATH中配置  YOUT_SDK_PATH\android-sdk-windows\platform-tools 和  YOUT ...

  5. 动态规划——最长公共子序列(LCS)

    /** * @brief longest common subsequence(LCS) * @author An * @data 2013.8.26 **/ #include <iostrea ...

  6. Python 第一篇:python简介和入门

    一.python简介 1.python下载地址:https://www.python.org/downloads/ Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆 ...

  7. Learning Lua Programming (4) Cocos2d-x中Lua编程(一)

    刚开始接触cocos2d-x 下的Lua编程,主要参看了李华明大神的博客中的介绍,http://blog.csdn.net/xiaominghimi/article/category/1155088  ...

  8. 【Bootstrap3.0建站笔记一】表单元素排版

    1.文字和输入框前后排列: 代码: <div class="row"> <div class="col-lg-12"> <div ...

  9. ubuntu: root用户

    ubuntu怎么设置root用户  http://blog.csdn.net/chenping314159/article/details/7561339 创建root帐号: 在安装系统时,root账 ...

  10. 操作3 mongodb和mysql 开启慢查询日志 ,以及mongodb从配置文件启动

    1. mongodb从配置文件启动 创建配置文件:/usr/local/mongodb/etc/mongodb.conf 配置文件的内容为: #Directory and relavent set d ...