codeforces 839D(89/600)

D. Winter is here
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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

裸题莫比乌斯反演

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long maxn = 1000001;
typedef long long ll;
long long normal[maxn];
long long mu[maxn];
long long prime[maxn];
long long pcnt;
ll mo = 1e9 + 7;
void Init()
{
    memset(normal, 0, sizeof(normal));
    mu[1] = 1;
    pcnt = 0;
    for (long long i = 2; i<maxn; i++)
    {
        if (!normal[i])
        {
            prime[pcnt++] = i;
            mu[i] = -1;
        }
        for (long long j = 0; j<pcnt&&i*prime[j]<maxn; j++)
        {
            normal[i*prime[j]] = 1;
            if (i%prime[j]) mu[i*prime[j]] = -mu[i];
            else
            {
                mu[i*prime[j]] = 0;
                break;
            }
        }
    }
}
long long tu[1000001], xx[1000001], cc[1000001],q;
ll ksm(ll ds, ll zs)
{
    ll fs = 1;
    while (zs)
    {
        if (zs & 1)fs *= ds;
        fs %= mo;
        ds *= ds;
        ds %= mo;
        zs >>= 1;
    }
    return fs%mo;
}
int main()
{
    Init();
    ll n;
    cin >> n;
    for (long long a = 1; a <= n; a++)
    {
        scanf("%d", &q);
        tu[q]++;
    }
    for (long long a = 2; a<=1000000; a++)
    {
        for (long long b = 1; b*a <= 1000000; b++)
        {
            cc[a] += tu[b*a];
        }
    }
    for (long long a = 2; a <= 1000000; a++)
    {
        if (!cc[a])continue;
        cc[a] = cc[a] * ksm(2, cc[a] - 1);
        cc[a] %= mo;
//      cc[a] *= a;
    //  cc[a] %= mo;
    }
//  for (long long a = 1; a <= 10; a++)cout << cc[a] << endl;
//  return 0;
    ll he = 0;
    for (long long a = 2; a <= 1000000; a++)
    {
    //  if (!xx[a])continue;
        for (long long b = 1; b*a <= 1000000; b++)
        {
            xx[a] += mu[b] * cc[b*a];
        }
        he += xx[a]*a;
        he %= mo;
    }
    //for (long long a = 1; a <= 10; a++)cout << xx[a] << endl;
    cout << he;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值