【IOI2020国家集训队作业 Part 1】CF585E Present for Vitalik the Philatelist

题目

题目描述
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 11 , 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 10^{9}+710
9
+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.

输入格式
The first line of the input contains integer nn ( 2<=n<=5·10^{5}2<=n<=5⋅10
5
) — the number of distinct stamps, available for sale in the ‘Robin Bobin’ store.

The second line contains a sequence of integers a_{1},a_{2},…,a_{n}a
1

,a
2

,…,a
n

( 2<=a_{i}<=10^{7}2<=a
i

<=10
7
), where a_{i}a
i

is the price of the ii -th stamp.

输出格式
Print a single integer — the remainder of the sought number of situations modulo 10^{9}+710
9
+7 .

题意翻译
有一个包含 nn 个 \in [2, 10^7]∈[2,10
7
] 的整数的可重集合。
要求满足条件的一个元素 xx 和一个集合 SS 的方案数。
条件:x \notin Sx∈
/

S,\gcd{S} > 1gcd{S}>1,\gcd(x, \gcd{S}) = 1gcd(x,gcd{S})=1。
n \le 5 \times 10^5n≤5×10
5
,答案对 10^9+710
9
+7 取模。
输入输出样例
输入 #1复制
3
2 3 2
输出 #1复制
5
输入 #2复制
2
9 6
输出 #2复制
0
说明/提示
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.

思路

枚举 d = g c d { S } d = gcd\{S\} d=gcd{S},设 c c c d d d 的倍数的个数,则 d d d 对应的方案数为 ( n − c ) × ( 2 c − 1 ) (n-c) \times (2^c - 1) (nc)×(2c1)
显然有算重的,容斥一下即可,容斥系数恰好为莫比乌斯函数的相反数。
时间复杂度 O ( w log ⁡ w ) \mathcal O(w \log w) O(wlogw)

代码

const int N = 5e5 + 7, M = 1e7 + 7;
int n, m, x, c[M], p[M], cnt, v[M];
modint num[N], miu[M], ans;

int main() {
    rd(n), num[0] = 1;
    for (int i = 1; i <= n; i++) rd(x), m = max(m, x), c[x]++, num[i] = num[i-1] * 2;
    miu[1] = 1;
    for (int i = 2; i <= m; i++) {
        if (!v[i]) v[i] = p[++cnt] = i, miu[i] -= 1;
        for (int j = 1; j <= cnt && i * p[j] <= m && p[j] <= v[i]; j++)
            v[i*p[j]] = p[j], miu[i*p[j]] = p[j] == v[i] ? 0 : -miu[i];
    }
    for (int i = 1; i <= m; i++) {
        cnt = 0;
        for (int j = i; j <= m; j += i) cnt += c[j];
        ans += (-miu[i]) * (n - cnt) * (num[cnt] - 1);
    }
    print(ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值