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. Qt Creator 的下载与安装

    一.Qt和Qt Creator的区别 Qt是C++的一个库,或者说是开发框架,里面集成了一些库函数,提高开发效率. Qt Creator是一个IDE,就是一个平台,一个开发环境,类似的比如说VS,也可 ...

  2. JAVA基础之HttpServletRequest请求

    HttpServletRequest请求是获取请求行.请求头和请求体:可以通过这个方法设置防盗链,获取地址.牢记解决乱码的方式. 怎么选择是重定向还是转发呢?通常情况下转发更快,而且能保持reques ...

  3. CI隐藏入口文件index.php

    1.需要apache打开rewrite_module,然后修改httpd.conf的AllowOverride none 为AllowOverride All(里面,不同的环境目录不同) 2.在CI的 ...

  4. 正则表达式修饰符 i、g、m、s、U、x、a、D、e 等。

    正则表达式中常用的模式修正符有i.g.m.s.U.x.a.D.e 等. 它们之间可以组合搭配使用. i 不区分(ignore)大小写: 例如: /abc/i 可以匹配 abc.aBC.Abc g 全局 ...

  5. JavaWeb 之 Filter 验证用户登录案例

    需求: 1. 访问一个网站的资源.验证其是否登录 2. 如果登录了,则直接放行. 3. 如果没有登录,则跳转到登录页面,提示"您尚未登录,请先登录". 代码实现: import j ...

  6. weblogic如何修改密码&密码找回

    一.修改Console密码后,挂载的服务无法重启[解决办法] 1. 打开weblogic控制台,安全领域 --> myrealm --> 用户和组,将会看到weblogic用户,可以直接删 ...

  7. 【Iterm2】item2 ssh保持连接

    profiles -> sessions -> 勾选 When idel, send ASCII code就可以了

  8. 20.centos7基础学习与积累-006-软实力-画图

    从头开始积累centos7系统运用 亿图是用指南 安装亿图软件 修改基础配置 路径:文件==>选项==>常规 需要修改的参数: 撤销次数:256 自动保存间隔:2分钟 路径:文件==> ...

  9. OpenStack核心组件-keystone

    1. Keystone介绍 keystone是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户权限的定义等等 ...

  10. BLE——协议层次结构

    未完待续…… BLE协议 Bluetooth Application Applications GATT-Based Profiles/Services Bluetooth Core (Stack) ...