Codeforces Gym 101138 G. LCM-er
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的更多相关文章
- Codeforces Gym 100513G G. FacePalm Accounting
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
- Codeforces Gym 100637G G. #TheDress 暴力
G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...
- Codeforces Gym 100513G G. FacePalm Accounting 暴力
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
- Codeforces Gym 100203G G - Good elements 标记暴力
G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- Codeforces Gym 100203G G - Good elements 暴力
G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)
题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, …, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...
- Codeforces gym 101061 G【递推公式+逆元】
题意: 就是n复制m次,然后数mod1e9+7; 思路: 案例:31*10^6 + 31*10^4 + 31*10^2 + 31*10^0 所以就是一个等比数列,然后整理一下就是n*(10^(m*le ...
- Codeforces Gym 101138 D. Strange Queries
Description 给你一下长度为 \(n\) 的序列. \(a_i=a_j\) \(l_1 \leqslant i \leqslant r_1\) \(l_2 \leqslant i \leqs ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
随机推荐
- 自然语言21_Wordnet
QQ:231469242 欢迎喜欢nltk朋友交流 http://baike.baidu.com/link?url=YFVbJFMkZO9A5CAvtCoKbI609HxXXSFd8flFG_Lg ...
- 自然语言7_NLTK中文语料库sinica_treebank
http://www.hankcs.com/program/python/nltk-chinese-corpus-sinica_treebank.html NLTK包含Sinica (中央研究院)提供 ...
- String类中的一些函数使用方法
最常用的就是Length()函数了,求字符串的长度 String s="";int i=s.length();i结果为0. 如果是String s=null;int i=s.len ...
- Linux下的压缩解压缩命令详解
linux zip命令zip -r myfile.zip ./*将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzipunzip -o - ...
- Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges()?
LINK With the Entity Framework most of the time SaveChanges() is sufficient. This creates a transact ...
- Python开发【第五篇】:Python基础之杂货铺
字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...
- Mount挂载命令使用方法
语法: mount -t 类型 -o 挂接方式 源路径 目标路径 -t 详细选项: 光盘或光盘镜像:iso9660 DOS fat16文件系统:msdos Windows 9x fat32文件 ...
- 如何快速有效的修改java的环境变量
之前已经修改过jdk的环境变量,,,,在/etc/profile下,,, export JAVA_HOME=/usr/java/jdk1.7.0_67-cloudera export PATH=${J ...
- 父容器的flowover:hidden 必须配合父容器的宽高height width才能生效
有时候 给父容器 加上了 flowover:hidden 这个css后, 其中的子元素为什么仍然会跑出来? 解决的根本方法就是要给 父容器 加上具体的一个宽度, 或高度. (而这个宽度和高度, 其实你 ...
- Emacs配置文件
;;tab and space;;when true,emacs use mixture of tab and space to archieve(setq-default indent-tabs-m ...