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
Copy
3 1 2 3
output
Copy
5
input
Copy
4 1 1 1 1
output
Copy
15
input
Copy
7 1 3 5 15 3 105 35
output
Copy
100
Note
In the first example coprime subsequences are:
- 1
- 1, 2
- 1, 3
- 1, 2, 3
- 2, 3
In the second example all subsequences are coprime.
题意:给一个序列,问有多少个子集满足子集内所有数的gcd为1
题解:考虑莫比乌斯反演,设F(x)为gcd = x的倍数的子集数,f(x)为gcd = x的子集数
则
易知,cnt为序列中x的倍数的个数
这道题令x = 1求f(1)即可
#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=a-1;i>=(b);--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'
#define add(x,y) x=((x)+(y)>=mod)?(x)+(y)-mod:(x)+(y)
#define sub(x,y) x=((x)-(y)<0)?(x)-(y)+mod:(x)-(y)
#define clr(a,b) memset(a,b,sizeof(a))
#define eps 1e-10
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 1e9 + 7;
const int MX = 1e5 + 5;
ll power(ll a, int b) {
ll ret = 1;
while(b) {if(b & 1)ret = ret * a % mod; a = a * a % mod; b >>= 1;}
return ret;
}
int a[MX], cnt[MX];
int p[MX], pnum;
int prime[MX], mob[MX];
void init() {
memset (prime, true, sizeof (prime) );
mob[1] = 1;
for (int i = 2; i < MX; i++) {
if (prime[i]) {
p[pnum ++] = i;
mob[i] = -1;
}
for (int j = 0; j < pnum && (ll) i * p[j] < MX; j++) {
prime[i * p[j]] = false;
if (i % p[j] == 0) {
mob[i * p[j]] = 0;
break;
}
mob[i * p[j]] = -mob[i];
}
}
}
int main() {
#ifdef local
freopen("in.txt", "r", stdin);
#endif // local
init();
int n; cin >> n;
rep(i, 0, n) scanf("%d", &a[i]);
rep(i, 0, n) cnt[a[i]]++;
int ans = 0;
for(int x = 1; x < MX; x++) {
int tmp = 0;
for(int k = 1; k * x < MX; k++) tmp += cnt[k * x];
ans += mob[x] * (power(2ll, tmp) - 1);
while(ans < 0) ans += mod;
while(ans >= mod) ans -= mod;
}
cout << ans << endl;
return 0;
}