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)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...
随机推荐
- Java数组的定义和使用
如果希望保存一组有相同类型的数据,可以使用数组. 数组的定义和内存分配 Java 中定义数组的语法有两种: type arrayName[]; type[] arrayName; type 为Java ...
- TortoiseXX 与TotalCommander (TC)的图标问题
TortoiseXX通过overlay图标标志文件或文件夹是否被修改等状态,非常有用. 可惜TotoalCommander上,这些图标都没有显示. 其实,只需要在TC的菜单 配置->显示-> ...
- 【异常】Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
原因:缺少aspect,AOP的maven坐标 导入maven坐标: <dependency> <groupId>org.springframework</groupId ...
- 20170405xlVBA快速录入
Dim Rng As Range Dim Arr As Variant Dim LastCell As Range Dim FindText As String Dim ItemCount As Lo ...
- 关于pthread_cond_wait()使用的理解
pthread_cond_wait()是linux多线程同步实现的一种方法,表示等待某一个线程共享变量满足了某种情况时 线程才能继续执行 pthread_cond_wait()之后的代码,如下面的示例 ...
- Confluence 6 为用户管理连接 Confluence 到 Crowd
你可以连接你的 Confluence 应用程序到 Atlassian Crowd 或 a Jira (5.3 及后续版本)来管理你的用户和用户组以及针对他们的授权. Atlassian Crowd 是 ...
- AI学习路径
- python-day42--单表查询
1. 简单查询select * from employee;select name,salary from employee; 2. where条件 1.比较运算符:> &l ...
- The requested URL /phpmyadmin was not found on this server.
这个报错,我弄了好久,第一次我以为我安装有问题,我就卸载重新安装了,但是在结果还是报这样子的错. 查找phpmyadmin的安装位置输入: sudo dpkg -L phpmyadmin 可以看到很多 ...
- hdu2510 爆搜+打表
符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...