【题目链接】 http://codeforces.com/contest/839/problem/D

【题目大意】

  给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上,
  求累加和。

【题解】

  我们枚举GCD,统计为其倍数的数字数量,先假定其能组成的集合数为贡献,
  但是我们发现在统计的过程中有多余统计的部分,比如4和8是2的倍数,
  然而它们的GCD等于4,所以我们对于每个集合数的贡献要减去所有其倍数的集合数的贡献,
  倒着容斥即可。

【代码】

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1000010,MOD=1e9+7;
int n,ans=0,w[N],dp[N],pw[N],mx;
int main(){
scanf("%d",&n);
for(int i=pw[0]=1;i<=n;i++)pw[i]=2*pw[i-1]%MOD;
for(int i=1,x;i<=n;i++)scanf("%d",&x),mx=max(x,mx),w[x]++;
for(int i=mx;i>1;i--){
int t=0;
for(int j=i;j<=mx;j+=i)t+=w[j];
if(!t)continue;
dp[i]=1LL*t*pw[t-1]%MOD;
for(int j=i+i;j<=mx;j+=i)dp[i]=(dp[i]-dp[j]+MOD)%MOD;
ans=(1LL*dp[i]*i+ans)%MOD;
}printf("%d\n",ans);
return 0;
}

Codeforces 839D Winter is here(容斥原理)的更多相关文章

  1. CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

  2. Codeforces 839D Winter is here - 暴力 - 容斥原理

    Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n s ...

  3. Codeforces 839D Winter is here【数学:容斥原理】

    D. Winter is here time limit per test:3 seconds memory limit per test:256 megabytes input:standard i ...

  4. Codeforces 839D Winter is here

    链接:CF839D 题目大意 给定一个数组大小为\(n(1\leq n\leq 200000)\)的数组\(a\),满足\(1\leq a_i \leq 1000000\). 选择其中任意\(len\ ...

  5. Codeforces 451E Devu and Flowers(容斥原理)

    题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组 ...

  6. 2019.02.09 codeforces gym 100548F. Color(容斥原理)

    传送门 题意简述:对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数.(n,m≤1e9,k≤1e6n,m\le1e9,k\le1e6n,m≤ ...

  7. [cf839d]Winter is here容斥原理

    题意:给定一个数列${a_i}$,若子序列长度为$k$,最大公约数为$gcd$,定义子序列的权值为$k*\gcd (\gcd  > 1)$.求所有子序列的权值和. 答案对10^9+7取模. 解题 ...

  8. codeforces 747D. Winter Is Coming(贪心)

    题目链接:http://codeforces.com/problemset/problem/747/D 题意:冬天有n天,冬天用的轮胎总共能用k天,一开始车子用的是夏天的轮胎. 给出n天的平均气温,温 ...

  9. codeforces 451E. Devu and Flowers 容斥原理+lucas

    题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...

随机推荐

  1. [CodePlus 2017 11月赛]晨跑 题解(辗转相除法求GCD)

    [CodePlus 2017 11月赛]晨跑 Description "无体育,不清华"."每天锻炼一小时,健康工作五十年,幸福生活一辈子".在清华,体育运动绝 ...

  2. 在C++11中实现监听者模式

    参考文章:https://coderwall.com/p/u4w9ra/implementing-signals-in-c-11 最近在完成C++大作业时,碰到了监听者模式的需求. 尽管C++下也可以 ...

  3. 【leetcode 简单】第二十三题 二叉树的最大深度

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...

  4. 20、什么样的项目适合Web自动化测试

    1.什么是Web自动化测试?概念:让程序代替人为自动验证Web项目功能的过程 2.什么Web项目适合做自动化测试 1.需求变动不频繁 2.项目周期长 3.项目需要回归测试 3.如阿进行Web自动化测试 ...

  5. [转载]PM管理技巧

      产品经理的沟通策略 2016年10月11日/分类: 文章 /编辑: Amy 产品经理处于沟通枢纽的位置,工作中需要跟各种岗位的人打交道,比如:领导.开发.运营.客户.用户.合作伙伴… 沟通能力是产 ...

  6. Tinyos 第三版Make系统

    1.make系统安装 cd tools ./Bootstrap ./configure make sudo make install 2.make系统结构 3.第三版Makerules文件部分解析 # ...

  7. weblogic性能监控

    1.

  8. Linux内核中的队列 kfifo【转】

    转自:http://airekans.github.io/c/2015/10/12/linux-kernel-data-structure-kfifo#api 在内核中经常会有需要用到队列来传递数据的 ...

  9. 概述sysfs文件系统【转】

    转自:http://blog.csdn.net/npy_lp/article/details/78933292 内核源码:linux-2.6.38.8.tar.bz2 目标平台:ARM体系结构 sys ...

  10. caffe Python API 之Solver定义

    from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file ...