链接:CF839D

题目大意

给定一个数组大小为\(n(1\leq n\leq 200000)\)的数组\(a\),满足\(1\leq a_i \leq 1000000\)。

选择其中任意\(len\)个数字,若\(gcd>1\),则该组数字对答案贡献为\(len*gcd\),求最终答案对\(1e9+7\)取模。

题目分析

因为\(gcd\)的本质是因数分解,可以想到,如果不考虑算重,那么:

对于一个\(gcd\)的结果\(x\),若有\(num\)个数含有\(x\)这个因数,则被选择的数可形成的形如\(gcd(...)==x\)的贡献为:

\((1*C_{num}^1+2*C_{num}^2+3*C_{num}^3+......+num*C_{num}^{num})*x\)

其中,\(num\)可以直接枚举得到:

\(for(int\ i=x;i<=1000000;i+=x)num[x]+=cnt[i];\)

\(cnt[i]\)表示\(a[\ ]\)中大小为\(i\)的数字的个数,由于时间复杂度是调和级数,可以\(O(nlog(n))\)求解。

由公式可得:

\(1*C_{n}^1+2*C_{n}^2+3*C_{n}^3+......+n*C_{n}^{n}=n*2^{n-1}\)

此处可以\(O(1)\)求得答案,总时间复杂度为\(O(nlog(n))\)。


由于会有重复的情况,我们可以使用容斥去重。

对于每个数的容斥系数,可以附初值:\(tmp[i]=i;\)

由于每个数会在它的因数部分算重,可得:

\(tmp[x]=x- \sum\limits_{d|x}tmp[d];\)

\(tmp\)数组的计算也是调和级数,可以在\(O(nlog(n))\)求解。


综上:

\(ans+=num[x]*2^{num[x]-1}*x*tmp[x];\)

总时间复杂度\(O(nlog(n))\)。

代码实现

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#define MAXN 0x7fffffff
typedef long long LL;
const int N=1000005,mod=1e9+7;
using namespace std;
inline int Getint(){register int x=0,f=1;register char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}return x*f;}
int tmp[N],prime[N];
bool vis[N];
void Pre(int n){
for(int i=2;i<=n;i++)tmp[i]=i;
for(int i=2;i<=n;i++){
if(!vis[i])prime[++prime[0]]=i,vis[i]=1;
for(int j=1;j<=prime[0]&&1ll*i*prime[j]<=n;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
for(int j=2;1ll*i*j<=n;j++)tmp[j*i]-=tmp[i];
}
}
LL ksm(LL x,LL k){
LL ret=1;
while(k){
if(k&1)ret=ret*x%mod;
x=x*x%mod;
k>>=1;
}
return ret;
}
int cnt[N];
int Query(int x){
int ret=0;
for(int i=x;i<=1000000;i+=x)ret+=cnt[i];
return ret;
}
int main(){
Pre(1000000);
int n=Getint();
for(int i=1;i<=n;i++)cnt[Getint()]++;
LL ans=0;
for(int i=2;i<=1000000;i++){
int x=Query(i);
if(!x)continue;
ans=(ans+x*ksm(2,x-1)%mod*tmp[i]%mod)%mod;
}
cout<<(ans+mod)%mod;
return 0;
}

Codeforces 839D Winter is here的更多相关文章

  1. CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

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

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

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

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

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

  5. codeforces 747D. Winter Is Coming(贪心)

    题目链接:http://codeforces.com/problemset/problem/747/D 题意:冬天有n天,冬天用的轮胎总共能用k天,一开始车子用的是夏天的轮胎. 给出n天的平均气温,温 ...

  6. CodeForces 747D Winter Is Coming

    贪心. 只考虑负数的位置,先填间隔较小的,再填间隔较大的.如果填不满就不填,如果有多余就留给最后一个负数到终点这段路. #include<cstdio> #include<cstri ...

  7. codeforce 839d.winter is here

    题意:如果一个子序列的GCD为1,那么这个子序列的价值为0,否则子序列价值为子序列长度*子序列GCD 给出n个数,求这n个数所有子序列的价值和 题解:首先得想到去处理量比较少的数据的贡献,这里处理每个 ...

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

  9. 【23.26%】【codeforces 747D】Winter Is Coming

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. javascript 插件开发教程

    如何自己开发一款js或者jquery插件 引子 初学js不久,接触到js插件开发,其实很简单,不像网上吹嘘的那么复杂,又要掌握js,掌握jquery,其实没有那么复杂,下面简单介绍,供学习使用. jq ...

  2. Contos7 FTP 安装步骤

    1. 使用rpm -q vsftpd查看是否已安装2.如未安装使用yum -y install vsftpd安装3.修改ftp配置文件vim /etc/vsftpd/vsftpd.conf,修改内容如 ...

  3. (干货)java中如何根据一个时间获取属于本年那一周,本周的开始时间以及最后一天时间。并且设置起始时间为周6.结束时间为周5

    本人亲测,有用,适用性比较强,直接上代码说话. package com.helloBike.data; import java.text.ParseException; import java.tex ...

  4. [USACO11OPEN]玉米田迷宫Corn Maze

    题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...

  5. USACO2006 Backward Digit Sums /// 全排列 oj24212

    题目大意: 给出杨辉三角的顶点值M和底边数的个数 N (1 ≤ N ≤ 10) ,求出底边各个数的值,其中各个数范围都为1 ~ N 当N=4,M=16时可能是这样的   3   1   2   4  ...

  6. Substring UVA - 11468 AC自动机+概率DP

    题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...

  7. 【校OJ】选网线

    暑假学校OJ上的题目. 一道很有意思的二分. 题意:三个数组,每个数组各选一个数出来看是否能组成目标数. 题解:前两个数组两两的和组合一下,二分第三个数组,找是否能组成目标数. 代码: #includ ...

  8. NFS服务器简易安装

    1.服务端 创建挂载目录 # mkdir /data/nfs 安装NFS软件 # yum install nfs-utils -y 添加配置信息 # vim /etc/exports /data/nf ...

  9. JS对象 颠倒数组元素顺序reverse() reverse() 方法用于颠倒数组中元素的顺序。

    颠倒数组元素顺序reverse() reverse() 方法用于颠倒数组中元素的顺序. 语法: arrayObject.reverse() 注意:该方法会改变原来的数组,而不会创建新的数组. 定义数组 ...

  10. scala入门基础学习

    1.Scala基础语法 区分大小写 类名 - 对于所有的类名的第一个字母要大写.如果需要使用几个单词来构成一个类的名称,每个单词的第一个字母要大写. 方法名称 - 所有的方法名称的第一个字母用小写. ...