zoukankan      html  css  js  c++  java
  • [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
    ", (tot[1]+mod)%mod);
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    Android 生命周期
    adb 的相关操作及遇到的问题
    各种内部类
    Android 的系统架构
    Hibernate的各种关联关系
    Eclipse常用的快捷键
    Android 开发前的基本的配置及第一个Android 程序
    spring MVC Validation(表单验证)
    创建 spring MVC项目的基本步骤
    Android_demo之生成二维码
  • 原文地址:https://www.cnblogs.com/kirai/p/6843094.html
Copyright © 2011-2022 走看看