Description

在 \([a,b]\) 之间选择 \(n\) 个数 (可以重复) ,使这 \(n\) 个数的最小公倍数能被 \(x\) 整除,对 \(10^9+7\) 取膜.

\(1\leqslant n \leqslant 100,1\leqslant a,b,x \leqslant 10^9\)

Sol

容斥原理+状态压缩.

这是容斥原理非常好的一道题啊QAQ.

转化成补集,有多少不能被 \(x\) 整除的序列.

首先质因数分解 \(x\) 因为最小的 \(9\) 个质数的积已经超过 \(10^9\) 所以 \(x\) 的质因子个数不会超过 \(9\)

那么 \(x=p_1^{k_1}p_2^{k_2}p_3^{k_3}...p_m^{k_m}\)

另集合 \(S=\{p_1^{k_1},p_2^{k_2},p_3^{k_3},...,p_m^{k_m}\}\)

\(f[S']\) 表示 \([a,b]\) 中不能被集合 \(S'\) 的任意一个元素整除的个数.

这个可以通过枚举子集和容斥原理来实现.

那么答案还需要一下容斥就是 \(ans=\prod (-1)^{|S'|}\begin{pmatrix}f[S']+n-1\\ n\end{pmatrix}\)

组合数可以最后乘上 \(n\) 的逆元.

枚举集合子集的复杂度是 \(O(2^{|S|})\)

枚举子集的子集复杂度是 \(O(3^{|S|})\) ,每个元素分为 \(3\) 种情况:不在子集中,在子集中不在子集的子集中,在子集的子集中.

所以总复杂度 \(O(m3^m+n2^m)\) .

Code

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std; #define debug(a) cout<<#a<<"="<<a<<" "
typedef long long LL;
const int N = 1<<11;
const int M = 15;
const int W = 100005;
const LL p = 1000000007; LL n,a,b,x,ans;
LL f[N];
LL pr[W],isp[W],s[M],c,cnt;
int pow2[M],lim; void out(int S){ for(int i=0;i<c;i++) if(S & pow2[i]) putchar('1');else putchar('0');putchar('\n'); }
LL gcd(LL a,LL b){ return a>b?(!b?a:gcd(a%b,b)):(!a?b:gcd(b%a,a)); }
LL Pow(LL a,LL b,LL res=1){ for(;b;b>>=1,a=a*a%p) if(b&1) res=res*a%p;return res; }
inline int count(int S,int res=0){ for(int i=0;i<c;i++) if(S & pow2[i]) res++;return res; }
void Pre(LL N){
for(int i=2;i<=N;i++){
if(!isp[i]) pr[++cnt]=i;
for(int j=1;j<=cnt && (LL)pr[j]*i<=N;j++){
isp[i*pr[j]]=1;
if(i%pr[j]==0) break;
}
}
}
void work(LL x){
for(int i=1;i<=cnt;i++) if(x%pr[i]==0){
s[c++]=pr[i],x/=pr[i];
while(x%pr[i]==0) s[c-1]*=pr[i],x/=pr[i];
}if(x>1) s[c++]=x;
}
LL calc(int S){
LL res=1;
for(int i=0;i<c;i++) if(S & pow2[i]) res=res/gcd(res,s[i])*s[i];
res=b/res-a/res;
return res;
}
LL Sum(LL m){
LL res=1;
for(int i=1;i<=n;i++) res=res*(m-i+1)%p;
return res;
}
int main(){
// freopen("in.in","r",stdin);
cin>>n>>a>>b>>x;a--;
Pre(sqrt(x)+1);work(x);
pow2[0]=1;for(int i=1;i<M;i++) pow2[i]=pow2[i-1]<<1;
lim=1<<c; // for(int i=1;i<=cnt;i++) cout<<pr[i]<<" ";cout<<endl;
// debug(c)<<endl;
// for(int i=0;i<c;i++) cout<<s[i]<<" ";cout<<endl;
// debug(gcd(2,3)),debug(gcd(2,6)),debug(gcd(3,6)),debug(gcd(6,6))<<endl; for(int S=0;S<lim;S++){
f[S]=b-a;
if(count(S)&1) f[S]=f[S]-calc(S);
else if(count(S)) f[S]=f[S]+calc(S);
for(int T=S&(S-1);T;T=(T-1)&S)
if(count(T)&1) f[S]=f[S]-calc(T);
else f[S]=f[S]+calc(T);
}
// cout<<lim<<endl;
// for(int i=0;i<lim;i++) cout<<f[i]<<" ";cout<<endl; for(int S=0;S<lim;S++){
if(count(S)&1) ans=(ans-Sum(f[S]+n-1)+p)%p;
else ans=(ans+Sum(f[S]+n-1))%p;
}
LL tmp=1;
for(int i=2;i<=n;i++) tmp=tmp*(LL)i%p;
cout<<ans*Pow(tmp,p-2)%p<<endl;
return 0;
}

  

Codeforces Gym 101138 G. LCM-er的更多相关文章

  1. Codeforces Gym 100513G G. FacePalm Accounting

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  2. Codeforces Gym 100637G G. #TheDress 暴力

    G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...

  3. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  4. Codeforces Gym 100203G G - Good elements 标记暴力

    G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  5. Codeforces Gym 100203G G - Good elements 暴力

    G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  6. Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)

    题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, …, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...

  7. Codeforces gym 101061 G【递推公式+逆元】

    题意: 就是n复制m次,然后数mod1e9+7; 思路: 案例:31*10^6 + 31*10^4 + 31*10^2 + 31*10^0 所以就是一个等比数列,然后整理一下就是n*(10^(m*le ...

  8. Codeforces Gym 101138 D. Strange Queries

    Description 给你一下长度为 \(n\) 的序列. \(a_i=a_j\) \(l_1 \leqslant i \leqslant r_1\) \(l_2 \leqslant i \leqs ...

  9. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

随机推荐

  1. ubuntu分区

    参考网址:http://jingyan.baidu.com/article/60ccbceb18624464cab197ea.html

  2. Java——URLEncoder和URLDecoder

    import java.net.URLDecoder; import java.net.URLEncoder; //========================================== ...

  3. Ios 消息推送

    手把手教你做iOS推送 http://www.cocoachina.com/industry/20130321/5862.html http://www.cnblogs.com/cdts_change ...

  4. python错误

    1.IndentationError: unindent does not match any outer indentation level   原因:一般是代码没有对齐   参考网址: http: ...

  5. NopCommerce Url分析

    using System; using System.Web; using System.Web.Routing; using Nop.Core; using Nop.Core.Data; using ...

  6. OC-内存管理-基本原理与引用计数器

    基本原理 1. 什么是内存管理 移动设备的内存极其有限,每个app所能占用的内存是有限制的 当app所占用的内存较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间.比如回收一些不需要使用 ...

  7. yii2的urlManager配置

    网址伪静态是一个非常常用的网站需求,Yii2可以非常简单地进行配置. 首先,在配置文件config/main.php的'components' 段中,加入如下设置:'urlManager'=>a ...

  8. 注册Com组件..

    开发一些第三方的接口或者功能时..有时会需要接入对方提供Com组件..是一个dll文件.. 这个文件会在你的程序中引用.. 比如一个CMBChina.dll文件..这是招商银行的支付开发时必备的.. ...

  9. mysql存储过程之异常处理篇

    mysql存储过程也提供了对异常处理的功能:通过定义HANDLER来完成异常声明的实现 语法如下: DECLARE handler_type HANDLER FOR condition_value[, ...

  10. MySql循环插入数据(定义了存储过程)

    MySQL一窍不通啊,今天工作上需要用到,请教了别人,做以备忘 DROP PROCEDURE test_insert ; DELIMITER ;; CREATE PROCEDURE test_inse ...