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) 最大 ...
随机推荐
- curl使用举例
我在银行工作时,一个具体的用例:shell脚本中使用的 sendAddr=`echo http:192.168.1.100:8080/cloud-monitor/perfaddperf` SendDa ...
- java xml解析方式(DOM、SAX、JDOM、DOM4J)
XML值可扩展标记语言,是用来传输和存储数据的. XMl的特定: XMl文档必须包含根元素.该元素是所有其他元素的父元素.XML文档中的元素形成了一颗文档树,树中的每个元素都可存在子元素. 所有XML ...
- ubuntu18安装PacketTracer-7.2.2
1.下载需要先注册个账号 https://www.netacad.com/zh-hans/courses/packet-tracer 2.运行.run文件 chmod +x PacketTracer ...
- (十三)springMvc 处理 Json
目录 文章目录 为什么用 Json 处理 json 的流程 环境准备 配置 json 转换器 后记 更新 为什么用 Json Json 格式简单,语法简单,解析简单 : 处理 json 的流程 判断客 ...
- codeforces 1251D Salary Changing (二分+贪心)
(点击此处查看原题) 题意分析 一共有s元钱,要用这些钱给n个人发工资,发给每个人的工资si有最少和最多限制 si ∈[li,ri],在发给n个人的总工资小于s的情况下,要求发给n个人中的工资的中位数 ...
- ajax 跨域要点
1.async: false 2.dataType: jsonp 3.返回数据格式.正常格式为{ param1: p1, param2: p2 },而jsonp跨域请求时,多了一个参数 callbac ...
- Class.getResources()和classLoader.getResources()区别
Class.getResource(String path) path不以’/'开头时,默认是从此类所在的包下取资源: path 以’/'开头时,则是从ClassPath根下获取: package t ...
- c# http文件上传
/// <summary> /// 上传文件的api /// </summary> [HttpPost] public string UploadFile(op_client_ ...
- shiro 权限过滤器 -------(1)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBEAAAJRCAIAAACcEbhqAAAgAElEQVR4nO3dv67sVtkHYEefhIKUIC ...
- C#动态生成Word文档并填充数据
C#也能动态生成Word文档并填充数据 http://www.cnblogs.com/qyfan82/archive/2007/09/14/893293.html 引用http://blog.csdn ...