P6298 齿轮

给$n$个数,求取$k$个数的$gcd=m$的方案数,求出所有$m$。

$g(x)=\sum [x|a[i]]$

$gcd=x$如果选择$C_{g(x)}^k$。会重复到$f(xk)$

倒过来枚举,调和级数$O(m\log m)$

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
ll qpow(ll a, ll x, ll mo)
{
ll res = 1;
while (x)
{
if (x & 1)
res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
x >>= 1;
}
return res;
}
int fac[N], facinv[N];
void prepare()
{
fac[0] = 1;
facinv[0] = 1;
for (int i = 1; i < N; i++)
fac[i] = 1ll * fac[i - 1] * i % mod;
facinv[N - 1] = qpow(fac[N - 1], mod - 2, mod);
for (int i = N - 2; i >= 1; i--)
facinv[i] = 1ll * facinv[i + 1] * (i + 1) % mod;
}
ll C(ll i, ll n)
{
if (n <= 0)
return 0;
if (i > n)
return 0;
if (i == 0)
return 1;
return 1ll * fac[n] * facinv[i] % mod * facinv[n - i] % mod;
}
int v[N], a[N];
ll f[N];
int n, m, k;
int main()
{
prepare();
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), v[a[i]]++;
for (int i = m; i >= 1; i--)
{
int num = 0;
for (int j = i; j <= m; j += i)
num += v[j];

int p = 0;
for (int j = i; j <= m; j += i)
p = (p + f[j]) % mod;
f[i] = (0ll + C(k, num) - p + mod + mod) % mod;
}
for (int i = 1; i <= m; i++)
printf("%lld ", f[i]);
}