Codeforces-822D Winter is here(DP)
来源:互联网 发布:国动网络集团是国企吗 编辑:程序博客网 时间:2024/06/05 06:10
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 ifi1 < 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.
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.
Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).
33 3 1
12
42 3 4 6
39
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
#include<bits/stdc++.h>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define x first#define y secondusing namespace std;typedef long long LL;typedef pair<int, int> PII;const int MX = 5e6 + 5;const LL mod = 1e9 + 7;int a[MX], arr[MX];void init() { for (int i = 2; i < MX; i++) { if (!a[i]) { for (int j = i + i; j < MX; j += i) { a[j] = 1; if (!arr[j]) arr[j] = i; } } }}LL pow(LL a, LL b) { LL ret = 1; while (b) { if (b & 1) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret;}LL f[MX];int main() { LL t; int l, r; init(); //freopen("in.txt", "r", stdin); scanf("%I64d%d%d", &t, &l, &r); for (int i = 2; i <= r; i++) { if (!arr[i]) f[i] = ((LL)i * (i - 1) % mod) * pow(2ll, mod - 2) % mod; else f[i] = (f[i / arr[i]] + f[arr[i]] * i / arr[i]) % mod; } LL ans = 0, k = 1; for (int i = l; i <= r; i++) { ans = (ans + k * f[i]) % mod; k = k * t % mod; } printf("%I64d\n", ans); return 0;}
- Codeforces-822D Winter is here(DP)
- Codeforces-839D Winter is here(容斥原理)
- CodeForces 839D 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 Round #428 (Div. 2) D. Winter is here
- Codeforces 839D Winter is here【容斥原理+数学公式】
- Codeforces 839 D. Winter is here 容斥
- Winter is here codeforces 839d 容斥
- Codeforces 839 D. Winter is here (莫比乌斯反演)
- Codeforces Round #428 (Div. 2) D. Winter is here(容斥,补题)
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式) +容斥
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)
- HTML&CSS高级05-07(选择器)
- C语言库函数(strcpy、strncpy、memcpy、memset)
- JS 页面加载触发事件 document.ready和onload的区别
- Linux系统优化
- 最短路径问题 变异Dijkstra
- Codeforces-822D Winter is here(DP)
- 新手上路53日
- 欠拟合、过拟合及其解决方法
- IIS7多域名绑定同一物理目录,设置不同默认文档的解决方案
- Android开发监听和屏蔽多任务键完美解决方法
- 温故知新python3--基础知识3(函数高级特性)
- 解释器模式
- Serializable序列化接口的使用
- Mui按钮事件,阻止提交表单