/**
题目: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. 使用ERStudio创建数据表与ER图

    内容中包含 base64string 图片造成字符过多,拒绝显示

  2. strcpy,memcpy,memmove和内存重叠分析

    一:strcpy函数用法和实现: /* GNU-C中的实现(节选): */ char* strcpy(char *d, const char *s) { char *r=d; while((*d++= ...

  3. 阅读源代码的重要性:如厨师选食材,耍厨具——在Eclipse中怎样查看Java、Android源代码

    首先,非常多人说,不会看jdk中的源代码就不叫学过Java.显然这是肯定的.打个例如:真正的厨师须要从食材的选取.加工.到最后的烹饪.装盘成型,甚至到最后给用户介绍食用方法等一整套流程走下来.而实际上 ...

  4. Android adb shell学习心得(四)

    1.awk中的sub sub为替换函数.形式如 sub(/1/,"",$2) 将第二个变量的第一个1替换为空,若将sub替换为gsub.则替换全部的1. 2.awk中的-F 分隔符 ...

  5. Node.js abaike图片批量下载Node.js爬虫1.01版

    //====================================================== // abaike图片批量下载Node.js爬虫1.01 // 1.01 修正了输出目 ...

  6. Telnet服务配置

    telnet:远程连接,使用未加密的用户/密码组进行验证,由xinetd服务管理.配置文件为/etc/xinetd.d/telnet Telnet服务的配置步骤如下: 一.安装telnet软件包 #r ...

  7. C# 5 in a Nutshell - Delegate

    1. What is delegate in C#? A delegate is an object that knows how to call a method.A delegate type d ...

  8. 解决Xcode 6 编译Cocos2d-x iOS项目失败

    在Xcode 6 beta里编译Cocos2d-x iOS项目时可能会失败,提示如下错误: Undefined symbols for architecture i386: "_fwrite ...

  9. Codeforces Round #177 (Div. 2) 题解

    [前言]咦?如今怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛.一般我报一个数字A,随便再拉一个人选一个数字B.然后開始做第A^B场.假设认为机 ...

  10. Load和CPU利用率是如何算出来的 (转发)

    本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/system/how_to_cal ...