CF(439E

來源:互聯網
上載者:User

題意:將n個糖果插入f-1個擋板分成f分(a1,a2,a3...af)。問有多少種分法可以使得gcd(a1,a2,a3...af)=1;


解法;莫比烏斯容斥,首先按1為單位分,這時候有C(n-1,f-1)種,然後去掉gcd不是1的。這時候就規定質因子個數是奇數的就減(mou值為-1),偶數的為加(mou值是+1),然後出現平方數為約數的數mou值為0。這樣就做到了容斥,很巧妙。容斥時,要注意只用計算是n的約數的數,因為如果不是n的約數,那麼gcd裡一定不會出現這個因子。


代碼:

/******************************************************* author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8#define zero(_) (abs(_)<=eps)const double pi=acos(-1.0);typedef long long LL;const int Max=100010;const int INF=1000000007;int mou[Max];LL fac[Max];map<pair<int,int>,LL> maps;int n,f;LL pow(LL a,int b){    LL ans=1;    while(b)    {        if(b&1)            ans=(ans*a)%INF;        a=(a*a)%INF;        b>>=1;    }    return ans;}LL getreverse(LL lo){    return pow(lo,INF-2);}void init(){    for(LL i=2; i<Max; i++)        if(!mou[i])        {            mou[i]=i;            for(LL j=i*i; j<Max; j+=i)                mou[j]=i;               // cout<<i<<" ";        }    mou[1]=1;    for(int i=2; i<Max; i++)    {        if((i/mou[i])%mou[i]==0) mou[i]=0;        else mou[i]=-mou[i/mou[i]];    }    fac[0]=1;    for(int i=1; i<Max; i++)        fac[i]=(fac[i-1]*i)%INF;}LL C(int a,int b){    LL ans=fac[a];    ans=(ans*getreverse(fac[a-b])%INF*getreverse(fac[b]))%INF;    return ans;}int main(){    int t;    cin>>t;    init();    while(t--)    {        scanf("%d%d",&n,&f);        if(maps.find(pair<int,int>(n,f))!=maps.end())        {            printf("%I64d\n",maps[pair<int,int>(n,f)]);            continue;        }        LL ans=0;        for(int i=1; i<=n/f; i++)        {            if((n%i)!=0)continue;            ans+=C(n/i-1,f-1)*mou[i];            if(ans>=INF)                ans-=INF;            if(ans<0)                ans+=INF;        }        printf("%I64d\n",ans);        maps[pair<int,int>(n,f)]=ans;    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.