CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)
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+7; int p[20],cnt,ans,X,Y; int qpow(int a,int x){ int res=1; while(x){ if(x&1) res=(ll)res*a%Mod; a=(ll)a*a%Mod; x>>=1; }return res; } void dfs(int pos,int s,int opt) { if(pos>cnt) { ans=((ans+opt*qpow(2,Y/s-1))%Mod+Mod)%Mod; return ;} dfs(pos+1,s,opt); dfs(pos+1,s*p[pos],-opt); } int main() { scanf("%d%d",&X,&Y); if(Y%X!=0) return puts("0"),0; Y/=X; int tp=Y; for(int i=2;i<=tp/i;i++){ if(tp%i==0) { p[++cnt]=i; while(tp%i==0) tp/=i; } } if(tp>1) p[++cnt]=tp; dfs(1,1,1); printf("%d\n",ans); return 0; }
It is your time to fight!