Zap

FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。作为FGD的同学,FGD希望得到你的帮助。

Input

第一行包含一个正整数n,表示一共有n组询问。(1<=n<= 50000)接下来n行,每行表示一个询问,每行三个

正整数,分别为a,b,d。(1<=d<=a,b<=50000)

Output

对于每组询问,输出到输出文件zap.out一个正整数,表示满足条件的整数对数。

Sample Input

2

4 5 2

6 4 3

Sample Output

3

2

//对于第一组询问,满足条件的整数对有(2,2),(2,4),(4,2)。对于第二组询问,满足条件的整数对有(

6,3),(3,3)。

题解

原问题等价于求有多少对二元组\((x,y)\)满足\(x\le a/k,y\le b/k\)且\(x,y\)互质。

设\(D(a,b,k)\)表示满足\(x \le a,y \le b\)且\(k|\gcd(x,y)\)的二元组有多少对,显然\(D(a,b,k)=\lfloor a/k \rfloor\lfloor b/k \rfloor\)。

设\(F(a,b)\)表示\(x\le a,y\le b\)且\(x,y\)互质的二元组有多少对,由容斥原理可得:

\[F(a,b)=\sum_{i=1}^{\min(a,b)} \mu (i)*D(a,b,i)
\]

上式表示没有限制的是\(D(a,b,1)\),减去公因数至少有一个质因子的\(D(a,b,p)\),加上减重复了的。以此类推,\(D(a,b,i)\)的系数恰好就是Mobius函数。

另外\(D(a,b,i)\)在\(\forall i \in [x,\min(\lfloor a/\lfloor a/x \rfloor \rfloor,\lfloor b/\lfloor b/x \rfloor \rfloor)]\)的值都相等,可以整除分块。那么预处理出Mobius函数的前缀和,就可直接累加这一段的答案。时间复杂度\(O(N \log \log N +N( \sqrt{a}+\sqrt{b}))\)。

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll; co int N=5e4+1;
int miu[N];
bool v[N];
void Zap(){
int a,b,k,ans=0;
read(a),read(b),read(k);
a/=k,b/=k;
if(a>b) std::swap(a,b);
for(int x=1,gx;x<=a;x=gx+1){
gx=std::min(a/(a/x),b/(b/x));
ans+=(miu[gx]-miu[x-1])*(a/x)*(b/x);
}
printf("%d\n",ans);
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
for(int i=1;i<N;++i) miu[i]=1,v[i]=0;
for(int i=2;i<N;++i){
if(v[i]) continue;
miu[i]=-1;
for(int j=2*i;j<N;j+=i){
v[j]=1;
if(j/i%i==0) miu[j]=0;
else miu[j]*=-1;
}
}
for(int i=1;i<N;++i) miu[i]+=miu[i-1];
int n=read<int>();
while(n--) Zap();
return 0;
}

再分析

重温了一遍洛谷网课上AmberFrame的ppt。

\[\sum_{x=1}^a \sum_{y=1}^b [\gcd(x,y)=k] \\
=\sum_{x=1}^{\lfloor a/k \rfloor} \sum_{y=1}^{\lfloor b/k \rfloor} [\gcd(x,y)=1] \\
=\sum_{x=1}^{\lfloor a/k \rfloor} \sum_{y=1}^{\lfloor b/k \rfloor} \sum_{d|x \wedge d|y} \mu(d) \\
=\sum_{d=1}^{\lfloor a/k \rfloor} \lfloor \frac{a}{kd} \rfloor \lfloor \frac{b}{kd} \rfloor
\]

Devu and Flowers

Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contains fi flowers. All flowers in a single box are of the same color (hence they are indistinguishable). Also, no two boxes have flowers of the same color.

Now Devu wants to select exactly s flowers from the boxes to decorate his garden. Devu would like to know, in how many different ways can he select the flowers from each box? Since this number may be very large, he asks you to find the number modulo (109 + 7).

Devu considers two ways different if there is at least one box from which different number of flowers are selected in these two ways.

Input

The first line of input contains two space-separated integers n and s (1 ≤ n ≤ 20, 0 ≤ s ≤ 1014).

The second line contains n space-separated integers f1, f2, … fn (0 ≤ fi ≤ 1012).

Output

Output a single integer — the number of ways in which Devu can select the flowers modulo (109 + 7).

Sample test(s)

input

2 3

1 3

1

2

2 3

1 3

output

2

1

2

input

2 4

2 2

1

2

2 4

2 2

output

1

1

1

input

3 5

1 3 2

1

2

3 5

1 3 2

output

3

1

3

Note

Sample 1. There are two ways of selecting 3 flowers: {1, 2} and {0, 3}.

Sample 2. There is only one way of selecting 4 flowers: {2, 2}.

Sample 3. There are three ways of selecting 5 flowers: {1, 2, 2}, {0, 3, 2}, and {1, 3, 1}.

分析

多重集组合问题。根据结论,答案为

\[\binom{N+M-1}{N-1}-\sum_{i=1}^N \binom{N+M-A_i-2}{N-1} + \sum_{1 \le i < J \le N} \binom{N+M-A_i-A_j-3}{N-1}-\dots+(-1)^N \binom{N+M-\sum_{i=1}^N A_i -(N+1)}{N-1}
\]

由于N很小,所以直接枚举子集即可。算组合数的时候用for循环算。时间复杂度\(O(2^N N)\)

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll; co int mod=1e9+7;
ll a[22],m,ans=0;
int inv[22],n;
int power(int a,int b){
int c=1;
for(;b;b>>=1){
if(b&1) c=(ll)c*a%mod;
a=(ll)a*a%mod;
}
return c;
}
int C(ll y,int x){
if(y<0||x<0||y<x) return 0;
y%=mod;
if(y==0||x==0) return 1;
int ans=1;
for(int i=0;i<x;++i)
ans=(ll)ans*(y-i)%mod;
for(int i=1;i<=x;++i)
ans=(ll)ans*inv[i]%mod;
return ans;
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
for(int i=1;i<=20;++i) inv[i]=power(i,mod-2);
read(n),read(m);
for(int i=0;i<n;++i) read(a[i]);
for(int x=0;x<1<<n;++x){
ll t=n+m;
int p=0;
for(int i=0;i<n;++i)
if(x>>i&1) ++p,t-=a[i];
t-=p+1;
if(p&1) ans=(ans-C(t,n-1))%mod;
else ans=(ans+C(t,n-1))%mod;
}
printf("%lld\n",(ans+mod)%mod);
return 0;
}

BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers的更多相关文章

  1. [BZOJ1101][POI2007]Zap

    [BZOJ1101][POI2007]Zap 试题描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd ...

  2. BZOJ1101 POI2007 Zap 【莫比乌斯反演】

    BZOJ1101 POI2007 Zap Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b, ...

  3. CF451E Devu and Flowers 解题报告

    CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...

  4. CF451E Devu and Flowers(容斥)

    CF451E Devu and Flowers(容斥) 题目大意 \(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案.不一定每种花都要选到. \(n\le 20\) 解法 利用可重组合的公 ...

  5. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

  6. BZOJ1101: [POI2007]Zap(莫比乌斯反演)

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2951  Solved: 1293[Submit][Status ...

  7. Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...

  8. CF451E Devu and Flowers(组合数)

    题目描述 Devu想用花去装饰他的花园,他已经购买了n个箱子,第i个箱子有fi朵花,在同一个的箱子里的所有花是同种颜色的(所以它们没有任何其他特征).另外,不存在两个箱子中的花是相同颜色的. 现在De ...

  9. 【莫比乌斯反演】BZOJ1101 [POI2007]zap

    Description 回答T组询问,有多少组gcd(x,y)=d,x<=a, y<=b.T, a, b<=4e5. Solution 显然对于gcd=d的,应该把a/d b/d,然 ...

随机推荐

  1. SSH连接服务器时,长时间不操作就会断开的解决方案

    最近在配置服务器相关内容时候,不同的事情导致长时间不操作,页面就断开了连接,不能操作,只能关闭窗口,最后通过以下命令解决. SSH连接linux时,长时间不操作就断开的解决方案: 1.修改/etc/s ...

  2. IDEA设置方法注释生成模板

    1.在项目设置里面找到Editor-Live Templates(默认设置里没有这个),然后点击右边的+号,选择Template Group,创建模板组,我这里起名叫Silentdoer: 2.选中自 ...

  3. Lua 学习笔记 (1)

    最简单的 lua脚本 , do print ("line:", indx) end 也可以写成 , do print("line:", indx) end lu ...

  4. Resin 与 Tomcat 服务器对比

      Resin 与 Tomcat对比(个人总结) 图片来源Tomcat PK Resin 上图对比发现Tomcat对于Resin来说,有诸多优点,但是Resin也有很多优点. 比方说: 速度比较 re ...

  5. scrapy2——框架简介和抓取流程

    scrapy简介 ​ Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中 scrapy的执行流程 Scrapy主要包括 ...

  6. uwsgi flask gevent 测试代码覆盖率(coverage)

    目录 可能出现的问题 解决 可能出现的问题 多进程启动 gevent启动 运行的服务可能不会停止 解决 我先参考了一下这一篇文章使用Coverage分析WSGI项目的代码覆盖率,他基本能够解决掉1.2 ...

  7. Linux磁盘管理系列 — LVM和RAID

    一.逻辑卷管理器(LVM) 1.什么是逻辑卷管理器(LVM) LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对卷进行操作的抽象层. LVM是建立在硬盘 ...

  8. 轻松玩转Ant Design Pro一

    ant design pro来源于ant design,其是一段自带样式的react组件,用于企业后台的漂亮的,可控的组件.ant design有很多组件和样式,不可能所有都记住,我们只要记住常用的, ...

  9. js 遍历树的层级关系的实现

    1.遍历树的层级关系 1)先整理数据 2)找到id和数据的映射关系 3)然后找到父节点的数据,进行存储 test() { const list = [ { id: ", parentId: ...

  10. 加快JavaScript加载和执行效率

    JavaScript 在浏览器中的性能成为开发者所面临的最重要的可用性问题.而这个问题又因 JavaScript 的阻塞特性变的复杂,也就是说当浏览器在执行 JavaScript 代码时,不能同时做其 ...