CodeForces - 645F:Cowslip Collections (组合数&&欧拉函数)
In an attempt to make peace with the Mischievious Mess Makers, Bessie and Farmer John are planning to plant some flower gardens to complement the lush, grassy fields of Bovinia. As any good horticulturist knows, each garden they plant must have the exact same arrangement of flowers. Initially, Farmer John has n different species of flowers he can plant, with ai flowers of the i-th species.
On each of the next q days, Farmer John will receive a batch of flowers of a new species. On day j, he will receive cj flowers of the same species, but of a different species from those Farmer John already has.
Farmer John, knowing the right balance between extravagance and minimalism, wants exactly k species of flowers to be used. Furthermore, to reduce waste, each flower of the k species Farmer John chooses must be planted in some garden. And each of the gardens must be identical; that is to say that each of the k chosen species should have an equal number of flowers in each garden. As Farmer John is a proponent of national equality, he would like to create the greatest number of gardens possible.
After receiving flowers on each of these q days, Farmer John would like to know the sum, over all possible choices of k species, of the maximum number of gardens he could create. Since this could be a large number, you should output your result modulo 109 + 7.
The first line of the input contains three integers n, k and q (1 ≤ k ≤ n ≤ 100 000, 1 ≤ q ≤ 100 000).
The i-th (1 ≤ i ≤ n) of the next n lines of the input contains an integer ai (1 ≤ ai ≤ 1 000 000), the number of flowers of species i Farmer John has initially.
The j-th (1 ≤ j ≤ q) of the next q lines of the input contains an integer cj (1 ≤ cj ≤ 1 000 000), the number of flowers of a new species Farmer John receives on day j.
Output
After each of the q days, output the sum of the maximum possible number of gardens, where the sum is taken over all possible choices of k species, modulo 109 + 7.
Examples
3 3 2
4
6
9
8
6
5
16
4 1 2
6
5
4
3
2
1
20
21
Note
In the first sample case, after the first day Farmer John has (4, 6, 9, 8) of each type of flower, and k = 3.
Choosing (4, 6, 8) lets him make 2 gardens, each with (2, 3, 4) of each flower, respectively. Choosing (4, 6, 9), (4, 9, 8) and (6, 9, 8) each only let him make one garden, since there is no number of gardens that each species can be evenly split into. So the sum over all choices of k = 3 flowers is 2 + 1 + 1 + 1 = 5.
After the second day, Farmer John has (4, 6, 9, 8, 6) of each flower. The sum over all choices is 1 + 2 + 2 + 1 + 1 + 2 + 2 + 3 + 1 + 1 = 16.
In the second sample case, k = 1. With x flowers Farmer John can make x gardens. So the answers to the queries are 6 + 5 + 4 + 3 + 2 = 20 and 6 + 5 + 4 + 3 + 2 + 1 = 21.
题意:给出N个初始的数,M个新加的数,以及K,求出每次新加一个数后,所有K元组的gcd之和。
思路:每次新加一个数x,我们在之前的答案基础上只考虑新加的这个数x和之前的K-1组合起来的gcd。其gcd肯定的x的因子,我们枚举x的因子f,统计之前的出现的含有这个因子f的个数,那么其贡献为C(num[f],K-1)*f。但是这样会出现重复,我们需要去重,这里可以考虑莫比乌斯,最后求出来每个f的系数应该是phi[f]。
莫比乌斯求的过程:我们设d的系数为g[d],那么g[d]=i-Σg[i](i是d且小于d的因子) ,可以线性筛出来,发现就是欧拉函数。
( 曲同工的题:求两两gcd之和 https://www.cnblogs.com/hua-dong/p/9905846.html
求互质对个数:https://www.cnblogs.com/hua-dong/p/9141249.html
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=1e9+;
int vis[maxn],p[maxn],phi[maxn],num[maxn],cnt,ans,K;
int fac[maxn],rev[maxn];
int qpow(int a,int x){
int res=; while(x){
if(x&) res=1LL*res*a%Mod;
x>>=; a=1LL*a*a%Mod;
} return res;
}
void init()
{
fac[]=rev[]=;
for(int i=;i<maxn;i++) fac[i]=1LL*fac[i-]*i%Mod;
rev[maxn-]=qpow(fac[maxn-],Mod-);
for(int i=maxn-;i>=;i--) rev[i]=1LL*rev[i+]*(i+)%Mod;
phi[]=;
for(int i=;i<maxn;i++){
if(!vis[i]) p[++cnt]=i,phi[i]=i-;
for(int j=;j<=cnt&&i*p[j]<maxn;j++){
vis[i*p[j]]=;
if(!(i%p[j])){phi[i*p[j]]=phi[i]*p[j]; break;}
phi[i*p[j]]=phi[i]*(p[j]-);
}
}
}
int C(int N,int M)
{
if(N<M) return ;
return 1LL*fac[N]*rev[M]%Mod*rev[N-M]%Mod;
}
void add(int x)
{
for(int i=;i*i<=x;i++){
if(x%i==){
ans=(ans+1LL*C(num[i],K-)*phi[i]%Mod)%Mod;
if(i*i!=x) ans=(ans+1LL*C(num[x/i],K-)*phi[x/i]%Mod)%Mod;
}
}
for(int i=;i*i<=x;i++){
if(x%i==){
num[i]++;
if(i*i!=x) num[x/i]++;
}
}
}
int main()
{
int N,Q,x;
init();
scanf("%d%d%d",&N,&K,&Q);
rep(i,,N) {
scanf("%d",&x);
add(x);
}
rep(i,,Q){
scanf("%d",&x);
add(x);
printf("%d\n",ans);
}
return ;
}
CodeForces - 645F:Cowslip Collections (组合数&&欧拉函数)的更多相关文章
- Codeforces 906D Power Tower(欧拉函数 + 欧拉公式)
题目链接 Power Tower 题意 给定一个序列,每次给定$l, r$ 求$w_{l}^{w_{l+1}^{w_{l+2}^{...^{w_{r}}}}}$ 对m取模的值 根据这个公式 每次 ...
- Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]
Codeforces 洛谷:咕咕咕 CF少有的大数据结构题. 思路 考虑一些欧拉函数的性质: \[ \varphi(p)=p-1\\ \varphi(p^k)=p^{k-1}\times (p-1)= ...
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- Codeforces 871D Paths (欧拉函数 + 结论)
题目链接 Round #440 Div 1 Problem D 题意 把每个数看成一个点,如果$gcd(x, y) \neq 1$,则在$x$和$y$之间连一条长度为$1$的无向边. ...
- Codeforces 1114F(欧拉函数、线段树)
AC通道 要点 欧拉函数对于素数有一些性质,考虑将输入数据唯一分解后进行素数下的处理. 对于素数\(p\)有:\(\phi(p^k)=p^{k-1}*(p-1)=p^k*\frac{p-1}{p}\) ...
- Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)
题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...
- Codeforces 776E: The Holmes Children (数论 欧拉函数)
题目链接 先看题目中给的函数f(n)和g(n) 对于f(n),若自然数对(x,y)满足 x+y=n,且gcd(x,y)=1,则这样的数对对数为f(n) 证明f(n)=phi(n) 设有命题 对任意自然 ...
- codeforces 1009D Relatively Prime Graph【欧拉函数】
题目:戳这里 题意:要求构成有n个点,m条边的无向图,满足每条边上的两点互质. 解题思路: 显然1~n这n个点能构成边的条数,就是2~n欧拉函数之和(x的欧拉函数值代表小于x且与x互质的数的个数. 因 ...
- HDU 4483 Lattice triangle(欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4483 题意:给出一个(n+1)*(n+1)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...
随机推荐
- 《剑指offer》第三十二题(分行从上到下打印二叉树)
// 面试题32(二):分行从上到下打印二叉树 // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层 // 打印到一行. #include <cstdio> #in ...
- mount: unknown filesystem type 'LVM2_member'解决方案【转】
一台服务器,普通/dev/sda1/2(硬盘一) 同步数据到 lvm_member(硬盘二) rsync两硬盘数据同步: From: http://hi.baidu.com/williwill/ite ...
- Linux下搜索文件
使用linux系统难免会忘记文件所在的位置,可以使用以下命令对系统中的文件进行搜索.搜索文件的命令为"find":"locate":"whereis& ...
- memcached 内存初始化与key-value存储
本次笔记未涉及到slab的动态重新平衡分配 /**首先介绍一下一个跟内存相关的非常重要的概念,内存块类型数据结构:*/ typedef struct { unsigned int size; /* c ...
- 微信小程序------MD5加密(支持中文和不支持中文)和网络请求(get和post)
开发中常常遇到MD5加密,最近做小程序也用到了,简单总结了一下: 这要有两个加密文件,一个不支持中文,一个支持,所以你选择支持的来用就行了: 也随便说说小程序的get和post网络请求. 来看看效果图 ...
- Jon Snow and his Favourite Number CodeForces - 768C (技巧)
链接 题意 给定数组, 每次操作先将数组排序, 再将奇数位全部异或x, 求k次操作后数组最大值与最小值 (1 ≤ n ≤ 105, 0 ≤ k ≤ 105, 0 ≤ x ≤ 103) 题解 直接暴力模 ...
- 高精度减法用string 和 stack
#include "bits/stdc++.h" using namespace std; int main() { string a,b; while(cin >> ...
- OAF日志使用总结
本文的完成感谢葛严大神授权使用LogUtil类,其次感谢Tavor大神的EBS OAF开发日志(见: EBS OAF开发中日志(Logging) ). 日志的使用是一门极大的学问,若读者有兴趣,可以自 ...
- hibernate中删除表遇到主键被外键引用违反完整约束条件不能删除的问题
MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. SET FOREIGN_KEY_CHECKS ...
- mysql判断表记录是否存在,不存在则插入新纪录
开始以为和SQL Server一样,使用not exists进行判断,结果不行: ) INSERT INTO vrv_paw_template(templateName,templateFileNam ...