前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces 839D Winter is here【数学:容斥原理】

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

作者头像
Angel_Kitty
发布2018-04-09 15:41:40
5290
发布2018-04-09 15:41:40
举报
文章被收录于专栏:小樱的经验随笔

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

代码语言:javascript
复制
3
3 3 1

Output

代码语言:javascript
复制
12

Input

代码语言:javascript
复制
4
2 3 4 6

Output

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
 1 #include<cstdio>
 2 const int N=1000010,P=1000000007;
 3 int n,i,j,x,ans,po[N],a[N],f[N];
 4 int main(){
 5   scanf("%d",&n);
 6   for(po[0]=i=1;i<=n;i++)po[i]=2*po[i-1]%P;
 7   while(n--)scanf("%d",&x),a[x]++;
 8   for(i=N-1;i>1;i--){
 9     for(j=i,x=0;j<N;j+=i)x+=a[j];
10     if(x){
11       f[i]=1LL*x*po[x-1]%P;
12       for(j=i+i;j<N;j+=i)f[i]=(f[i]-f[j]+P)%P;
13       ans=(1LL*f[i]*i+ans)%P;
14     }
15   }
16   printf("%d",ans);
17 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-08-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • D. Winter is here
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档