codeforces 900D 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
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]);
	}
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值