您的位置:首页 > 其它

Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd

2017-08-14 14:28 465 查看
传送门

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
题意:给定一个序列,要求求出该序列所有gcd不为1的子序列,并定义该子序列的值为序列元素个数*gcd(该序列的所有元素),求序列值的总和。

用一个a[]数组来记录各个数字出现的次数(具体可以看代码),t[i]表示2^i,

对于一个序列,碧如2 4 6 来说,可以组成的子集有2,4,6,24,26,46,246。__gcd==2

则他们对答案的贡献是gcd(==2)*(1+1+1+2+2+2+3)   仔细分析可得出,其实每个数字出现的次数也就是t[x-1].

x是以gcd==2的所有数的个数的总和。比如 246 x=3。最终得出结论x*t[x-1]。

2 4 6在其子序列中每个元素都出现了2^(3-1)次,一共有3种元素,所以是x*t[x-1],但是其中4,6,两个序列的最大公约数并不为2(为其本身),所以要减掉这两个。因为小的包括大的,所以应该从后往前推。

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e5+10;
const int maxx=1e6+100;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}

inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

LL a[maxx],t[maxx],f[maxx];
int n;
int main()
{
LL ans=0,x;
scan_d(n);
FOR(1,n,i)
{
scan_d(x);
a[x]++;
}
t[0]=1;
FOR(1,1000000,i)
{
t[i]=2*t[i-1]%mod;
}
for(int i=1000000;i>1;i--)
{
x=0;
for(int j=i;j<=1000000;j+=i)
x+=a[j];
if(x)
{
f[i]=x*t[x-1]%mod;
for(int j=i+i;j<=1000000;j+=i)
f[i]=(f[i]-f[j]+mod)%mod;
ans=(ans+f[i]*i%mod)%mod;
}
}
print(ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: