HDU 4675 GCD of Sequence

來源:互聯網
上載者:User

標籤:

題意:給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;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

HDU 4675 GCD of Sequence

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.