赛后听 Forever97 讲的思路,强的一匹- -

/*
CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Div. 2)
题意:
给出数列a[N]
对每个子集,若 gcd(a[I1], a[I2], a[I3] ..., a[In]) > 1,则贡献为 n*gcd
求总贡献和
限制: N <= 2e5,a[i] <= 1e6
分析:
记录 num[i]数组为 i 的倍数的个数
则 gcd >= i 能组成的所有方案的总人数 f(i) = 2^(num[i]-1)*num[i]
设 g(i) 为 gcd == i 能组成的所有方案的总人数
可得 f(x) = ∑ [x|y] g(y)
反演或者容斥即可
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 1e6+5;
const LL MOD = 1e9+7;
int n, a[N], num[N], Max;
LL two[N], sum[N];
int main()
{
two[0] = 1;
for (int i = 1; i < N; i++) two[i] = two[i-1] * 2 % MOD;
scanf("%d", &n);
Max = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
Max = max(a[i], Max);
num[a[i]]++;
}
for (int i = 1; i <= Max; i++)
for (int j = i+i; j <= Max; j += i)
num[i] += num[j];
LL ans = 0;
for (int i = Max; i >= 2; i--)
{
sum[i] = two[num[i]-1]*num[i] % MOD;
for (int j = i+i; j <= Max; j += i)
{
sum[i] = (sum[i] - sum[j] + MOD) % MOD;
}
ans = (ans + sum[i] * i % MOD) % MOD;
}
printf("%lld\n", ans);
}

比赛时候写的很随意- -,不过思路是一样的

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL MOD = 1e9+7;
const int N = 1000005;
bool notp[N];
int prime[N], pnum, mu[N];
void Mobius() {
memset(notp, 0, sizeof(notp));
mu[1] = 1;
for (int i = 2; i < N; i++) {
if (!notp[i]) prime[++pnum] = i, mu[i] = -1;
for (int j = 1; prime[j]*i < N; j++) {
notp[prime[j]*i] = 1;
if (i%prime[j] == 0) {
mu[prime[j]*i] = 0;
break;
}
mu[prime[j]*i] = -mu[i];
}
}
}
int n, a[N], Max;
int num[N];
LL two[N];
int main()
{
two[0] = 1;
for (int i = 1; i < N; i++) two[i] = two[i-1]*2 % MOD;
Mobius();
scanf("%d", &n);
Max = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
Max = max(Max, a[i]);
for (LL j = 1; j*j <= a[i]; j++)
{
if (j*j == a[i]) num[j]++;
else if (a[i] % j == 0)
num[j]++, num[a[i]/j]++;
}
}
LL ans = 0;
for (int i = 2; i <= Max; i++)
{
LL sum = 0;
for (int j = i, k = 1; j <= Max; j += i, k++)
{
sum += (mu[k] * (two[num[j]-1]*num[j])%MOD + MOD) % MOD;
sum %= MOD;
}
ans = (ans + sum * i%MOD) % MOD;
}
printf("%lld\n", ans% MOD);
}

  

CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)的更多相关文章

  1. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

  2. CodeForces 839B - Game of the Rows | Codeforces Round #428 (Div. 2)

    血崩- - /* CodeForces 839B - Game of the Rows [ 贪心,分类讨论] | Codeforces Round #428 (Div. 2) 注意 2 7 2 2 2 ...

  3. 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 ...

  4. 【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here

    给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和. 设sum(i)为i的倍数的数的个数,可以通过容斥算出来. 具体看这个吧:http://blog.csdn.net/j ...

  5. 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 ...

  6. Codeforces Round #428 (Div. 2) 题解

    题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...

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

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

  8. Codeforces 839D Winter is here(容斥原理)

    [题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累 ...

  9. Codeforces Round #428 (Div. 2)E. Mother of Dragons

    http://codeforces.com/contest/839/problem/E 最大团裸题= =,用Bron–Kerbosch算法,复杂度大多博客上没有,维基上查了查大约是O(3n/3) 最大 ...

随机推荐

  1. MySQL添加、修改、撤销用户数据库操作权限的一些记录

    查看MYSQL数据库中所有用户 SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; ...

  2. Python Excel文件的读写操作(xlwt xlrd xlsxwriter)

    转:https://www.cnblogs.com/ultimateWorld/p/8309197.html Python语法简洁清晰,作为工作中常用的开发语言还是很强大的(废话). python关于 ...

  3. SQLite进阶-13.Autoincrement关键字

    目录 AUTOINCREMENT 是一个关键字,用于表中的字段值自动递增.我们可以在创建表时在特定的列名称上使用 AUTOINCREMENT 关键字实现该字段值的自动增加. 关键字 AUTOINCRE ...

  4. 什么是阿里云CDN

    阿里云内容分发网络(Content Delivery Network,简称CDN)是建立并覆盖在承载网之上,由分布在不同区域的边缘节点服务器群组成的分布式网络.阿里云CDN分担源站压力,避免网络拥塞, ...

  5. 动态代理 aop切面实现事务管理

    1.定义接口和实现 public interface UserService { public String getName(int id); public Integer getAge(int id ...

  6. Linux系列(17)之系统服务

    我们知道,在我们登陆Linux后,系统就为我们提供了很多服务,比如例行工作调度服务crond.打印服务.邮件服务等.那么这些服务是如何被启动的呢? 这个问题先放一下,接下来我们先了解一下Linux的启 ...

  7. 剑指offer16:输入两个单调递增的链表,合成后的链表满足单调不减规则。

    1 题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 2 思路与方法 迭代法:两个链表中较小的头结点作为合并后头结点,之后依次合并两个链表中较小的 ...

  8. Laravel 最佳实践

    单一职责原则 一个类和一个方法应该只有一个责任. 例如: public function getFullNameAttribute() { if (auth()->user() &&am ...

  9. shell习题第16题:查用户

    [题目要求] 写个shell,看看你的Linux系统中是否有自定义的用户(普通用户),如有有的话统计个数 [核心要点] CentOS6,uid>=500 CentOS7,uid>=1000 ...

  10. 数据库分库分表策略之MS-SQL读写分离方案

    MS-SQL读写分离将从以下知识点进行展开: 以下截图内容来自博主:https://www.cnblogs.com/echosong/p/3603270.html 1.本地发布(写库如:centerd ...