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 n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).
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 n, f (1 ≤ f ≤ n ≤ 105).
For each query, output a single integer in a line corresponding to the answer of each query.
题目意思就是 有n枚糖果,将它分配个f个不同的朋友,要求被分配给这些朋友的糖果最大公约数不能>1
直觉跟我说就是莫比乌斯。。然后推导一下,就出来公式了。。
先得到公式 F(n,f) 表示没有限制条件下的分配方案数, 将n个糖果成一个个点,水平放着,中间插 n-1个板,就是选出 f-1个板。
所以总方案 F(n,f) = C(n-1,f-1);
然后知道 F(n,f) = ∑G(d) // n 被 d整除 其中G(d)表示被分配出去糖果最大公约数刚好是d的情况。
然后反演知道 G(n) = ∑u(d)F(n/d) // n 被 d 整除
这时候G(1)也就是最大公约数是1的情况是答案,只用计算出 G(1)就好了。。
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define LL long long
const int N = 111111;
vector<int> P[N];
const int MOD = 1000000007;
LL d[N],e[N];
LL quick_pow(LL b, LL n){
LL ret = 1;
while(n != 0){
if(n % 2 == 1) ret = ret * b % MOD;
b = b * b % MOD;
n /= 2;
}
return ret;
}
LL C(LL n,LL m){
return d[n] * e[n-m] % MOD * e[m] % MOD;
}
int pri[N],pnum,mu[N],vis[N];
void mobius(int n){
pnum = 0 ,vis[1] = mu[1] = 1;
for(int i = 2;i <= n; i++) {
if(!vis[i]) {
pri[pnum++] = i;
mu[i] = -1;
}
for(int j = 0;j < pnum; j++) {
if(i*pri[j] > n) break;
vis[i*pri[j]] = 1;
if(i%pri[j] == 0) {
mu[i*pri[j]] = 0;
break;
}
mu[i*pri[j]] = -mu[i];
}
}
}
void init(){
d[0]=e[0]=1;
for(int i=1;i<N;i++){
d[i] = d[i-1] * i % MOD;
e[i] = quick_pow(d[i],MOD-2);
}
for(int i = 2;i < N; i++)
for(int j = i; j < N;j += i)
P[j].push_back(i);
mobius(N-1);
}
int q,n,f;
LL F(int n){
return C(n-1,f-1);
}
LL solve(){
LL ans = F(n);
for(int i = 0;i < P[n].size();i++){
int d = P[n][i];
if(n/d >= f){
ans = ( ans + mu[d] * F(n/d) ) %MOD;
}
}
return (ans + MOD)%MOD;
}
int main(){
init();
scanf("%d",&q);
while(q--){
scanf("%d %d",&n,&f);
cout << solve() << endl;
}
}