/**
题目:Codeforces839D Winter is here
链接:http://codeforces.com/contest/839/problem/D
题意:给定n个数,求所有的最大公约数>1的子集的贡献之和。
一个子集的贡献为最大公约数*子集大小
思路:
cnt[i]表示约数为i的数的个数。
ans[i]表示约数为i的所有k的和。 已知ans[i]可以求得贡献为i*ans[i]; cnt[i]个数的长度为(sigma(k))cnt[i]*(2^(cnt[i]-1)); 即所有子集长度的和。
但是其中有些子集的gcd不是等于i。所以要减去。
逆序枚举,ans[i] = cnt[i]*(2^(cnt[i]-1)) - sigma(ans[j]);(j%i==0&&j/i>1) */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
int cnt[maxn];
int sum[maxn];
LL ans[maxn];
LL Pow(LL x, int y)
{
LL p = ;
while (y)
{
if (y & ) p = p*x%mod;
x = x*x%mod;
y >>= ;
}
return p;
}
int main()
{
int T, cas = ;
int n;
while (scanf("%d",&n)==)
{
int x, mas = ;
ms(cnt,);
ms(sum,);
ms(ans,);
for(int i = ; i <= n; i++){
scanf("%d",&x);
mas = max(mas,x);
sum[x]++;
}
for(int i = ; i < maxn; i++){
for(int j = i; j < maxn; j+=i){
cnt[i] += sum[j];
}
}
LL res = ;
for(int i = mas; i >= ; i--){
if(cnt[i]==) continue;
LL num = ;
for(int j = *i; j <= mas; j+=i){
num = (num+ans[j])%mod;
}
ans[i] = (cnt[i]*Pow(,cnt[i]-)%mod-num+mod)%mod;
res = (res+ans[i]*i%mod)%mod;
}
printf("%I64d\n",res);
} return ;
}

Codeforces839D Winter is here 容斥的更多相关文章

  1. Codeforces Round #428 (Div. 2) D. Winter is here 容斥

    D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...

  2. POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)

      题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...

  3. HDU 4059 容斥初步练习

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  4. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  5. 【BZOJ-4455】小星星 容斥 + 树形DP

    4455: [Zjoi2016]小星星 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 204  Solved: 137[Submit][Status] ...

  6. cf#305 Mike and Foam(容斥)

    C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. UVa12633 Super Rooks on Chessboard(容斥 + FFT)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/42145 Description Let’s assume there is a new chess ...

  8. PE-1 & 暴模|容斥

    题意: 求1000以下3或5的倍数之和. SOL: 暴模也是兹瓷的啊... 那么就想到了初赛悲催的滚粗...容斥忘了加上多减的数了... 然后对着题...T = 3*333*(1+333)/2 + 5 ...

  9. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

随机推荐

  1. [OpenCV]实验1.1:图像加载、显示

    实验要求:利用图像库的功能,实现从文件加载图像,并在窗口中进行显示的功能:利用常见的图像文件格式(.jpg;.png;.bmp; .gif)进行测试 实验原理:图片读取到程序中是以Mat结构存储的,在 ...

  2. ​Mac触控板常用的手势操作

    ​Mac触控板常用的手势操作 学习了:http://topbook.cc/archives/151   一个手指直接点击,类似Windows中鼠标左键功能,同时在苹果Safari等浏览器中,这个手势还 ...

  3. keepalived 配置需要注意的问题

    keepalived 配置过程中遇到了一些问题,做个记录: 1.selinux的影响:keepalived配置了vrrp_script脚本总是无效      注:脚本返回值0代表成功,1或其他非0值代 ...

  4. Hibernate从入门到上手(纯java project、Maven版本hibernate)

    Hibernate(orm框架)(开放源代码的对象关系映射框架) Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一 ...

  5. Cocos2d-x中如何增加图片和文本菜单

    菜单都以MenuItem开头 MenuItemLabel - 文本菜单项 MenuItemImage - 图片菜单项 // on "init" you need to initia ...

  6. RTSP - RTP over TCP

    RTP over RTSP(TCP)(一)   RTP over RTSP包混合发送的解决办法   RTSP - RTP over TCP     To use TCP communication, ...

  7. ffmpeg怎么样处理网络流

    http://blog.sina.com.cn/s/blog_675142dc01010otk.html 最近遇到好几个人在问ffmpeg如何处理网络流,刚好前段时间也在做这方面,抽空整理了下,把主要 ...

  8. Git学习笔记一--创建版本库、添加文件、提交文件等

    Git,是Linus花了两周时间用C写的一个分布式版本控制系统.牛该怎么定义? 其实,很多人都不care谁写了Git,只在乎它是免费而且好用的!So do I! 下面开始我们的学习: 1.Git安装( ...

  9. Hibernate开发环境搭建

    一.下载Hibernate包的下载 官网地址:http://hibernate.org/orm/ 下载版本:hibernate-release-4.3.11.Final 二.Hibernate jar ...

  10. 尚学堂 hadoop

    mr spark storm 都是分布式计算框架,他们之间不是谁替换谁的问题,是谁适合做什么的问题. mr特点,移动计算,而不移动数据. 把我们的计算程序下发到不同的机器上面运行,但是不移动数据. 每 ...