Codeforces 839D Winter is here【数学:容斥原理】

时间:2022-09-05 07:31:05

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

题目链接:http://codeforces.com/contest/839/problem/D

Codeforces 839D Winter is here【数学:容斥原理】

下面给出AC代码:

 #include<cstdio>
const int N=,P=;
int n,i,j,x,ans,po[N],a[N],f[N];
int main(){
scanf("%d",&n);
for(po[]=i=;i<=n;i++)po[i]=*po[i-]%P;
while(n--)scanf("%d",&x),a[x]++;
for(i=N-;i>;i--){
for(j=i,x=;j<N;j+=i)x+=a[j];
if(x){
f[i]=1LL*x*po[x-]%P;
for(j=i+i;j<N;j+=i)f[i]=(f[i]-f[j]+P)%P;
ans=(1LL*f[i]*i+ans)%P;
}
}
printf("%d",ans);
}

Codeforces 839D Winter is here【数学:容斥原理】的更多相关文章

  1. Codeforces 839D Winter is here - 暴力 - 容斥原理

    Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n s ...

  2. Codeforces 839D Winter is here(容斥原理)

    [题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累 ...

  3. CodeForces 839D - Winter is here &vert; Codeforces Round &num;428 &lpar;Div&period; 2&rpar;

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

  4. Codeforces 839D Winter is here

    链接:CF839D 题目大意 给定一个数组大小为\(n(1\leq n\leq 200000)\)的数组\(a\),满足\(1\leq a_i \leq 1000000\). 选择其中任意\(len\ ...

  5. hdu4135-Co-prime &amp&semi; Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  6. &lbrack;Codeforces 1178D&rsqb;Prime Graph &lpar;思维&plus;数学&rpar;

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  7. Codeforces 627 A&period; XOR Equation &lpar;数学&rpar;

    题目链接:http://codeforces.com/problemset/problem/627/A 题意: 告诉你s 和 x,a + b = s    a xor b = x   a, b &gt ...

  8. Codeforces Beta Round &num;2B&lpar;dp&plus;数学&rpar;

    贡献了一列WA.. 数学很神奇啊 这个题的关键是怎么才能算尾0的个数 只能相乘 可以想一下所有一位数相乘 除0之外,只有2和5相乘才能得到0 当然那些本身带0的多位数 里面肯定含有多少尾0 就含有多少 ...

  9. codeforces 803C Maximal GCD&lpar;GCD数学&rpar;

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

随机推荐

  1. 完整的PHP MYSQL数据库类

    <?php class mysql {     private $db_host; //数据库主机     private $db_user; //数据库用户名     private $db_ ...

  2. &lbrack;推荐&rsqb;Zookeeper大型分布式系统的可靠协调系统知识介绍

    [推荐]Zookeeper大型分布式系统的可靠协调系统知识介绍 基于Zookeeper的锁开发手册 http://wenku.baidu.com/view/acbb8fc6102de2bd960588 ...

  3. 第4章 分治策略 monge阵列

    /* fi表示第i行的最左最小元素的列小标,则有f0<f1<f2<...<fn-1 取数组的偶数行,组成新的子数组,递归求解最左最小元素的列下表,利用偶数项限定奇数项的范围,再 ...

  4. 算子:sample&lpar;false&comma; 0&period;1&rpar;抽样数据

    抽样示例操作: scala> import org.apache.spark.sql.hive.HiveContext import org.apache.spark.sql.hive.Hive ...

  5. 9&period;27 h5日记

    9.27 1.怎样给title前加小图标? <link rel="short icon"  href="favicon.ico"/> ❤link有哪 ...

  6. Oracle&lowbar;PL&sol;SQL&lpar;5&rpar; 包

    包1.定义:包用于逻辑组合相关的PL/SQL类型,项和子程序,由包规范和包体组成 建立包规范:包规范是包与应用程序之间的接口,用于定义包的公用组件, 包括常量,变量,游标,过程,函数等 建立包体:用于 ...

  7. Nodejs学习资源汇总

    Node.js v6.3.1 Documentation https://nodejs.org/dist/latest-v6.x/docs/api/​ npm官网  https://www.npmjs ...

  8. 01&period;如何把&period;py文件打包成为exe,重点讲解pyinstaller的用法

    1.应用场景 1.1 故事背景 我自己用python写了一个小程序发给其他同事用,给他的就是一个.py文件,不过他觉得比较麻烦,还要安装环境,他问我有没有简单一点的方式,我给一个exe文件,他就不用安 ...

  9. 基于Landmark的人脸对齐以及裁剪方法

    利用Landmarks进行人脸对齐裁剪是人脸检测中重要的一个步骤.效果如下图所示: 基本思路为: a.人脸检测 人脸的检测不必多说了,基本Cascade的方式已经很不错了,或者用基于HOG/FHOG的 ...

  10. 对EJB的认识

    对EJB的认识 接触EJB以来有一段时间了,走马观花一样把它所涉及到的东西看了一遍,随着深入了解越来越感觉到ejb的很强大,用了java后觉的java好用.学历SSH觉的比java好用.学了ejb觉的 ...