CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)
赛后听 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)的更多相关文章
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- 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 ...
- 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 ...
- 【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here
给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和. 设sum(i)为i的倍数的数的个数,可以通过容斥算出来. 具体看这个吧:http://blog.csdn.net/j ...
- 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 ...
- Codeforces Round #428 (Div. 2) 题解
题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...
- Codeforces 839D Winter is here【数学:容斥原理】
D. Winter is here time limit per test:3 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces 839D Winter is here(容斥原理)
[题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累 ...
- Codeforces Round #428 (Div. 2)E. Mother of Dragons
http://codeforces.com/contest/839/problem/E 最大团裸题= =,用Bron–Kerbosch算法,复杂度大多博客上没有,维基上查了查大约是O(3n/3) 最大 ...
随机推荐
- Websocket基础梳理
Websocket原理: websocket介绍: WebSocket(http://dev.w3.org/html5/websockets)是HTML5规范(http://www.w3.org/TR ...
- Git在IDEA工具中快捷拉取代码
在拥有GitLab账号之后, 进入IDEA中,点击vcs菜单-->Checkout from Version Control-->Git 随后会出现一个弹框,输入git上的项目地址点击CL ...
- Reids 连环炮面试(转)
出处: <今天面试了吗>-Redis Redis是什么 面试官:你先来说下redis是什么吧 我:(这不就是总结下redis的定义和特点嘛)Redis是C语言开发的一个开源的(遵从BSD ...
- varnish应用
Nginx+Varnish+基本业务 ngnix nginx.conf配置文件 user root; worker_processes ; error_log logs/error.log crit; ...
- Antd中,Form和Select联合使用,导致placeholder不生效分析
在使用antd的form组件时候,需要对Select组件进行语体示,placeholder,但是写的值并不生效 效果如上,但是现实的时候不生效,经检查发现,组件需要传递的是undefined,如果传入 ...
- php curl post请求
/** * CreateBy Song * @param String $url url地址 * @param Array $post url参数 * @return Array */ functio ...
- python 使用三种常用的工具包处理图片
matplotlib,PIL(Pillow),Opencv三种常用的作图方式. 使用matplotlib画图,很棒,matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab ...
- Django多对多
表名小写+_set() 得到的是一个QuertSet集合,她的后面可以跟 .add() .remove() .update() .clear() models.py 文件 # 学生表 ...
- LeetCode 腾讯精选50题--求众数
由于众数是指数组中相同元素的个数超过数组长度的一半,所以有两种思路,一. 先排序,后取排序后的数组的中间位置的值:二. 统计,设定一个变量统计相同元素出现的次数,遍历数组,若与选定的元素相同,统计变量 ...
- js重点——作用域——内部原理(二)
本篇是深入分析和理解作用域的第一篇——内部原理和工作模型. 我们知道作用域是变量,对象,函数可访问的一个范围.这说明了我们需要一套良好的规则来存储变量,之后方便查找.所以我们首先要理解的是在哪里而且怎 ...