codeforces439E: Devu and Birthday Celebration

Description

Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend.

He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai.

Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7).

To make the problem more interesting, you are given q queries. Each query contains an nf pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).

Input

The first line contains an integer q representing the number of queries (1 ≤ q ≤ 105). Each of the next q lines contains two space space-separated integers nf (1 ≤ f ≤ n ≤ 105).

Output

For each query, output a single integer in a line corresponding to the answer of each query.

Sample Input

Input
5
6 2
7 2
6 3
6 4
7 4
Output
2
6
9
10
20

Hint

For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].

For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.


对于直接求gcd=1的显然是不好做的

我们考虑求所有gcd=1的倍数的

这个用隔板法就可以得出答案是C(f-1,n-1)

再考虑求gcd=k的倍数的

那么因为求出的每一个方案所有数都是k的倍数

那么我们将n/k,然后再求gcd=1的倍数的即可

最后用莫比乌斯函数容斥一下就可以得出答案

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long mod=1000000007;
struct line
{
	int l;
	int x;
	bool operator< (line y) const
	{
		return l<y.l;
	}
}a[500001];
int ss[200001];
long long pow1[200001],pow2[200001];
inline long long power(long long x,int y)
{
	long long xt=1;
	while(y!=0)
	{
		if(y%2==1)
			xt=xt*x%mod;
		x=x*x%mod;
		y=y/2;
	}
	return xt;
}
inline long long prepare()
{
	long long i;
	pow1[0]=1;
	pow2[0]=1;
	for(i=1;i<=200000;i++)
	{
		pow1[i]=pow1[i-1]*i%mod;
		pow2[i]=power(pow1[i],mod-2);
	}
}
inline long long cale(long long k,long long n)
{
	return (pow1[n]*pow2[n-k]%mod)*pow2[k]%mod;
}
int main()
{
	prepare();
	int n,k;
	scanf("%d%d",&n,&k);
	int i;
	int p=0;
	for(i=1;i<=n;i++)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		p++;
		a[p].l=l;
		a[p].x=1;
		p++;
		a[p].l=r+1;
		a[p].x=-1;
	}
	sort(a+1,a+1+p);
	int d=1,sx=0,la=a[1].l;
	while(d<=p)
	{
		int sum=0;
		while(a[d].l==la)
		{
			sx+=a[d].x;
			d++;
		}
		ss[sx]+=a[d].l-la;
		la=a[d].l;
	}
	long long ans=0;
	for(i=k;i<=n;i++)
		ans=(ans+cale(k,i)*(long long)ss[i]%mod)%mod;
	printf("%I64d\n",ans);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值