Codeforces 839D Winter are here-violence-repulsion principle

Source: Internet
Author: User
Tags greatest common divisor

Winter is, and the white Walkers is close. John Snow has a army consisting of n soldiers. While the rest of the world was fighting for the Iron Throne, he was going to get ready for the attack of the white Walkers.

He has created a method to know how strong He army is. Let theI-th soldier ' s strength beaI. For somekHe callsI1,I2,...,IkA clan IfI1 <I2 < i 3 < ... < i K  and  gcd ( a i 1, a i 2, ..., a i K ) > 1  . He calls the strength of that Clan  k GCD ( a i 1, a i 2, ..., a I K ). Then he defines the strength of him army by the sum of strengths in all possible clans.

Your task is to find the strength of he army. As the number may is very large, you has to print it modulo 1000000007 (9 + 7).

Greatest common divisor (GCD) of a sequence of integers is the maximum possible integer so, each element of the Sequen Ce 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 ( C10>1≤ ai ≤1000000)-denoting the strengths of his soldiers.

Output

Print one integer-the strength of John Snow ' s army modulo 1000000007 (9 + 7).

Examples input
3
3 3 1
Output
12
input
4
2 3 4 6
Output
39
Note

In the first sample the Clans is {1}, {2}, {1, 2} So the answer would be 1 3 + 1 3 + 2·3 =

  The topic is given n, set a, which represents the result of greatest common divisor all the numbers in this set, seeking.

According to the usual routines, set a loop to enumerate the results of the GCD, and then seek the coefficients, so there is

Now set, so there is

Now consider F (i). Can think of the principle of repulsion.

First assume that the GCD of all sets is a multiple of I meet the criteria and then compute the answer (a subset of the number of multiples of I in a given set of a), and then subtract F (2i), F (3i), ...

Now there are two problems to be faced

    1. How to deal with the first time evaluation?
      First the Bashi is written out, set the size of this set is N, then there is

      Because

      The simultaneous derivation of the two sides is obtained

      Then bring in x = 1 to get

    2. Set to is the number of multiples of I, how to quickly find out?

      According to the definition has

      Obviously timed out. Although this is violent, but not graceful enough.
      Set to indicate how many of the numbers in set a exactly are I.

      Then you can get the total time complexity of O (mlog2m) Violence:

At last the sum was finished.

Code
1 /**2 * Codeforces3 * problem#839d4 * Accepted5 * time:171ms6 * memory:15400k7  */8#include <bits/stdc++.h>9 using namespacestd;Ten  One Const intLim = 1e6 +1; A Const intModer = 1e9 +7; -  - intN; the int*A; - int*Pow2; - intCnt[lim], Counter[lim]; - intF[lim]; + intres =0; -  +Inlinevoidinit () { Ascanf"%d", &n); atA =New int[(n +1)]; -Pow2 =New int[(n +1)]; -pow2[0] =1; -      for(inti =1; I <= N; i++) { -scanf"%d", A +i); -counter[a[i]]++; inPow2[i] = (Pow2[i-1] <<1) %Moder; -     } to } +  -Inlinevoidsolve () { the      for(inti =1; I < Lim; i++) *          for(intj = i; J < Lim; J + =i) $Cnt[i] + =Counter[j];Panax Notoginseng      -      for(inti = Lim-1; i >1; i--) { the         if(!cnt[i])Continue;  +F[i] = (cnt[i] * 1LL * pow2[cnt[i]-1]) %Moder; A          for(intj = i <<1; J < Lim; J + =i) theF[i] = (F[i]-f[j])%Moder; +         if(F[i] <0) F[i] + =Moder; -Res = (res + (f[i] * 1LL * i)% moder)%Moder; $     } $      -printf"%d\n", res); - } the  - intMain () {Wuyi init (); the solve (); -     return 0; Wu}

Codeforces 839D Winter are here-violence-repulsion principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.