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.
Input
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.
OutputAfter 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.
Examples3 3 2
4
6
9
8
6
5
16
4 1 2
6
5
4
3
2
1
20
21
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=1000010; const int Mod=1e9+7; 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=1; while(x){ if(x&1) res=1LL*res*a%Mod; x>>=1; a=1LL*a*a%Mod; } return res; } void init() { fac[0]=rev[0]=1; for(int i=1;i<maxn;i++) fac[i]=1LL*fac[i-1]*i%Mod; rev[maxn-1]=qpow(fac[maxn-1],Mod-2); for(int i=maxn-2;i>=1;i--) rev[i]=1LL*rev[i+1]*(i+1)%Mod; phi[1]=1; for(int i=2;i<maxn;i++){ if(!vis[i]) p[++cnt]=i,phi[i]=i-1; for(int j=1;j<=cnt&&i*p[j]<maxn;j++){ vis[i*p[j]]=1; if(!(i%p[j])){phi[i*p[j]]=phi[i]*p[j]; break;} phi[i*p[j]]=phi[i]*(p[j]-1); } } } int C(int N,int M) { if(N<M) return 0; return 1LL*fac[N]*rev[M]%Mod*rev[N-M]%Mod; } void add(int x) { for(int i=1;i*i<=x;i++){ if(x%i==0){ ans=(ans+1LL*C(num[i],K-1)*phi[i]%Mod)%Mod; if(i*i!=x) ans=(ans+1LL*C(num[x/i],K-1)*phi[x/i]%Mod)%Mod; } } for(int i=1;i*i<=x;i++){ if(x%i==0){ 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,1,N) { scanf("%d",&x); add(x); } rep(i,1,Q){ scanf("%d",&x); add(x); printf("%d\n",ans); } return 0; }