题意:将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. SQL-基础学习4--聚集函数:AVG(),COUNT(),MAX(),MIN(),SUM();聚集不同值:DISTINCT

    第九课 9.1 聚集函数(对某些行运行的函数,计算并返回一个值) 我们经常需要汇总数据而不用把它们实际检索出来,为此SQL提供了专门的函数.使用这些函数,SQL查询可用于检索数据,以便分析和报表生成. ...

  2. mysql老司机之路

    MYSQL数据库基础: 数据库帮我们解决以下数据存取难题: 较大数据量 事务控制 持久化和数据安全 高性能要求 高并发访问 关系型:mysql,oracle,sql server,postgresql ...

  3. CentOS 挂载NTFS分区的两种方法

    第一种是安装内核模块,可到 http://sourceforge.net/projects/linux-ntfs/files/ 下载,需下载与你系统内核想对应的版本,使用uname -a 查看当前内核 ...

  4. mac 安装 mysql-python

    1 首先安装mysql 要那种纯净,独立的mysql,不是xampp里附带的 2 下载 mysql-python压缩包,解压,找到里面的site.cfg里的 mysql_config = /usr/l ...

  5. MyEclipse 中自定义日期格式

    从数据库中读出Data数据: 而想实现的是这样: 解决办法: 1. 在这个类里添加自定义的变量birthf: public abstract class AbstractUsers implement ...

  6. 转: scala语言的简单入门 (IBM develop)

    转: https://www.ibm.com/developerworks/cn/java/j-lo-funinscala2/

  7. RR调度(Round-robin scheduling)简单介绍

    在RR调度策略下,一个线程会一直运行.直到: 自愿放弃控制权 被更高优先级的线程抢占 时间片用完 例如以下图所看到的,A在用完自己的时间片后,将CPU运行权让给线程B.于是A离开Read队列,而B进入 ...

  8. 不让命令记录到history中

    先执行export HISTCONTROL=ignoresapce 然后再敲命令时在命令前面加一个空格

  9. C 共用体

    C 共用体 共用体是一种特殊的数据类型,允许您在相同的内存位置存储不同的数据类型.您可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值.共用体提供了一种使用相同的内存位置的有效方式. 定 ...

  10. vue2.X computed 计算属性

    需求:数据msg值为12345,我们现在需要反向显示成54321. 1.在模板中绑定表达式是非常便利的,但是它们实际上只用于简单的操作.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <! ...