题意:将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;
}

CF(439E - Devu and Birthday Celebration)莫比乌斯容斥的更多相关文章

  1. codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理

    题意: q个询问,每一个询问给出2个数sum,n 1 <= q <= 10^5, 1 <= n <= sum <= 10^5 对于每一个询问,求满足下列条件的数组的方案数 ...

  2. [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]

    题面 传送门 思路 新姿势get 莫比乌斯容斥 $\sum_{i=1}{n}\mu(i)f(i)$ 这个东西可以把所有没有平方质因子的东西表示出来,还能容斥掉重复的项 证明是根据莫比乌斯函数的定义,显 ...

  3. Codeforces 439E Devu and Birthday Celebration 容斥

    Devu and Birthday Celebration 我们发现不合法的整除因子在 m 的因子里面, 然后枚举m的因子暴力容斥, 或者用莫比乌斯系数容斥. #include<bits/std ...

  4. HihoCoder - 1867: GCD (莫比乌斯容斥)

    Sample Input 6 1 6 2 5 3 4 Sample Output 10 You are given a {1, 2, ..., n}-permutation a[1], a[2], . ...

  5. CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)

    Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...

  6. BZOJ2440(全然平方数)二分+莫比乌斯容斥

    题意:全然平方数是指含有平方数因子的数.求第ki个非全然平方数. 解法:比較明显的二分,getsum(int middle)求1-middle有多少个非全然平方数,然后二分.求1-middle的非全然 ...

  7. hdu6053(莫比乌斯+容斥+分块)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意: 给出一个含 n 个元素的 a 数组, 求 bi <= ai 且 gcd(b1, ...

  8. CodeForces - 1097F:Alex and a TV Show (bitset & 莫比乌斯容斥)

    Alex decided to try his luck in TV shows. He once went to the quiz named "What's That Word?!&qu ...

  9. bzoj2440 [中山市选2011]完全平方数——莫比乌斯+容斥

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2440 莫比乌斯...被难倒... 看TJ:http://hzwer.com/4827.htm ...

随机推荐

  1. lock与monitor的区别

    1.Lock 只能对引用对象加锁 Lock锁定区间内可以对锁定值修改而不发生运行时错误,通常也会采用此种修改方式.这种方式又有点类同于使用Monitor.Wait取得资源,并对这个资源进行操作. 用法 ...

  2. ios6.0 siri语音识别

    Siri 让你能够利用语音来完成发送信息.安排会议.拨打电话等更多事务* .只需像平常一样讲话,就能让 Siri 帮你做事.你会发现 Siri 是如此易用而且能干,精彩用法越用你会发现越多. 懂你所说 ...

  3. xss---攻击

    xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/修改/删除数据的目的,而在xss攻击中,通过插入恶意 ...

  4. redis参数配置

    redis.conf配置文件 配置项 值 说明 slave-read-only yes slave是否只读 slave-serve-stale-data yes 当slave与master断开连接,s ...

  5. 2016.6.20 maven更改repository的位置

    默认位置为${userhome}/.m2/repository: 修改位置: 在setting,xml中更改 这个时候再看eclipse的设置,已经自动更改了.因为它是读取setting.xml中的数 ...

  6. 更新到mysql 5.7后解决0000-00-00日期问题

    更新到mysql 5.7后解决0000-00-00日期问题 学习了:http://www.07net01.com/2016/04/1479450.html mysql 5.7 默认开始用以下sql m ...

  7. 上传中文文件到linux文件出现乱码问题的解决方案

    convm -f gbk -t utf8 -r --notest /ftp的目录 效果:

  8. HM编码器代码阅读(14)——帧间预測之AMVP模式(二)predInterSearch函数

    简单介绍     predInterSearch基本的工作是ME(运动预计)和MC(运动补偿).     函数中有一个bTestNormalMC变量.它表示是否进行正常的MC过程,正常的MC过程就是进 ...

  9. highcharts 绘制图标的JAVASCRIPT 类库 收藏

    官方站点 : http://www.highcharts.com 演示样例网址 : http://www.highcharts.com

  10. Nexus 5刷阿里云OS

    刷机有风险,刷机需慎重! 1. 下载 recovery 的img和 阿里云os. recovery 的img下载: https://kanbox.com/f/V00KA 阿里云OS3.0.3 : 2月 ...