Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers.

He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans.

Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7).

Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.

Input

The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers.

Output

Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).

Examples
input
3
3 3 1
output
12
input
4
2 3 4 6
output
39
Note

In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12


  题目大意 给定n,集合A,设表示把这个集合内的所有数求最大公约数的结果,求

  根据常用套路,套一个循环去枚举gcd的结果,然后再求系数,于是有

  现在设,于是有

  现在考虑求f(i)。可以想到容斥原理。

  先假设所有的集合的gcd是i的倍数都符合条件然后计算答案(给定数集A中所有是i的倍数的数组成的集合任选一个子集),然后再减去f(2i), f(3i),...

  现在要面临两个问题

  1. 第一次求值如何处理?
    首先把式子写出来,设这个集合的大小为n,那么有

    因为

    对两边同时进行求导得到

    再带入x = 1得到

  2. 为是i的倍数的数的个数,如何快速求出?

    根据定义式有

    显然超时。虽然这是暴力,但是不够优美。
    表示,集合A中恰好为i的数有多少个。

    然后就可以得到总时间复杂度为O(mlog2m)的暴力:

  最后求求和就完事了。

Code

 /**
* Codeforces
* Problem#839D
* Accepted
* Time: 171ms
* Memory: 15400k
*/
#include <bits/stdc++.h>
using namespace std; const int lim = 1e6 + ;
const int moder = 1e9 + ; int n;
int *a;
int *pow2;
int cnt[lim], counter[lim];
int f[lim];
int res = ; inline void init() {
scanf("%d", &n);
a = new int[(n + )];
pow2 = new int[(n + )];
pow2[] = ;
for(int i = ; i <= n; i++) {
scanf("%d", a + i);
counter[a[i]]++;
pow2[i] = (pow2[i - ] << ) % moder;
}
} inline void solve() {
for(int i = ; i < lim; i++)
for(int j = i; j < lim; j += i)
cnt[i] += counter[j]; for(int i = lim - ; i > ; i--) {
if(!cnt[i]) continue;
f[i] = (cnt[i] * 1LL * pow2[cnt[i] - ]) % moder;
for(int j = i << ; j < lim; j += i)
f[i] = (f[i] - f[j]) % moder;
if(f[i] < ) f[i] += moder;
res = (res + (f[i] * 1LL * i) % moder) % moder;
} printf("%d\n", res);
} int main() {
init();
solve();
return ;
}

更新日志

  • 2017-11-30 更新两处指数错误

Codeforces 839D Winter is here - 暴力 - 容斥原理的更多相关文章

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

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

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

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

  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

    链接:CF839D 题目大意 给定一个数组大小为\(n(1\leq n\leq 200000)\)的数组\(a\),满足\(1\leq a_i \leq 1000000\). 选择其中任意\(len\ ...

  5. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  6. Codeforces Gym 100015H Hidden Code 暴力

    Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...

  7. Codeforces gym 100685 A. Ariel 暴力

    A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...

  8. Codeforces Gym 100637G G. #TheDress 暴力

    G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...

  9. [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力

    Couple Cover Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Descri ...

随机推荐

  1. 学习笔记之三十年软件开发之路 - Things I Learnt The Hard Way (in 30 Years of Software Development)

    三十年软件开发之路 https://mp.weixin.qq.com/s/EgN-9bIHonRid1DM0csQDw https://blog.juliobiason.net/thoughts/th ...

  2. ABAP-表修改日志存储

    1.设置了变更文档的,可以通过查看标准表CDHDR,CDPOS SE38   --  RSUSR200   ST03N -- 专家模式 2.SE16N维护的数据,可以查看表SE16N_CD_KEY,S ...

  3. Jmeter学习笔记(二十一)——Concurrency Thread Group阶梯式加压测试

    一.先安装jmeter的插件管理工具 1.下载地址:jmeter-plugins.org 点击plugins-manager.jar下载. 2.安装 把下载下来的文件plugins-manager.j ...

  4. linux技能点 二

    三.  文件操作:新增,删除,修改,查找,文件属性,文件内容查看,文件辅助命令,打包(解压缩),文件名注意事项.          新增:单文件,目录                         ...

  5. php 函数阶乘理解

    <?php //函数阶乘 函数调用自身,函数在执行的时候每次都会开辟一个空间,如 /** * $a =3的话,首先判断 3>1 为真 $r=3*demo(3-1) 开辟一个空间调用自身. ...

  6. MySQL Percona Toolkit--pt-osc学习

    工作流程 1.检查更改表是否有主键或唯一索引,是否有触发器 2.检查修改表的表结构,创建一个临时表,在新表上执行ALTER TABLE语句 3.在源表上创建三个触发器分别对于INSERT UPDATE ...

  7. 数据库与数据仓库的区别实际讲的是OLTP与OLAP的区别

    什么是数据仓库 数据仓库,英文名称为Data Warehouse,可简写为DW或DWH.数据仓库,是为企业所有级别的决策制定过程,提供所有类型数据支持的战略集合.它出于分析性报告和决策支持目的而创建. ...

  8. Apache Thrift安装介绍 (ubuntu)

    apache thrift是一种常用的远程服务调用框架. 下面对apache thrift的安装进行介绍: 下面是thrift的源码安装: Debian/Ubuntu (14+) 编译运行依赖安装 $ ...

  9. 第五次博客作业——Alpha2项目的测试

    格式描述: 这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 你的代码我的发 这个作业的目标 选取非自己所在团队的3个项目进行测试,并写出 ...

  10. ViCANdo新版本发布(PART1) | 点云库(PCL)集成

    激光雷达         随着智能驾驶技术的发展,激光雷达迅速的进入工程师的视野,不管是机械式.MEMS还是纯固态激光雷达,本质上都是以一定的速度扫描照射区域,在此过程中激光雷达不断的发出激光并接收反 ...