日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CF(439E - Devu and Birthday Celebration)莫比乌斯容斥

發(fā)布時間:2025/7/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CF(439E - Devu and Birthday Celebration)莫比乌斯容斥 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意:將n個糖果插入f-1個擋板分成f分(a1,a2,a3...af)。

問有多少種分法能夠使得gcd(a1,a2,a3...af)=1;


解法。莫比烏斯容斥,首先按1為單位分,這時候有C(n-1,f-1)種,然后去掉gcd不是1的。這時候就規(guī)定質(zhì)因子個數(shù)是奇數(shù)的就減(mou值為-1),偶數(shù)的為加(mou值是+1),然后出現(xiàn)平方數(shù)為約數(shù)的數(shù)mou值為0。這樣就做到了容斥,非常巧妙。

容斥時,要注意僅僅用計算是n的約數(shù)的數(shù),由于假設(shè)不是n的約數(shù),那么gcd里一定不會出現(xiàn)這個因子。


代碼:

/****************************************************** * 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; }

轉(zhuǎn)載于:https://www.cnblogs.com/ldxsuanfa/p/10589070.html

總結(jié)

以上是生活随笔為你收集整理的CF(439E - Devu and Birthday Celebration)莫比乌斯容斥的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。