839D(容斥 || 莫比乌斯反演)

本文介绍了一个算法问题,通过莫比乌斯反演计算军队的总体强度。士兵们的强度由其战斗力决定,而军队的整体强度则是所有可能组合的士兵群体(称为氏族)的强度之和。
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问题,肯定是要用到容斥或者莫比乌斯反演,这里我用的是莫比乌斯反演。

假设答案为ans.则根据题意可得:

ans = Σd * Σk * (gcd(ai1, ai2,,,,,aik) == d)

我们枚举d就行。

我们令f(n) = Σk * (gcd(ai1, ai2,,,,,aik) == n)

g(n) = Σf(d) (n|d) = num * 2^(num -1)(num为数组中能够整除n的数的个数)

然后我们可以得到:

f(n) = Σmu(d / n)g(d) (n|d)

然后代回原公式计算就行

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
const ll maxn = 1000000 + 10;
ll n;
ll ans;
ll times[maxn];//原数组中每个元素出现的次数
ll term[maxn];
ll g[maxn];
ll f[maxn];
ll prime[maxn];
ll mu[maxn];//莫比乌斯函数
bool valid[maxn];
ll quick_pow(ll x, ll i)
{
    ll result = 1;
    x %= mod;
    while(i)
    {
        if(i&1) result = (result * x) % mod;
        i >>= 1;
        x = (x * x) % mod;
    }
    return result;
}
void initMu()
{
    mu[1] = 1;
    memset(valid, true, sizeof(valid));
    ll cnt = 0;
    for(ll i = 2; i < maxn; i++)
    {
        if(valid[i])
        {
            prime[++cnt] = i;
            mu[i] = -1;
        }
        for(ll j = 1; j <= cnt && i * prime[j] < maxn; j++)
        {
            valid[i * prime[j]] = false;
            if(i % prime[j] == 0)
            {
                mu[i * prime[j]] = 0;
                break;
            }
            mu[i * prime[j]] = -mu[i];
        }
    }
}
void initCase()
{
    memset(times, 0, sizeof(times));
    memset(term, 0, sizeof(term));
    ans = 0;
}
void initG()
{

    for(ll i = 2; i < maxn; i++)
    {
        for(ll j = i; j < maxn; j += i)
        {
            times[i] += term[j];
        }
    }

    for(ll i = 2; i < maxn; i++)
    {
        ll res = times[i];
        if(res)
        g[i] = ((res % mod) * quick_pow(2, res - 1)) % mod;
        else g[i] = 0;
    }
}
void initF()
{
    for(ll i = 2; i < maxn; i++)
    {
        f[i] = 0;
        for(ll j = i; j < maxn; j += i)
        {
            f[i] = (f[i] + mu[j / i] * g[j]) % mod;
        }
    }
}
void initAns()
{
    for(ll i = 2; i < maxn; i++)
    {
        ans = (ans + i * f[i] % mod) % mod;
    }
}
int main()
{
    initMu();
    while(~scanf("%lld", &n))
    {
        ll value;
        initCase();
        for(ll i = 1; i <= n; i++)
        {
            scanf("%lld", &value);
            term[value]++;
        }
        initG();
        initF();
        initAns();
        printf("%I64d\n", ans % mod);
    }
    return 0;
}

 
### 关于莫比乌斯反演的模板代码 以下是基于线性筛法实现的莫比乌斯函数计算模板代码: ```cpp const int N = 1e6 + 5; int mob[N], vis[N], prime[N]; int tot; void Mobius(int n) { memset(prime, 0, sizeof(prime)); memset(mob, 0, sizeof(mob)); memset(vis, 0, sizeof(vis)); tot = 0; mob[1] = 1; // 初始化 mu(1) = 1 for (int i = 2; i <= n; ++i) { if (!vis[i]) { prime[tot++] = i; // 记录素数 mob[i] = -1; // 如果是质数,则 mu(p) = -1 } for (int j = 0; j < tot && i * prime[j] <= n; ++j) { vis[i * prime[j]] = 1; // 标记合数 if (i % prime[j] == 0) { mob[i * prime[j]] = 0; // 若 p 整除 i,则 mu(ip) = 0 break; } else { mob[i * prime[j]] = -mob[i]; // 否则 mu(ip) = -mu(i) } } } } ``` 上述代码实现了通过线性筛法快速预处理莫比乌斯函数值的功能[^1]。 --- ### 莫比乌斯反演的学习资料推荐 #### 定义与基本形式 莫比乌斯反演的核心在于两个互逆的关系式: - $ f(n) = \sum_{d|n} g(d) $ - $ g(n) = \sum_{d|n} \mu(d) f\left(\frac{n}{d}\right) $ 其中 $\mu$ 是莫比乌斯函数,定义如下: $$ \mu(n) = \begin{cases} 1 & \text{如果 } n=1 \\ (-1)^k & \text{如果 } n \text{ 是无平方因子数且有 } k \text{ 个不同质因数} \\ 0 & \text{其他情况} \end{cases} $$ 这些关系可以用于解决涉及整除和约数的问题[^3]。 #### 应用场景举例 1. **计数问题** 给定一个正整数 $n$,求满足某些条件的数的数量。例如,求长度为 $n$ 的字符串且其循环节也为 $n$ 的字符串数量可以通过莫比乌斯反演来优化复杂度[^2]。 2. **加权约数和** 对于给定范围内的所有数,计算某种加权约数和的形式化表达式也可以借助莫比乌斯反演简化计算过程[^4]。 3. **组合数学中的应用** 利用狄利克雷卷积以及容斥原理推导复杂的组合公式时,莫比乌斯反演提供了一种有效的工具[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值