[cf803F] Coprime Subsequences(组合数学,容斥)

题目链接:http://codeforces.com/contest/803/problem/F

题意:n个数,统计所有满足gcd为1的子序列的个数。

统计各个数字出现的次数,然后容斥。

从10000扫到1,枚举所有倍数,把x的倍数出现的次数和统计出来,然后再统计x的倍数出现的次数和-1(减掉空集),然后作差。

倒着扫是因为,假如先前遇到了同倍数的,要减掉,如果正着扫就会容斥不到。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 const int maxn = 100100;
 6 const LL mod = (LL)1e9+7;
 7 int n, a;
 8 LL vis[maxn], tot[maxn];
 9 
10 LL mul(LL x, LL n) {
11     LL tot = 1;
12     for(; n; x=(x*x)%mod, n>>=1) if(n & 1) tot = (tot * x) % mod;
13     return tot;
14 }
15 
16 int main() {
17     // freopen("in", "r", stdin);
18     while(~scanf("%d",&n)) {
19         memset(vis, 0, sizeof(vis));
20         memset(tot, 0, sizeof(tot));
21         for(int i = 1; i <= n; i++) {
22             scanf("%d", &a);
23             vis[a]++;
24         }
25         for(int i = 100000; i >= 1; i--) {
26             LL X = 0, Y = 0;
27             for(int j = 1; i * j <= 100000; j++) {
28                 X = (X + vis[i*j]) % mod;
29                 Y = (Y + tot[i*j]) % mod;
30             }
31             tot[i] = (mul(2, X) - 1 - Y + mod) % mod;
32         }
33         printf("%I64d\n", (tot[1]+mod)%mod);
34     }
35     return 0;
36 }

 

转载于:https://www.cnblogs.com/kirai/p/6843094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值