前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数学--数论--HDU 4675 GCD of Sequence

数学--数论--HDU 4675 GCD of Sequence

作者头像
风骨散人Chiam
发布2020-11-06 00:28:13
2880
发布2020-11-06 00:28:13
举报
文章被收录于专栏:CSDN旧文

先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模

GCD of Sequence

代码语言:javascript
复制
 Alice is playing a game with Bob.
Alice shows N integers a 1, a 2, …, a N, and M, K. She says each integers 1 ≤ a i ≤ M.
And now Alice wants to ask for each d = 1 to M, how many different sequences b 1, b 2, …, b N. which satisfies :
1. For each i = 1…N, 1 ≤ b[i] ≤ M
2. gcd(b 1, b 2, …, b N) = d
3. There will be exactly K position i that ai != bi (1 ≤ i ≤ n)

Alice thinks that the answer will be too large. In order not to annoy Bob, she only wants to know the answer modulo 1000000007.Bob can not solve the problem. Now he asks you for HELP!
Notes: gcd(x 1, x 2, …, x n) is the greatest common divisor of x 1, x 2, …, x n 

Input

代码语言:javascript
复制
The input contains several test cases, terminated by EOF.
The first line of each test contains three integers N, M, K. (1 ≤ N, M ≤ 300000, 1 ≤ K ≤ N)
The second line contains N integers: a 1, a 2, …, a n (1 ≤ a i ≤ M) which is original sequence.

Output

代码语言:javascript
复制
For each test contains 1 lines :
The line contains M integer, the i-th integer is the answer shows above when d is the i-th number.

Sample Input

代码语言:javascript
复制
3 3 3
3 3 3
3 5 3
1 2 3
1
2
3
4

Sample Output

代码语言:javascript
复制
7 1 0
59 3 0 1 1
1
2

Hint

代码语言:javascript
复制
In the first test case :
when d = 1, {b} can be :
(1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 2, 2)
(2, 1, 1)
(2, 1, 2)
(2, 2, 1)
when d = 2, {b} can be :
(2, 2, 2)
And because {b} must have exactly K number(s) different from {a}, so {b} can't be (3, 3, 3), so Answer = 0
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

卢卡斯求组合数是log级别的所以没问题

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
const int maxn = 310000;
const int mod = 1000000007;
int n, m, k;
int prime[maxn], tot, mu[maxn]; //莫比乌斯函数
bool vis[maxn];
long long fac[maxn], rev[maxn]; //乘法逆元,和卢卡斯定理
long long F[maxn], f[maxn];     //莫比乌斯反演
int a[maxn];
int cnt[maxn]; //对于d,有多少a[i]是d的倍数
long long extend_gcd(long long a, long long b, long long &x, long long &y)
{
    //扩展欧几里得
    if (a == 0 && b == 0)
        return -1;
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extend_gcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}
long long mod_rev(long long a, long long n) //乘法逆元lucas用
{
    long long x, y;
    long long d = extend_gcd(a, n, x, y);
    if (d == 1)
        return (x % n + n) % n;
    else
        return -1;
}

void init() //线性筛求莫比乌斯函数
{
    tot = 0;
    mu[1] = 1;
    for (int i = 2; i < maxn; i++)
    {
        if (!vis[i])
        {
            prime[tot++] = i;
            mu[i] = -1;
        }
        for (int j = 0; j < tot; j++)
        {
            if (i * prime[j] >= maxn)
                break;
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)
            {
                mu[i * prime[j]] = 0;
                break;
            }
            else
            {
                mu[i * prime[j]] = -mu[i];
            }
        }
    }
    fac[0] = rev[0] = 1;
    for (int i = 1; i < maxn; i++)
    {
        fac[i] = fac[i - 1] * i % mod;
        //预处理卢卡斯定理参数
        rev[i] = mod_rev(fac[i], mod);
        //预处理逆元
    }
}

long long quick_mod(long long a, long long b)
{
    long long ans = 1;
    a %= mod;
    while (b)
    {
        if (b & 1)
        {
            ans = ans * a % mod;
            b--;
        }
        b >>= 1;
        a = a * a % mod;
    }
    return ans;
}

long long Lucas(long long m, long long n)
{
    if (n == 0)
        return 1;
    long long ans = fac[m] * rev[n] % mod * rev[m - n] % mod;
    return ans;
}

int main()
{
    init();
    while (scanf("%d%d%d", &n, &m, &k) != EOF)
    {
        memset(cnt, 0, sizeof cnt);
        memset(f, 0, sizeof f);
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            cnt[a[i]]++;
        }
        for (int i = 1; i <= m; i++)
            for (int j = i + i; j <= m; j += i)
                cnt[i] += cnt[j];

        for (int i = 1; i <= m; i++)
        {
            long long p = cnt[i];
            if (k - n + p < 0)
            {
                F[i] = 0;
                continue;
            }
            F[i] = Lucas(p, k - n + p) * quick_mod(m / i - 1, k - n + p) % mod * quick_mod(m / i, n - p) % mod;
        }

        for (int i = 1; i <= m; i++)
        {
            if (F[i] == 0)
                f[i] = 0;

            else
                for (int j = i; j <= m; j += i)
                {
                    f[i] += mu[j / i] * F[j];
                    f[i] %= mod;
                }
            printf("%lld", (f[i] + mod) % mod);
            if (i != m)
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/01/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档