D. Unusual Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
题意:给出 x,y 让求 有几种组合使得他们的gcd为 x并且和为y
分析:这题有点类似于隔板法
那么知道满足的最小条件时 y%x==0
然后将 y分成 p=y/x分 每一份就为 x且 满足条件
这个时候这 p份可以排列组合得到的也必定满足 和为 y
那么只需要容斥去掉 gcd不为 x的就可以了
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
long long f(long long a,long long b)
{
long long c=1;
while(b)
{
if(b&1)
{
c*=a;
c%=mod;
}
b>>=1;
a*=a;
a%=mod;
}
return c;
}
long long vis[1000000];
long long dp[1000000]={0};
int main()
{
long long x,y;
scanf("%lld%lld",&x,&y);
int t=0;
if(y%x)
printf("0\n");
else
{
long long q=y/x;
for(long long i=1;i*i<=q;i++)
{
if(q%i==0)
{
vis[t++]=i;
if(i*i!=q)
{
vis[t++]=q/i;
//printf("%lld\n",q/i);
}
}
}
sort(vis,vis+t);
for(int i=0;i<t;i++)
{
//printf("%lld ",vis[i]);
dp[i]=f(2LL,(q/vis[i])-1LL);
//printf("%lld %lld %lld\n",vis[i],dp[i],(q/vis[i])-1);
}
for(int i=t-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(vis[i]%vis[j]==0)
{
dp[j]=(dp[j]-dp[i]+mod)%mod;
}
}
}
printf("%lld\n",dp[0]);
}
}