赛后听 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. checkBox复选框,获得选中那一行所有列的数据

    function showCol(){ var check=$("input[name='one']:checked");//选中的复选框 check.each(function( ...

  2. javascript学习笔记 BOM和DOM详解

    js组成 我们都知道, javascript 有三部分构成,ECMAScript,DOM和BOM,根据宿主(浏览器)的不同,具体的表现形式也不尽相同,ie和其他的浏览器风格迥异. 1. DOM 是 W ...

  3. ibox 的使用

    <div class="ibox float-e-margins"> <div class="ibox-title"> <h5&g ...

  4. springboot中配置文件使用1

    1.表达方式:application.properties或者application.yml,这是已经约定成俗的文件,不用修改文件名,此文件为全局配置文件. 2.语法格式:yml或者yaml. a.基 ...

  5. C++继承种类

  6. 怎样修改 VS Code 主题?

    方法1. 点击左上角 File > Preferences > Color Theme. 方法2. 使用快捷键: Ctrl + K , Ctrl + T  PS: 查询各种操作的快捷键可以 ...

  7. oracle基本查询01

    /*数据库-----> 数据库实例----->表空间[逻辑单位]------>数据文件[物理单位] 通常情况下oracle数据库只会有一个实例ORCL 新建一个项目: MYSQL:创 ...

  8. 逆向知识第九讲,switch case语句在汇编中表达的方式

    一丶Switch Case语句在汇编中的第一种表达方式 (引导性跳转表) 第一种表达方式生成条件: case 个数偏少,那么汇编中将会生成引导性的跳转表,会做出 if else的情况(类似,但还是能分 ...

  9. postman中传参说明

    1.form-data 表单传递,对应multipart/form-data, 2.x-www-form-urlencoded 默认传递,对应application/x-www-from-urlenc ...

  10. springboot由于bean加载顺序导致的问题

    先记录现象: dubbo整合zipkin时,我的配置文件是这样的 @Bean("okHttpSender") public OkHttpSenderFactoryBean okHt ...