HDU 4675 GCD of Sequence

题意:给N个数,要求改变其中K个数,是改变后的数列GCD为1~M,问对于各个GCD一共有多少组数

解:1、预处理C(n,m),这里用快速幂计算n!,m!,(n-m)!,quick(a,mod-1)=a

2、给定数列中统计i的倍数的个数为temp,如果n-temp>k,那么把k个数换掉,GCD依旧不为i,所以sum[i]=0;

若n-temp<=k,那么先替换掉不是i的倍数的数,一共有(m/i)^(n-temp)种组合,再换掉k-(n-temp)个i的倍数,有(m/i-1)^(k-(n-temp))*C(temp,k-(n-temp))种组合,所以

sum[i]=(m/i)^(n-temp)*(m/i-1)^(k-(n-temp))*C(temp,k-(n-temp));

3、这样算出来的sum值包含了sum[i],sum[2*i]......,所以要剪掉多余部分

sum[i]=sum[i]-sum[i*2]-sum[i*3]-......

所以要从第m项开始向前计算

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define ll __int64
#define MIN(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int maxn=300050;
const int mod=1000000007;
ll quick(ll a,ll b)                //快速幂
{
    ll temp=1;
    while(b)
    {
        if(b%2)
            temp=(temp*a)%mod;
        b/=2;
        a=(a*a)%mod;
    }
    return temp%mod;
}
int num[maxn];
ll sum[maxn];
ll b[maxn],e[maxn];
ll pac(ll n,ll m)              //求C(n,m)
{
    return (b[n]*e[m]%mod)*e[n-m]%mod;
}
int main()
{
    int n,m,k;
    int temp;
    b[0]=e[0]=1;
    for(int i=1;i<maxn;i++)
    {
        b[i]=(b[i-1]*i)%mod;
        e[i]=quick(b[i],mod-2);
    }
    while(scanf("%d%d%d",&n,&m,&k)!=-1)
    {
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&temp);
            num[temp]++;
        }
            //printf("%d\n",pac(2,3));
        for(int i=m;i>=1;i--)
        {
            temp=0;
            for(int j=i;j<=m;j+=i)
                temp+=num[j];
            if(n-temp>k)
            {
                sum[i]=0;
                continue;
            }
            sum[i]=(quick(m/i,n-temp)*quick(m/i-1,k-n+temp)%mod)*pac(temp,k-n+temp);
            //printf("%d..........%d\n",i,sum[i]);
            for(int j=2*i;j<=m;j+=i)
                sum[i]=(sum[i]-sum[j]+mod)%mod;
        }
        for(int i=1;i<m;i++)
            printf("%I64d ",sum[i]);         //注意空格问题
        printf("%I64d\n",sum[m]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 22:23:07

HDU 4675 GCD of Sequence的相关文章

HDU 4675 GCD of Sequence(莫比乌斯反演 + 打表注意事项)题解

题意: 给出\(M\)和\(a数组\),询问每一个\(d\in[1,M]\),有多少组数组满足:正好修改\(k\)个\(a\)数组里的数使得和原来不同,并且要\(\leq M\),并且\(gcd(a_1,a_2,\dots,a_n)=d\). 思路: 对于每一个\(d\),即求\(f(d)\):修改\(k\)个后\(gcd(a_1,a_2,\dots,a_n)=d\)的对数. 那么假设\(F(d)\):修改\(k\)个后\(gcd(a_1,a_2,\dots,a_n)\)是\(d\)倍数的对数.

hdu4675 GCD of Sequence 莫比乌斯+组合数学

/** 题目:hdu4675 GCD of Sequence 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4675 题意:给定n个数的a数组,以及m,k: 构造满足1<=bi<=m,和a数组恰好k个位置ai!=bi的b数组. 输出b数组所有数的gcd分别为1~m的数组个数. 思路: f(n)表示gcd==n的数组个数. g(n)表示gcd是n的倍数的数组个数. f(n) = sigma[n|d]mu[d/n]*g(d); 如何求g(d)呢? 如果没

HDU 5726 GCD 区间GCD=k的个数

GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 5063 Operation the Sequence(暴力)

HDU 5063 Operation the Sequence 题目链接 把操作存下来,由于只有50个操作,所以每次把操作逆回去运行一遍,就能求出在原来的数列中的位置,输出即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 100005; const ll MOD = 100000

hdu 4893 Wow! Such Sequence!(线段树)

题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 间l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

HDU Wow! 4893 Such Sequence!(线段树)

HDU 4893 Wow! Such Sequence! 题目链接 题意:给定一个序列,3种操作,单点添加值,查询区间和,把区间和变成最接近的婓波那契数 思路:线段树,就是第三个操作麻烦,就在结点添加一个值,标记它区间是不是都是婓波那契数了,然后修改区间的时候,如果区间是了就不用修改,如果不是就继续往后一层推即可 代码: #include <cstdio> #include <cstring> #include <cstdlib> #define lson(x) ((x

HDU 4893 Wow! Such Sequence! 水线段树

思路: 线段树走起.. 写完这题就退役T^T 单点更新的时候直接找到这个点的最近fib,然后维护当前和 和 fib的和 #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<algorithm> #include<queue> #include<map> #include<set> #include&l

HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000) 思路:因为x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质.要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,可以