D. Winter is here

time limit per test:3 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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

题目链接:http://codeforces.com/contest/839/problem/D

下面给出AC代码:

 #include<cstdio>
const int N=,P=;
int n,i,j,x,ans,po[N],a[N],f[N];
int main(){
scanf("%d",&n);
for(po[]=i=;i<=n;i++)po[i]=*po[i-]%P;
while(n--)scanf("%d",&x),a[x]++;
for(i=N-;i>;i--){
for(j=i,x=;j<N;j+=i)x+=a[j];
if(x){
f[i]=1LL*x*po[x-]%P;
for(j=i+i;j<N;j+=i)f[i]=(f[i]-f[j]+P)%P;
ans=(1LL*f[i]*i+ans)%P;
}
}
printf("%d",ans);
}

Codeforces 839D Winter is here【数学:容斥原理】的更多相关文章

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

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

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

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

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

  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 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  7. Codeforces 627 A. XOR Equation (数学)

    题目链接:http://codeforces.com/problemset/problem/627/A 题意: 告诉你s 和 x,a + b = s    a xor b = x   a, b > ...

  8. Codeforces Beta Round #2B(dp+数学)

    贡献了一列WA.. 数学很神奇啊 这个题的关键是怎么才能算尾0的个数 只能相乘 可以想一下所有一位数相乘 除0之外,只有2和5相乘才能得到0 当然那些本身带0的多位数 里面肯定含有多少尾0 就含有多少 ...

  9. codeforces 803C Maximal GCD(GCD数学)

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

随机推荐

  1. Ubuntu系统怎么切换多用户命令界面

    ctrl+alt+F2~F6 切换窗口 返回桌面 Ctrl+Alt+F7

  2. 【ANT】创建删除目录,复制移动重命名文件

    一.创建目录: <?xml version="1.0"?> <project default="test_mkdir"> <tar ...

  3. 【MySQL】查看字符集对应的校对规则show collation like 'gbk%';

  4. JavaWeb 例子 JDBC+JSP登陆注册留言板

    注册页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  5. [array] leetCode-27. Remove Element - Easy

    27. Remove Element - Easy descrition Given an array and a value, remove all instances of that value ...

  6. arcgis api for js之echarts开源js库实现地图统计图分析

    前面写过一篇关于arcgis api for js实现地图统计图的,具体见:http://www.cnblogs.com/giserhome/p/6727593.html 那是基于dojo组件来实现图 ...

  7. CSS篇(上)

    紧接着HTML篇的CSS篇开啦,老铁们快来围观... 1.介绍一下标准的CSS盒子模型?低版本IE的盒子模型有什么不同? 1>有两种:IE盒子模型      W3C盒子模型 2>盒模型:内 ...

  8. 谈谈序列化—实体bean一定要实现Serializable接口?

    导读:最近在做项目的过程中,发现一个问题,就是我们最开始的时候,传递参数包括返回类型,都有map类型.但是由于map每次都要匹配key值,很麻烦.所以在之后就将参数传递和返回类型全都改成了实体bean ...

  9. jQuery 文档操作方法

    jQuery 文档操作方法 这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html(). 方法 描述 addClass() 向匹配的元素添加指定的类名. after() 在匹配的元素之 ...

  10. C# 各种帮助类大全

    前言 此篇专门记录一些常见DB帮助类及其他帮助类,以便使用时不用重复造轮子. DBHelper帮助类 ①首当其冲的就是Sql Server帮助类,创建名为DbHelperSQL 的类 ,全部代码如下: ...