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下JDK的安装

    硬盘上有下载好的JDK,直接解压后配置profile环境变量就行 export JAVA_HOME=/usr/lib/jvm/java-8-oracle export JRE_HOME=${JAVA_ ...

  2. JQM页面跳转,多种效果

    <div data-role="page" id="pageone"> <div data-role="header"&g ...

  3. js数字、字符串、数组之间的转化

    1.数组转字符串 var a, b; a = ,,,,); b = a.join("-"); 2.字符串转数组 var s = "abc,abcd,aaa"; ...

  4. mysql之用户授权

    授权命令: 1.全部权限:grant all on *.* to user@192.168.10.2 identified by "pass": 2.部分权限:grant sele ...

  5. git命令之git tag 给当前分支打标签

    git tag - 标签相关操作 发表于 2011年06月29日 由 机器猫 标签可以针对某一时间点的版本做标记,常用于版本发布. 列出标签 $ git tag # 在控制台打印出当前仓库的所有标签$ ...

  6. python中os/sys/platform模块区别

    os:This module provides a portable way of using operating system dependent functionality. sys:This m ...

  7. Java面试笔记

    1.&和&& if(str != null& !str.equals("")){ System.out.println("ok" ...

  8. 正确运用synchronized和二次判断 实现多线程安全

    正确运用synchronized和二次判断 实现多线程安全,做出高效二符合预期的程序,特别是多个线程跑一个对象的时候,如下图所示:  测试代码如下: 特别注意if(shutdownRequested) ...

  9. 再说vundle: 完全vim字符编程的四个必须插件 - zen coding 和emmet插件的使用

    一个常识: 基本上vim插件的配置文集都是放在对应插件目录 的/autoload/ plugin_name.vim 文件中的 有四个必要/必须的插件,实现vim完全的字符界面的编程: NERDTree ...

  10. 黄永成-thinkphp讲解-个人博客讲解26集

    如果是要导入 前后台分组都公用的 第三方类库, 就将类库(*.class.php文件) 放在 App下的Class文件夹中. 导入方法是: import('Class.Image', APP_PATH ...