codeforces900D Unusual Sequences

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
Copy
3 9
output
3
input
Copy
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.

题意:问能构造出多少个序列满足,序列中所有元素的gcd为x,序列中所有元素的和为y

题解:首先序列中每个数字都可以用x*k表示,所以y是x的倍数,先判定。

之后就是放球问题,j个球放到i个箱子里允许为空。但是我们还要减去gcd是2x,3x...的方案数,这些数字正好可以对x分解质因数,然后逐个求解并减去。

#include 
using namespace std;
typedef long long ll;
ll a[10005],dp[10005],n,m,tot;
ll pow(ll mi)
{
	ll res=1;
	for(ll a=2;mi;mi>>=1)
	{
		if(mi&1) res=res*a%1000000007;
		a=a*a%1000000007;
	}
	return res;
}
int main()
{
	scanf("%I64d%I64d",&n,&m);
	if(m%n) return puts("0"),0;
	for(ll i=1;i<=sqrt(m);i++)
		if(m%i==0)
		{
			if(i%n==0) a[++tot]=i;
			if(i*i!=m&&m/i%n==0) a[++tot]=m/i;
		}
	sort(a+1,a+tot+1);
	for(int i=tot;i;i--)
	{
		dp[i]=pow(m/a[i]-1);
		for(int j=i+1;j<=tot;j++)
			if(a[j]%a[i]==0)
				dp[i]=(dp[i]-dp[j]+1000000007)%1000000007;
	}
	printf("%I64d\n",dp[1]);
}

你可能感兴趣的:(数学,dp)