题意:将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. 最新Webstrom, Idea 2019.1.3 的激活

    1.注册码激活 打开网址(IntelliJ IDEA 注册码),我们能看到下面的界面,直接点击获取激活码,将生成的激活码粘贴到WebStorm激活对话框中的Lisence Code输入框,点击OK即可 ...

  2. word 插入空白偶数页(论文、报告等写作常用)

    目前在写大论文的过程中,需要在偶数页中插入一个空白页,但是又不想在下面加上页码,所以采用通常的方式并不妥,所以就用到了这篇博文中内容,采用奇偶页的方式插入空白偶数页,而且偶数页也不会在上面出现任何的字 ...

  3. ASIHTTPRequest, request sent twice

    ´ve just started using ASIHTTPRequest for iOs and I have a small issue with it. All requests are sen ...

  4. quick-coco2d下的事件

    qucik下的事件机制很简洁,也很简单,能够处理大多数的自定义时间,我们新建一个类型,然后用扩展的方式实现事件,之前quick的文档上很详细,但是很悲哀,傻逼的的网站现在是404,记下来,怕到时候忘记 ...

  5. django导出数据到excel

    import xlwt,StringIodef dumpData(request): #获取数据 activitys = Activity.objects.all().order_by('id') i ...

  6. appium 'WebDriver' object has no attribute 'keyevent'

    这个问题是我自己犯二了,开头应该是from appium import webdriver,写成了from selenium import webdriver,也可以运行,就是不能使用appium中独 ...

  7. opengl interface

    glTranslate()是移动坐标系,比如glTranslate(-1.5,0,0),之后你画的图就是在屏幕左边1.5个单位~glRotation()是做旋转的,第一个参量是angle,后面3个分别 ...

  8. 对数据进行GZIP压缩和解压

    public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...

  9. Vue2.0 引用 exif.js 实现调用摄像头进行拍照功能以及图片上传功能

    vue组件代码 <template> <div> <div style="padding:20px;"> <div class=" ...

  10. apue学习笔记(第十七章 高级进程间通信)

    本章介绍一种高级IPC---UNIX域套接字机制,并说明它的应用方法 UNIX域套接字 UNIX域套接字用于在同一台计算机上运行的进程(无关进程)之间的(全双工)通信.相比于因特网套接字,UNIX域套 ...