/**
题目: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. VMware安装报VT-x未开启的解决办法

    摘自: http://www.bloomylife.com/?p=650 前段时间MSDN上放出WIN8最终版的ISO文件,心里一直痒痒.最近闲来无事,想体验下WIN8的魅力.考虑到新系统刚面世,在驱 ...

  2. Java笔记17:导出可执行jar包

    这里介绍Java程序的两种导出可执行jar包的方式,一种是图形界面打包,另一种是命令行打包. 一.图形界面打包 1 建立名为JarDemo的JavaProject,在src目录下建立com.abc的P ...

  3. jsp页面数据回显(select下拉选择框)

    1.静态变量方式: <!-- 实现select标签回显 --> 1.<select name="curStatus" value="${curStatu ...

  4. TortoiseSVN版本管理软件使用简单说明

    很多时候在写一个小的项目不想使用github等工具,只想简单在本地搭建一个版本管理器.那么TortoiseSVN就非常适合. 第一步:下载TortoiseSVN,http://tortoisesvn. ...

  5. HDU 5296 Annoying problem

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. 算法笔记_098:蓝桥杯练习 算法提高 盾神与条状项链(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 有一天,盾神捡到了好多好多五颜六色的珠子!他心想这些珠子这么漂亮,可以做成一条项链然后送给他心仪的女生~于是他用其中一些珠子做成了长度为n ...

  7. js 数组分解 解构

    // ES5 a = list[0], rest = list.slice(1) // ES6 [a, ...rest] = list

  8. [leetcode]Path Sum--巧用递归

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 百度URL參数解析

    百度URL參数解析 在用Python爬取百度搜索的内容时,发现百度搜索的url非常的长.往往会跟一大段的參数,但事实上非常多參数都是没有必要的,如相同是搜索javakeyword,能够通过 http: ...

  10. Hadoop-2.4.1学习之怎样确定Mapper数量

    MapReduce框架的优势是能够在集群中并行运行mapper和reducer任务,那怎样确定mapper和reducer的数量呢,或者说怎样以编程的方式控制作业启动的mapper和reducer数量 ...