题目链接:

F. Coprime Subsequences

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common divisor of all elements of this sequence is equal to 1.

Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109 + 7.

Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1].

Input

The first line contains one integer number n (1 ≤ n ≤ 100000).

The second line contains n integer numbers a1, a2... an (1 ≤ ai ≤ 100000).

Output

Print the number of coprime subsequences of a modulo 109 + 7.

Examples
input
  1. 3
    1 2 3
output
  1. 5
input
  1. 4
    1 1 1 1
output
  1. 15
input
  1. 7
    1 3 5 15 3 105 35
output
  1. 100
  2.  
  3. 题意:给一个序列,问gcd1的子序列有多少个;
  4.  
  5. 思路:容斥,可以求出gcd1的倍数的子序列个数,然后减去gcd2,3,5,等等一个素数倍数的子序列个数再加上两个素数积倍数的子序列个数以此类推,
    (注意容斥里面集合的交集里面不可能有4,9,12这种数,因为质因数分解后里面同一个质因数的个数>1,这就不可能在两个集合的交集里面);
  6.  
  7. AC代码:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int maxn=1e5+10;
  5. const int mod=1e9+7;
  6. int n,a[maxn],p[maxn];
  7. int vis[maxn],num[maxn];
  8. void init()
  9. {
  10. p[0]=1;
  11. for(int i=1;i<maxn;i++){p[i]=p[i-1]*2;if(p[i]>=mod)p[i]-=mod;}
  12. for(int i=2;i<maxn;i++)
  13. {
  14. if(!vis[i])
  15. {
  16. num[i]++;
  17. for(int j=2*i;j<maxn;j+=i)
  18. {
  19. vis[j]=1;
  20. if(num[j]==-1)continue;
  21. int tep=j,x=0;
  22. while(tep%i==0)x++,tep/=i;
  23. if(x>1)num[j]=-1;
  24. else num[j]++;
  25. }
  26. }
  27. }
  28. }
  29. int main()
  30. {
  31. init();
  32. scanf("%d",&n);
  33. int x;
  34. for(int i=1;i<=n;i++)
  35. {
  36. scanf("%d",&x);
  37. for(int j=1;j*j<=x;j++)
  38. {
  39. if(x%j)continue;
  40. a[j]++;
  41. if(j*j!=x)a[x/j]++;
  42. }
  43. }
  44. int ans=0;
  45. for(int i=1;i<maxn;i++)
  46. {
  47. if(num[i]==-1)continue;
  48. if(num[i]&1)ans=(ans-p[a[i]]+1);
  49. else ans=(ans+p[a[i]]-1);
  50. if(ans>=mod)ans-=mod;
  51. else if(ans<0)ans+=mod;
  52. }
  53. printf("%d\n",ans);
  54. return 0;
  55. }

  

  1.  

F. Coprime Subsequences的更多相关文章

  1. F. Coprime Subsequences 莫比乌斯反演

    http://codeforces.com/contest/803/problem/F 这题正面做了一发dp dp[j]表示产生gcd = j的时候的方案总数. 然后稳稳地超时. 考虑容斥. 总答案数 ...

  2. CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)

    Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...

  3. Codeforces 803F Coprime Subsequences (容斥)

    Link:http://codeforces.com/contest/803/problem/F 题意:给n个数字,求有多少个GCD为1的子序列. 题解:容斥!比赛时能写出来真是炒鸡开森啊! num[ ...

  4. 【codeforces 803F】Coprime Subsequences

    [题目链接]:http://codeforces.com/contest/803/problem/F [题意] 给你一个序列; 问你这个序列里面有多少个子列; 且这个子列里面的所有数字互质; [题解] ...

  5. Codeforces 803F - Coprime Subsequences(数论)

    原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的 ...

  6. CodeForces 803F Coprime Subsequences

    $dp$. 记$dp[i]$表示$gcd$为$i$的倍数的子序列的方案数.然后倒着推一遍减去倍数的方案数就可以得到想要的答案了. #include <iostream> #include ...

  7. Educational Codeforces Round 20

    Educational Codeforces Round 20  A. Maximal Binary Matrix 直接从上到下从左到右填,注意只剩一个要填的位置的情况 view code //#pr ...

  8. Codeforces Round 363 Div. 1 (A,B,C,D,E,F)

    Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...

  9. 山东省第四届ACM省赛

    排名:http://acm.sdut.edu.cn/sd2012/2013.htm 解题报告:http://www.tuicool.com/articles/FnEZJb A.Rescue The P ...

随机推荐

  1. Python(面向对象编程4——继承顺序、封装)

    继承顺序 ''' 一点需要注意 ''' class Father: def f1(self): print("test func followed ==>") self.te ...

  2. SQL联接 外联接 内联接 完全联接 交叉联接

    联接分为: 内联接                        [inner join] 外联接        (左外联接,右外联接)        [left join/left outer jo ...

  3. ruby项目文件上传功能实现

    这里我将从视图.控制器各个层面进行讲解. rails 提供了文件上传功能,可以直接进行下面的编码 <%= form_for :document, :html =>{:multipart = ...

  4. 微信小程序将带来web程序员的春天!

    微信之父张小龙在年初那次演讲中曾表示:“我自己是很多年的程序员,我觉得我们应该为开发的团体做一些事情.”几个月后,微信正式推出微信应用号(即微信小程序)在互联网中掀起又一波热潮. 过去,对于很多开发者 ...

  5. day1 python调用模块,密码加密

    import getpass #加密密码 username = input("username:") password = getpass.getpass("passwo ...

  6. IDEA MAVEN Project 显示问题

    今天正常打开idea,却发现maven窗口找不到了:试了这些方法 首先idea自带了maven控件,不像Eclipse还需要下载控件,如果你以前有maven在右边,出于某种原因,消失找不到 了,你可以 ...

  7. jQuery带小图标的Tab切换焦点图

    在线演示 本地下载

  8. Linux内核优化项

    net.ipv4.ip_forward = #该文件内容为0,表示禁止数据包转发,1表示允许 net.ipv4.conf.default.rp_filter = #是否忽略arp请求 net.ipv4 ...

  9. 解析CEPH: 存储引擎实现之一 filestore

    Ceph作为一个高可用和强一致性的软件定义存储实现,去使用它非常重要的就是了解其内部的IO路径和存储实现.这篇文章主要介绍在IO路径中最底层的ObjectStore的实现之一FileStore. Ob ...

  10. Samba的安装配置使用

    ////////////////////////////////Samba//////////////////////////////////////////// 小常识:在同一局域网中,两个系统的工 ...