当前位置: 代码迷 >> 综合 >> Codeforces 439E Devu and Birthday Celebration (容斥定理+组合)
  详细解决方案

Codeforces 439E Devu and Birthday Celebration (容斥定理+组合)

热度:58   发布时间:2023-11-15 14:19:40.0

题目链接:http://codeforces.com/problemset/problem/439/E

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
const int  maxn =5e5+5;
const int mod=1e9+7;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
题目大意:将n块蛋糕分给m个人,
要求每个人分的的蛋糕大于1并且公因数为1.典型的容斥,最后套个隔板模型就行了。*/
///sieve mbious
int prim[maxn],tot=0,miu[maxn],vis[maxn];
vector<int> p[maxn];
void sieve()
{miu[1]=1;for(int i=2;i<maxn;i++){if(vis[i]==0) prim[tot++]=i,miu[i]=-1;for(int j=0;j<tot;j++){if(i*prim[j]>=maxn) break;int k=i*prim[j];vis[k]=1;if(i%prim[j]) miu[k]=-miu[i];else break;}}p[1].push_back(1);for(int i=2;i<maxn;i++){p[i].push_back(1);for(int j=i;j<maxn;j+=i) p[j].push_back(i);}
}
///
ll fac[maxn],inv[maxn];
void init()
{fac[0]=1;for(int i=1;i<maxn;i++) fac[i]=fac[i-1]*i%mod;inv[maxn-1]=powmod(fac[maxn-1],mod-2);for(int i=maxn-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
}
ll C(ll n,ll m)
{if(m>n) return 0LL;if(m==0) return 1LL;return fac[n-1]*inv[m-1]%mod*inv[n-m]%mod;
}
///data
ll n,m;
int main()
{sieve();init();int t;scanf("%d",&t);///cout<<inv[1]<<endl;while(t--){scanf("%lld%lld",&n,&m);ll ans=0;for(int i=0;i<p[n].size();i++){int v=p[n][i];/// cout<<n/i<<" "<<m<<" "<<C(n/i,m)<<endl;ans=(ans+miu[v]*C(n/v,m)+mod)%mod;}printf("%lld\n",ans);}return 0;
}

 

  相关解决方案