【CF 585E】 E. Present for Vitalik the Philatelist

E. Present for Vitalik the Philatelist
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vitalik the philatelist has a birthday today!

As he is a regular customer in a stamp store called 'Robin Bobin', the store management decided to make him a gift.

Vitalik wants to buy one stamp and the store will give him a non-empty set of the remaining stamps, such that the greatest common divisor (GCD) of the price of the stamps they give to him is more than one. If the GCD of prices of the purchased stamp and prices of present stamps set will be equal to 1, then Vitalik will leave the store completely happy.

The store management asks you to count the number of different situations in which Vitalik will leave the store completely happy. Since the required number of situations can be very large, you need to find the remainder of this number modulo 109 + 7. The situations are different if the stamps purchased by Vitalik are different, or if one of the present sets contains a stamp that the other present does not contain.

Input

The first line of the input contains integer n (2 ≤ n ≤ 5·105) — the number of distinct stamps, available for sale in the 'Robin Bobin' store.

The second line contains a sequence of integers a1, a2, ..., an (2 ≤ ai ≤ 107), where ai is the price of the i-th stamp.

Output

Print a single integer — the remainder of the sought number of situations modulo 109 + 7.

Examples
input
3
2 3 2
output
5
input
2
9 6
output
0
Note

In the first sample the following situations are possible:

  • Vitalik buys the 1-st stamp, the store gives him the 2-nd stamp as a present;
  • Vitalik buys the 3-rd stamp, the store gives him the 2-nd stamp as a present;
  • Vitalik buys the 2-nd stamp, the store gives him the 1-st stamp as a present;
  • Vitalik buys the 2-nd stamp, the store gives him the 3-rd stamp as a present;
  • Vitalik buys the 2-nd stamp, the store gives him the 1-st and 3-rd stamps as a present.

 

【题意】

  给出一列数,对于每一个数,求选出一个不包含当前数的非空子集满足子集与当前数gcd为1,并且子集中的所有数的gcd不为1的方案数,统计总和。

 

【分析】

  就是说s是一个子集,x是一个数,然后求$\sum gcd(s,x)==1且gcd(s)!=1$

    设d=gcd(s),枚举这个d,那就是(2^[d的倍数的个数]-1)*(不是含d因子的数)

  但是这样会重复,比如2,3,6在2,3,6时都算了一遍。所以容斥。【你会发现容斥系数是莫比乌斯函数的相反数

  【然后mu[i]=0就没有必要算了。时间极限是mlogm,但是mu=0没算,应该会快一点把【反正过了

 

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 #define Maxn 500010
 8 #define Maxm 10001000
 9 #define Mod 1000000007
10 
11 int mu[Maxm],pri[Maxm],pl,mx;
12 int cnt[Maxm],pw[Maxn],a[Maxn];
13 bool vis[Maxm];
14 void init()
15 {
16     memset(vis,0,sizeof(vis));
17     for(int i=2;i<=mx;i++)
18     {
19         if(!vis[i]) pri[++pl]=i,mu[i]=-1;
20         for(int j=1;j<=pl;j++)
21         {
22             if(pri[j]*i>mx) break;
23             vis[pri[j]*i]=1;
24             if(i%pri[j]==0) mu[i*pri[j]]=0;
25             else mu[i*pri[j]]=-mu[i];
26             if(i%pri[j]==0) break;
27         }
28     }
29 }
30 
31 int main()
32 {
33     int n;
34     scanf("%d",&n);mx=0;
35     memset(cnt,0,sizeof(cnt));
36     for(int i=1;i<=n;i++) {scanf("%d",&a[i]);mx=max(mx,a[i]);cnt[a[i]]++;}
37     init();
38     pw[0]=1;for(int i=1;i<=n;i++) pw[i]=(pw[i-1]*2)%Mod;
39     int ans=0;
40     for(int i=2;i<=mx;i++) if(mu[i]!=0)
41     {
42         int nw=0;
43         for(int j=i;j<=mx;j+=i) nw+=cnt[j];
44         ans=(ans+1LL*(pw[nw]-1)*(-mu[i])*(n-nw)%Mod)%Mod;
45     }
46     ans=(ans+Mod)%Mod;
47     printf("%d\n",ans);
48     return 0;
49 }
View Code

 

2017-04-20 19:16:41

posted @ 2017-04-20 19:16  konjak魔芋  阅读(329)  评论(0编辑  收藏  举报