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 ...
随机推荐
- linux配置oracle11G监听及本地网络服务 及 数据库建库
配置监听及本地网络服务 在oracle用户的图形界面oracle用户中,新开启一个终端,输入命令netca 会弹出如下界面. 数据库建库 在oracle用户的图形界面oracle用户中,新开启一个终端 ...
- CodeForces 716A Crazy Computer
题目链接:http://codeforces.com/problemset/problem/716/A 题目大意: 输入 n c, 第二行 n 个整数,c表示时间间隔 秒. 每个整数代表是第几秒.如果 ...
- json 是个什么东西?
JSONP原理 JSONP(JSON with Padding),就是异步请求跨域的服务器端时,不是直接返回数据,而是返回一个js方法,把数据作为参数传过来.如果只是跨域传递数据那么这种方式是比较好的 ...
- LaTeX 算法代码排版 --latex2e范例总结
LaTeX 写作: 算法代码排版 --latex2e范例总结 latex2e 宏包的使用范例: \usepackage[ruled]{algorithm2e} ...
- js中自定义事件,使用了jQuery
$(function(){ $('#btn').bind("myClick", function(){ //自定义myClick事件 $('#test').append(" ...
- singleton和prototype
public class testScope { @Test public void test() { //默任singleton ApplicationContext ctx= new ClassP ...
- C#指定日期为一年中的第几周
/// <summary> /// 获取指定时间在为一年中为第几周 /// </summary> /// <param name="dt">指定 ...
- mysql支持跨表delete删除多表记录
前几天写了Mysql跨表更新的一篇总结,今天我们看下跨表删除. 在Mysql4.0之后,mysql开始支持跨表delete. Mysql可以在一个sql语句中同时删除多表记录,也可以根据多个表之间的关 ...
- Java中为什么有abstract interface 修饰类?
如果有人问你为什么有abstract interface 修饰类,答案一定是他看到的这种方式一定是反编译出来的结果.实际中abstract interface和interface修饰的类没有区别. 下 ...
- 基于AngularJS的过滤与排序
前面了解了AngularJS的使用方法,这里就简单的写个小程序,实现查询过滤以及排序的功能. 本程序中可以了解到: 1 angularjs的过滤器 2 ng-repeat的使用方法 3 控制器的使用 ...