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 ...
 
随机推荐
- 【音乐App】—— Vue-music 项目学习笔记:播放器内置组件开发(二)
			
前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 播放模式切换 歌词滚动显示 ...
 - iOS 振动反馈
			
代码地址如下:http://www.demodashi.com/demo/12461.html 1. 常用场景 继 iPhone7/7P 实体 home 键出现后,home 键再也无法通过真实的物理按 ...
 - Linux安装indicator-china-weather
			
https://launchpad.net/indicator-china-weather sudo apt-get update sudo apt-get install python-appind ...
 - Win 8.1 安装后要做的优化
			
①无光驱恢复电脑或重置系统(无需插入介质,本来老提示插入介质) 1.准备工作:win8iso,解压到任意非系统盘,解压后的路径假如是:H:\OS\win8 2.接着以管理员身份启动命令提示符,输入 ...
 - .NET实现爬虫
			
前几天看到一个.NET Core写成的爬虫,有些莫名的小兴奋,之前一直用集搜客去爬拉勾网的招聘信息,这个傻瓜化工具相当于用HTML模板页去标记DOM节点,然后在浏览器窗口上模拟人的浏览行为同时跟踪节点 ...
 - Linux环境变量PS1配置
			
1. 说明: 在Shell下,我们能够拥有更加色慘斑斓的提示行信息.这能够通过改变bash的$PS1环境变量还设置,如以下就是提示行的一种: user@host$ root用户的提示是这种: user ...
 - android 怎样单独下载一个项目
			
起因,"网络"不太好."比方铁通的就是不如联通的" 每次运行一边repo sync,十分蛋疼,假设不做full build无需所有下载,着急看某个项目的修改但是 ...
 - Smart Battery Specification Revision 1.1
			
1.SBS Specifications 2.System Management Bus (SMBus) Specification
 - 利用JS最真实的模拟鼠标点击
			
为了破解永乐票务登录验证码问题 http://www.228.com.cn/auth/login?logout 当然,打码的过程自然依赖第三方平台,但问题是,哪怕平台给了你需要点击的(相对)坐标.你又 ...
 - xml.etree.ElementTree模块的封装
			
转载:https://www.cnblogs.com/hongten/p/hongten_python_xml_etree_elementtree.html 1 # -*- coding: utf-8 ...