您的位置:首页 > 其它

CF 439 E. Devu and Birthday Celebration 莫比乌斯反演

2014-07-01 23:55 411 查看
E. Devu and Birthday Celebration

time limit per test
5 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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).

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 n, f (1 ≤ f ≤ n ≤ 105).

Output

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

Sample test(s)

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.

题目意思就是 有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
;

const int MOD = 1000000007;
LL d
,e
;

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
* e[n-m] % MOD * e[m] % MOD;
}
int pri
,pnum,mu
,vis
;
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
.size();i++){
int d = P
[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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CF ACM 莫比乌斯 数论