CF(439E - Devu and Birthday Celebration)莫比乌斯容斥
题意:将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)莫比乌斯容斥的更多相关文章
- codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理
题意: q个询问,每一个询问给出2个数sum,n 1 <= q <= 10^5, 1 <= n <= sum <= 10^5 对于每一个询问,求满足下列条件的数组的方案数 ...
- [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]
题面 传送门 思路 新姿势get 莫比乌斯容斥 $\sum_{i=1}{n}\mu(i)f(i)$ 这个东西可以把所有没有平方质因子的东西表示出来,还能容斥掉重复的项 证明是根据莫比乌斯函数的定义,显 ...
- Codeforces 439E Devu and Birthday Celebration 容斥
Devu and Birthday Celebration 我们发现不合法的整除因子在 m 的因子里面, 然后枚举m的因子暴力容斥, 或者用莫比乌斯系数容斥. #include<bits/std ...
- 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], . ...
- CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)
Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...
- BZOJ2440(全然平方数)二分+莫比乌斯容斥
题意:全然平方数是指含有平方数因子的数.求第ki个非全然平方数. 解法:比較明显的二分,getsum(int middle)求1-middle有多少个非全然平方数,然后二分.求1-middle的非全然 ...
- hdu6053(莫比乌斯+容斥+分块)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意: 给出一个含 n 个元素的 a 数组, 求 bi <= ai 且 gcd(b1, ...
- 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 ...
- bzoj2440 [中山市选2011]完全平方数——莫比乌斯+容斥
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2440 莫比乌斯...被难倒... 看TJ:http://hzwer.com/4827.htm ...
随机推荐
- Rebound动画框架简单介绍
Rebound动画框架简单介绍 Android菜鸟一枚,有不对的地方希望大家指出,谢谢. 最近在接手了一个老项目,发现里面动画框架用的是facebook中的Rebound框架,由于以前没听说过,放假时 ...
- mysql赋给用户权限grant all privileges on
查看mysql用户表的结构,Field项都是各类权限限制 Host限制登录的IP,User限制登录的用户,Delete_priv限制删除权限,Grant_priv限制权限授予,Super_priv为超 ...
- selenium firefox46.0.1设置禁用图片
firefox_profile = webdriver.FirefoxProfile()firefox_profile.set_preference('permissions.default.ima ...
- 2017.2.28 activiti实战--第六章--任务表单(二)外置表单
学习资料:<Activiti实战> 第六章 任务表单(二)外置表单 6.3 外置表单 考虑到动态表单的缺点(见上节),外置表单使用的更多. 外置表单的特点: 页面的原样显示 字段值的自动填 ...
- js 数组去重复
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JAVA Eclipse的Android的进程和生命周期是什么
安卓程序的生命周期是不受自己控制的,安卓的程序根据不同的重要性做了一些区分,最重要的进程仅仅在安卓已经崩溃或者卡死的情况下才会终止前台进程. Activity就是表现层的界面,它有三种常见的状态, ...
- MySQL binlog-do-db选项是危险的[转]
很多人通过 binlog-do-db, binlog-ignore-db, replicate-do-db 和 replicate-ignore-db 来过滤复制(某些数据库), 尽管有些使用, ...
- bootstrap selectpicker使用问题
文档查阅:http://silviomoreto.github.io/bootstrap-select/options/ 1.实用属性 size:5 表示下拉列表默认展示5行(ie8展示4.5行) ...
- bzoj4010【HNOI2015】菜肴制作
4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB Submit: 981 Solved: 480 [Submit][Statu ...
- 笔记本中LVDS屏与eDP屏的比较
LVDS,即Low Voltage Differential Signaling,是一种低压差分信号技术接口.它是美国NS公司(美国国家半导体公司)为克服以TTL电平方式传输宽带高码率数据时功耗大.E ...