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)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...
随机推荐
- C语言位域解析&符号位扩展规则
从一个例子说起: int main(void){ union{ int i; struct{ ; ; ; }bits; }num; printf("Input an integer for ...
- 【Golang 接口自动化07】struct转map的三种方式
背景 我们在前面介绍过怎么使用net/http发送json或者map数据,那么它能不能直接发送结构体数据呢?我们今天一起来学习结构体struct转map的三种方法,为后续做铺垫. struct转map ...
- SVN同步版本库与网站目录
如何创建SVN版本库及同步文件到WEB目录 来源:空谷 一 安装与配置SVN 1.安装subversion centos: yum install subversion ubuntu: apt-get ...
- FMUtils.KeyboardHook 轻量级键盘监听器
Nuget添加引用 Install-Package FMUtils.KeyboardHook 调用示例: var KeyboardHook = new Hook("Global Action ...
- LeetCode--107--二叉树的层次遍历II
问题描述: 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / ...
- (GoRails) Form对象设计风格: 用自建的Model来对参数进行操作。
视频:https://gorails.com/episodes/form-objects-design-pattern?autoplay=1 git代码 :https://github.com/gor ...
- 12月17日周日 form_for的部分理解。belongs_to的部分理解
1.lean guide:helper method query ,✅
- java.lang.UnsupportedClassVersionError: com/my/test/TestUser : Unsupported major.minor version 52.0
问题原因: 1.执行代码的jdk版本 低于 编译的jdk版本 2.项目用JDK1.8运行过,现在又在本地的eclipse等开发工具或者本地环境变量为低版本的jdk1.7或者jdk1.6下运行,ecli ...
- 『科学计算』通过代码理解SoftMax多分类
SoftMax实际上是Logistic的推广,当分类数为2的时候会退化为Logistic分类 其计算公式和损失函数如下, 梯度如下, 1{条件} 表示True为1,False为0,在下图中亦即对于每个 ...
- ASP.NET网页生命周期事件
网页事件 典型的使用方式 PreInit PreInit事件是网页生命周期中非常早起的一个事件,在PreInit事件触发之后,就会加载用户设置信息与网页主题.我们通常使用PreInit事件来执行下列处 ...