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

简介: D. Winter is here time limit per test:3 seconds memory limit per test:256 megabytes input:standard input output:standard out...

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代码:

 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 }

 

目录
相关文章
|
12月前
codeforces 272C. Dima and Staircase(线段树)
题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。
27 0
|
人工智能
codeforces455——A. Boredom(线性DP)
codeforces455——A. Boredom(线性DP)
117 0
codeforces455——A. Boredom(线性DP)
|
人工智能 vr&ar Perl
codeforces1509 C. The Sports Festival (区间DP)
codeforces1509 C. The Sports Festival (区间DP)
105 0
|
人工智能
Codeforces-1260-E. Tournament贪心
题意: 有n个人,第i个人有力量值i,这n个人中,每次对局两两之间进行solo,如果说一个人的力量之大于他的对手,这个人是赢的,赢得比赛的选手会进入下一轮比赛中。 然而里面有一个人为你的朋友,他或许并不是里面最强的人,要想让朋友成为冠军,就需要对必要的人进行贿赂,求出最少需要花费多少才能使得朋友成为冠军。在每次对局中,都可以进行任意的对局安排,对局安排只取决于自己
90 0
|
Java
HDOJ/HDU 1865 1sting(斐波拉契+大数~)
HDOJ/HDU 1865 1sting(斐波拉契+大数~)
97 0
|
Java
HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)
HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)
97 0
|
人工智能
codeforces1143B Nirvana(贪心)
codeforces1143B题解(贪心)
970 0