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) 最大 ...
随机推荐
- Hadoop介绍与安装
前言 最近想学习下大数据,有点急于求成,于是去网上找了各种培训机构的视频,发现大都质量不佳,理论基本不说或者简单讲下,然后教你照猫画虎的敲代码,出了问题都没法分析.最后还是找了厦门大学的公开课从理论开 ...
- Junit测试类中如何调用Http通信
在使用Junit做测试的时候,有时候需要调用Http通信,无论是request还是response或者是session会话,那么在测试类里该如何调用呢,其实很简单,spring给我们提供了三个类 or ...
- vue的基本语法
在学习vue之前,我们应了解一下什么是vue.js? 什么是Vue.js? Vue.js是目前最后一个前端框架,React是最流行的一个前端框架(react除了开发网站,还可以开发手机App,Vue语 ...
- WPF 位图处理相关类
位图的存储方式 开始之前,先了解下位图的存储方式 位图的像素都分配有特定的位置和颜色值.每个像素的颜色信息由RGB组合或者灰度值表示,根据位深度,可将位图分为1.4.8.16.24及32位图像等.每个 ...
- Istio技术与实践01: 源码解析之Pilot多云平台服务发现机制
服务模型 首先,Istio作为一个(微)服务治理的平台,和其他的微服务模型一样也提供了Service,ServiceInstance这样抽象服务模型.如Service的定义中所表达的,一个服务有一个全 ...
- Java String类源码
String类的签名(JDK 8): public final class String implements java.io.Serializable, Comparable<String&g ...
- 排序之快排(JS)
快速排序(Quicksort)是对冒泡排序的一种改进. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分 ...
- C#签名验签帮助类
using System; using System.IO; using System.Text; using System.Collections.Generic; using System.Sec ...
- 怎样理解AJAX
AJAX: Asynchronous JavaScript and XML, 翻译过来就是: 异步的JavaScript与XML 这已经成为了一个通用名词, 字面意义已经消失了, 因为现在使用Java ...
- request.getScheme() 使用方法(转)
今天在看代码时,发现程序使用了 request.getScheme() .不明白是什么意思,查了一下.结果整理如下: 1.request.getScheme() 返回当前链接使用的协议:一般应用返回h ...