【Codeforces 839D. Winter is here】& 莫比乌斯反演
来源:互联网 发布:win10设置java环境变量 编辑:程序博客网 时间:2024/06/04 17:52
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
题意 : 可以挑出任意 n 个数,他们的贡献 = gcd(a1,a2….an) * n (注 : gcd 必须大于 1)
思路 : 单独计算每个gcd的贡献 ,例如 gcd 为 i ,i 的倍数有 n 个数的贡献
sum = 1*C(1,n)+2*C(2,n)+……+(n-1)*C(n-1,n)+n*C(n,n)
= n * 2 ^ (n - 1) , 莫比乌斯反演 去重
莫比乌斯反演是数论中的重要内容,在许多情况下能够简化运算
我们需要找到f(n)与F(n)之间的关系。从和函数定义当中,我们可以知道:
F(1)=f(1)
F(2)=f(1)+f(2)
F(3)=f(1)+ f(3)
F(4)=f(1)+f(2)+f(4)
F(5)=f(1)+f(5)
F(6)=f(1)+f(2)+f(3)+f(6)
F(7)=f(1)+f(7)
F(8)=f(1)+f(2)+f(4)+f(8)
那么:
f(1)=F(1)
f(2)=F(2)-f(1)=F(2)-F(1)
f(3) =F(3)-F(1)
f(4)=F(4) -f(2)- f(1) =F(4)-F(2)
f(5) =F(5)-F(1)
f(6)=F(6)-F(3)-F(2)+F(1)
f(7)=F(7)-F(1)
f(8)=F(8)-F(4)
从中,可以看出,若n=p2(p为质数)那么,F(p)=f(1)+f(p),F(n)=f(1)+f(p)+f(p2),所以,f(n)=F(p2)-F(p)
AC代码 :
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int MAX = 1e6 + 10;const int mod = 1e9 + 7;typedef long long LL;LL s[MAX];int n,x,a[MAX];LL ks(LL o,int b){ LL sum = 1; while(b){ if(b & 1) sum = sum * o % mod; b /= 2; o = o * o % mod; } return sum;}int main(){ scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&x),a[x]++; LL ans = 0; for(int i = MAX - 10; i >= 2; i--){ int o = 0; for(int j = i; j <= MAX - 10; j += i) o += a[j],s[i] -= s[j]; s[i] += (LL) o * ks(2, o - 1) % mod; ans = ((ans + (LL)i * s[i]) % mod + mod) % mod; } printf("%lld\n",ans); return 0;}
- Codeforces 839 D. Winter is here (莫比乌斯反演)
- 【Codeforces 839D. Winter is here】& 莫比乌斯反演
- codeforces839D Winter is here 数论,莫比乌斯反演
- codeforces 839 D Winter is here
- Codeforces 839D Winter is here
- codeforces 839d Winter is here
- Codeforces 839D-Winter is here
- CodeForces 839D Winter is here(数学 容斥)
- Codeforces 839D Winter is here【容斥原理+数学公式】
- Codeforces 839 D. Winter is here 容斥
- Codeforces-839D Winter is here(容斥原理)
- Winter is here codeforces 839d 容斥
- CodeForces 839D Winter is here (容斥原理)
- Codeforces Round #428 (Div. 2) D. Winter is here
- Codeforces-822D Winter is here(DP)
- Codeforces 839D Winter is here 组合数公式/容斥
- CodeForces Round 428 div2 839D Winter is here(数学)(详解)
- [容斥] Codeforces#428 (Div. 2) 839D. Winter is here
- unix 编辑命令大全(vi/vim)
- 面试复习—网络知识点-----UDP
- 前台页面调试技巧----页面打印console
- java匿名内部类 学习笔记
- Android 7.0 ActivityManagerService(3) 启动Activity的过程:二
- 【Codeforces 839D. Winter is here】& 莫比乌斯反演
- vm dhcp hostname
- C#中DataTable添加列和行的三种方法
- 网络流,增广路,Edmonds_Karp 以及 Dinic 算法(HDU 1532)
- K&R寄存器变量
- Qt 常用快捷键 + 引用第三方库
- zoj2277 The Gate to Freedom
- 理解端口与端口组
- 基于maven发送邮件(4)-----记录一下spring的timer注解方式task