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. nodeJS从入门到进阶三(MongoDB数据库)

    一.MongoDB数据库 1.概念 数据库(DataBase)是一个按照数据结构进行数据的组织,管理,存放数据的仓库. 2.关系型数据库 按照关系模型存储的数据库,数据与数据之间的关系非常密切,可以实 ...

  2. JAVA基础之会话技术-Cookie及Session

    至此,学习Servlet三个域对象:ServletContext(web项目).request(一次请求).Session(一个客户端)!均有相同的方法! 从用户开始打开浏览器进行操作,便开始了一次会 ...

  3. 嵌入式应用开发第四阶段-基于rk3399的视频监控系统

    一.需求分析 伴随着嵌入式技术.图像处理技术和无线网络传输技术的发展,传统模拟视频监控系统和基于PC的远程视频监控系统由于自身的不足,已经无法满足现代社会应用中不断涌现出来的新需求,于是基于嵌入式技术 ...

  4. day 05 预科

    目录 文本处理 什么是文件 什么是文本 视频/音频文件(多媒体文件) 我们如何通过文本编辑器去控制txt文件 文本高级 文本处理+高级分析 文本处理 什么是文件 文件是操作系统提供的一个特殊概念,拿来 ...

  5. MySQL删除语句

    删除数据(DELETE) 使用前需注意:删除(DELETE),是删除一(条)行数据.假如我们有四条(行)数据,换句话说,你要删除其中一条(行) 名字为“xx”的用户,那么关于他的 i所有数据都会被删除 ...

  6. Load Balancing in gRPC

    背景 基于每次调用的负载均衡 需要注意的是,gRPC的负载均衡发生在每次调用时,而不是每次连接时.换句话说,就算所有的请求来自于同一个客户,我们也希望可以将它们负载均衡到所有的服务器. 负载均衡的方法 ...

  7. https://www.runoob.com/linux/mysql-install-setup.html

    https://www.runoob.com/linux/mysql-install-setup.html

  8. 使用Arduino和LED光柱显示器件轻松制作电池电压指示器

    电池有一定的电压限制,如果电压在充电或放电时超出规定的限制,电池的使用寿命就会受到影响或降低.每当我们使用电池供电的项目,有时我们需要检查电池电压电量,确定是否需要充电或更换.本电路将帮助您监测电池电 ...

  9. django项目使用layui插件给网站设置一个日历挂件,很简单实用。

    进入https://www.layui.com/首页下载layui文件 下载解压后把文件放在static静态文件中, html页面引入css和js <link rel="stylesh ...

  10. CentOS7安装Chrome

    1. 进入官网:https://www.google.cn/intl/zh-CN/chrome/2. 点击下载3. 直接安装:sudo yum localinstall google-chrome-s ...