Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such that gcd(a1, a2, ..., an) = x and . As this number could be large, print the answer modulo 109 + 7.

gcd here means the greatest common divisor.

Input

The only line contains two positive integers x and y (1 ≤ x, y ≤ 109).

Output

Print the number of such sequences modulo 109 + 7.

Examples

Input
3 9
Output
3
Input
5 8
Output
0

Note

There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3).

There are no suitable sequences in the second test.

题意:N个未知数,他们的GCD是X,和是Y,问方案数。

思路:显然我们枚举GCD=x,然后就是(Y/X)/x-1个隔板,2^隔板。前面加莫比乌斯系数mu[x/X]即可。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int Mod=1e9+;
int p[],cnt,ans,X,Y;
int qpow(int a,int x){
int res=; while(x){
if(x&) res=(ll)res*a%Mod;
a=(ll)a*a%Mod; x>>=;
}return res;
}
void dfs(int pos,int s,int opt)
{
if(pos>cnt) { ans=((ans+opt*qpow(,Y/s-))%Mod+Mod)%Mod; return ;}
dfs(pos+,s,opt); dfs(pos+,s*p[pos],-opt);
}
int main()
{
scanf("%d%d",&X,&Y);
if(Y%X!=) return puts(""),;
Y/=X; int tp=Y;
for(int i=;i<=tp/i;i++){
if(tp%i==) {
p[++cnt]=i;
while(tp%i==) tp/=i;
}
}
if(tp>) p[++cnt]=tp;
dfs(,,);
printf("%d\n",ans);
return ;
}

CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)的更多相关文章

  1. cf900D. Unusual Sequences(容斥 莫比乌斯反演)

    题意 题目链接 Sol 首先若y % x不为0则答案为0 否则,问题可以转化为,有多少个数列满足和为y/x,且整个序列的gcd=1 考虑容斥,设\(g[i]\)表示满足和为\(i\)的序列的方案数,显 ...

  2. Codeforces 900D Unusual Sequences 容斥原理

    题目链接:900D  Unusual Sequences 题意: 给出两个数N,M.让你求数列(和为M,gcd为N)的个数. 题解: 首先,比较容易发现的是M%N如果不为零,那么一定不能构成这样的序列 ...

  3. 【CF900D】Unusual Sequences 容斥(莫比乌斯反演)

    [CF900D]Unusual Sequences 题意:定义正整数序列$a_1,a_2...a_n$是合法的,当且仅当$gcd(a_1,a_2...a_n)=x$且$a_1+a_2+...+a_n= ...

  4. CodeForces 900D Unusual Sequences

    题目链接: https://codeforces.com/contest/900/problem/D 题意 假设有distinct 正整数序列{a1,a2,,,an},满足gcd(a1, a2, .. ...

  5. Codeforces 900D Unusual Sequences:记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/900/D 题意: 给定x,y,问你有多少个数列a满足gcd(a[i]) = x 且 ∑(a[i]) = y ...

  6. 【BZOJ3294】放棋子(动态规划,容斥,组合数学)

    [BZOJ3294]放棋子(动态规划,容斥,组合数学) 题面 BZOJ 洛谷 题解 如果某一行某一列被某一种颜色给占了,那么在考虑其他行的时候可以直接把这些行和这些列给丢掉. 那么我们就可以写出一个\ ...

  7. Codeforces 803F Coprime Subsequences (容斥)

    Link:http://codeforces.com/contest/803/problem/F 题意:给n个数字,求有多少个GCD为1的子序列. 题解:容斥!比赛时能写出来真是炒鸡开森啊! num[ ...

  8. Codeforces 100548F - Color (组合数+容斥)

    题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...

  9. 【二分+容斥+莫比乌斯反演】BZOJ2440 完全平方数

    Description 求第k个没有完全平方因子的数,k<=1e9. Solution 这其实就是要求第k个µ[i](莫比乌斯函数)不为0的数. 然而k太大数组开不下来是吧,于是这么处理. 二分 ...

随机推荐

  1. hive与hbase

    作者:有点文链接:https://www.zhihu.com/question/21677041/answer/185664626来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  2. asp.net 下载图片

    public class DownLoad : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Res ...

  3. 每天一个Linux命令(41)iostat命令

        iostat是I/O statistics(输入/输出统计)的缩写,对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况.     (1)用法:     ...

  4. 使用git从本地上传至git码云远程仓库

    从 http://git-scm.com/download  下载window版的客户端.下载好,一步一步安装即可. 使用前的基本设置 git  config --global user.name & ...

  5. [NOI2008]设计路线

    题目 洛谷 BZOJ 做法 神仙题 显然这是棵树 个节点相东仅连接一个结点 不同于剖分,还能存在\("V"\)字型,一个节点最多与另外节点连两条边 \(dp[i][j][k]\)表 ...

  6. 主攻ASP.NET.4.5.1 MVC5.0之重生:政府行政网站常用友情链接跳转javascript[干货分享]

    <!-----------------------------------> <script language="JavaScript" type="t ...

  7. Could not autowire field: private javax.servlet.http.HttpServletRequest

    在写单元测试类的时候,报错,废了很大劲才给调试好,给大家分享下. 完整错误如下: Caused by: org.springframework.beans.factory.BeanCreationEx ...

  8. 【P2107】小Z的AK计划(优先队列+贪心)

    水一发优先队列的水题.. 这个题貌似以前有做过类似的.具体的方法是用大根堆辅助贪心算法得出正解.可以看出来,如果小Z走到了某个地方,那么他最远一定是到了这里,不可能有再走回来这种操作,因为很明显那样不 ...

  9. ios 发布相关材料记录

    1 app icon Spotlight iOS 5,6 base: 29pt, 需要 @1x, @2x, @3x,得出:29 x 29, 58 x 58, 87 x 87 iOS 7,8 base: ...

  10. 基于主主复制的mysql双机热备+keepalived实现高可用性