【Codeforces 839D. Winter is here】& 莫比乌斯反演
2017-08-15 11:08
351 查看
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代码 :
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 558c&558d&558e
- Educational Codeforces Round 17 C && codeforces 762C C. Two strings (二分 + 字符串前后缀)
- codeforces 397A On Segment's Own Points-yy
- {CodeForces】788E New task && 汕头市队赛SRM06 D 五色战队
- Codeforces Round #378 (Div. 2) B && codeforces 733B(暴力)
- CodeForces 235E Number Challenge (莫比乌斯反演)
- CodeForces - 368C Sereja and Algorithm (找规律&模拟)
- CodeForces 620 A. Professor GukiZ's Robot(水~)
- 【恶心的模拟 && 找时刻问题】CodeForces - 883A Automatic Door
- codeforces 15 C&D&E
- 【Codeforces 840 A. Leha and Function】& 构造
- CodeForces 333A Secrets 题解&代码
- codeforces 630 I(规律&&组合)
- CodeForces 235 E.Number Challenge(莫比乌斯反演+数论)
- 关于 codeforces中的 int abs(int x) 还有头文件 # include <algorithm> 不带.h
- Codeforces 383C Propagating tree 题解&代码
- 【CodeForces】223A - Bracket Sequence(栈 & 模拟)
- Codeforces 100187E:Two Labyrinths(搜索&&BFS)
- BZOJ-2154 && 2693 Crash的数字表格&&jzptab 莫比乌斯反演
- 51NOD 1491 黄金系统 && Codeforces 458 A. Golden System(斐波那契数列 + 找规律)