题意:将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. 转:浅析Collections.sort

    浅析Collections.sort 问题引入   在之前的一次Java上机实习中,老师布置了一道很简单的题: 从控制台输入10个整数,对它们进行升序排序并输出.   考虑到只有10个数,需要比较的次 ...

  2. Java中的http相关的库:httpclient/httpcore/okhttp/http-request

    httpclient/httpcore是apache下面的项目:中文文档下载参考 5 官网:http://hc.apache.org/ 在线文档:http://hc.apache.org/httpco ...

  3. Importance sampling

    用蒙特卡洛求解积分时 (Monte Carlo 随机采样对目标积分函数做近似) importance sampling func p(x) p(x)值大的地方,Monte Carlo多采几次 值小的地 ...

  4. HDU 5402 Travelling Salesman Problem(多校9 模拟)

    题目链接:pid=5402">http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:给出一个n×m的矩阵,位置(i.j)有一个非负权值. ...

  5. SSO单点登录系列2:cas客户端和cas服务端交互原理动画图解,cas协议终极分析

    落雨 cas 单点登录 一.用户第一次访问web1应用. ps:上图少画了一条线,那一条线,应该再返回来一条,然后再到server端,画少了一步...谢谢提醒.而且,重定向肯定是从浏览器过去的.我写的 ...

  6. c#课程设计---猜猜看游戏

    1:游戏要求 1. 随机显示 一个名字 与 若干张相片(如3张).选择正确的相片. 2. 记录老师对每一个学生的认识概率P.并依据认识概率,确定"猜猜看"游戏中学生出现的频率. 认 ...

  7. Win7如何修复开机画面

    将下面文件保存为"修复Win7开机画面.bat"双击运行即可   bcdedit /set {current} locale zh-CN    

  8. vue install 注册组件

    1.myPlugin.js文件 let MyPlugin = {}; MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或属性 Vue. ...

  9. void 0 或者 undefined

    Problem 在检查一个值是否为undefined的时候.你们是假设去測试的? 要是之前的我会这样子測试 function isUndefined(obj){ return obj === unde ...

  10. Linux环境变量PS1配置

    1. 说明: 在Shell下,我们能够拥有更加色慘斑斓的提示行信息.这能够通过改变bash的$PS1环境变量还设置,如以下就是提示行的一种: user@host$ root用户的提示是这种: user ...