Summer Training day4 cf439E 莫比乌斯反演

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.

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

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.




代码:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
int n,f;
const int MOD = 1e9 + 7;
const int maxn = 100006;  
int mu[maxn],a[maxn],prime[maxn],cnt[maxn],num[maxn];  
bool vis[maxn];  
int pnum;  
void mobeius(int N)  
{  
    pnum=0;  
    vis[1]=mu[1]=1;  
    for(int i=2;i<=N;i++)  
    {  
        if(!vis[i])  
        {  
            mu[i]=-1;  
            prime[pnum++]=i;  
        }  
        for(int j=0;j<pnum;j++)  
        {  
            if(i*prime[j]>N)break;  
            vis[i*prime[j]]=1;//ɸµôºÏÊý  
            if(i%prime[j]==0)  
            {  
                mu[i*prime[j]]=0;  
                break;//±£Ö¤ºÏÊýʹÓÃ×îСµÄËØÊýɸµôµÄ  
            }  
            mu[i*prime[j]]=-mu[i];  
        }  
    }  
}  
LL mod_pow(LL x,LL p,LL mod)
{
       LL ans = 1;
       while(p > 0)
       {
              if(p & 1)
                     ans = (x * ans)%mod;
              x = (x*x) % mod;
              p >>= 1;
       }
       return (ans + mod )% mod;
}
LL mod_inverse_feima(LL a,LL m)
{
       LL ans = mod_pow(a,m-2,m);
       return (ans+m) % m;
}
LL fact[maxn];
LL inv_fact[maxn];
void init(LL N,LL MOD){
	fact[0] = 1;
	for(int i = 1;i <= N;i++){
		fact[i] = fact[i-1]*i % MOD;
		
	}
	for(int i = 0;i <= N;i++){
		inv_fact[i] = mod_inverse_feima(fact[i],MOD) % MOD;
	}
}

LL C(LL n,LL m){
	//if(n == m && n == 0) return 1;
	if(m > n) return 0;
	//LL inv = mod_inverse_feima(fact[m],MOD) * mod_inverse_feima(fact[n-m],MOD) % MOD;
	LL inv = inv_fact[m] * inv_fact[n-m] % MOD;
	return fact[n] * inv % MOD;
}
int facs[10000];
int fcnt;
void make_factors(int tar){
	fcnt = 0;
	for(int i = 1;i * i <= tar;i++){
		if(tar % i == 0){
			facs[fcnt++] = i;
			if(i*i != tar) facs[fcnt++] = tar / i;
		}
	}
}
int main(){
	init(100005,MOD);
	//cout<<C(0,0)<<endl;
	mobeius(maxn-1);
	int T;scanf("%d",&T);
	while(T--){
		LL ans = 0;
		scanf("%d%d",&n,&f);
		make_factors(n);
		for(int i = 0;i < fcnt;i++){
			int d = facs[i];
			ans = (ans + mu[d] * C(n / d - 1,f - 1) + MOD) % MOD;
		}
		printf("%lld\n",ans);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值