output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int Inverse[100010];
int Factorial[100010];
int used[100010], F[100010];
vector<int> Divisors[100010];
int power(int a, int n) {
if (n == 0) return 1;
if (n == 1) return a % 1000000007;
int temp = power(a, n / 2);
temp = (1LL * temp * temp) % 1000000007;
if (n % 2 == 1) temp = (1LL * temp * a) % 1000000007;
return temp;
}
void init() {
for (int i = 1; i <= 100000; i++)
for (int j = i + i; j <= 100000; j += i) Divisors[j].push_back(i);
Factorial[0] = 1;
for (int i = 1; i <= 100000; i++)
Factorial[i] = (1LL * Factorial[i - 1] * i) % 1000000007;
for (int i = 1; i <= 100000; i++)
Inverse[i] = power(Factorial[i], 1000000007 - 2);
}
int C(int k, int n) {
if (k == 0) return 1;
if (k > n) return 0;
int temp = (1LL * Factorial[n] * Inverse[k]) % 1000000007;
return (1LL * temp * Inverse[n - k]) % 1000000007;
}
int cal(int T, int m, int n) {
if (m == n) return 1;
if (n > m) return 0;
if (used[m] == T) return F[m];
used[m] = T;
int res = C(n - 1, m - 1);
for (auto x : Divisors[m]) {
res = (res - cal(T, x, n)) % 1000000007;
}
res = (res + 1000000007) % 1000000007;
return F[m] = res;
}
int main() {
if (fopen("BIRTHDAY"
".inp",
"r")) {
freopen(
"BIRTHDAY"
".inp",
"r", stdin);
freopen(
"BIRTHDAY"
".out",
"w", stdout);
} else
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
init();
cin >> q;
for (int i = 1; i <= q; i++) {
int m, n;
cin >> m >> n;
cout << cal(i, m, n) << '\n';
}
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int Inverse[100010];
int Factorial[100010];
int used[100010], F[100010];
vector<int> Divisors[100010];
int power(int a, int n) {
if (n == 0) return 1;
if (n == 1) return a % 1000000007;
int temp = power(a, n / 2);
temp = (1LL * temp * temp) % 1000000007;
if (n % 2 == 1) temp = (1LL * temp * a) % 1000000007;
return temp;
}
void init() {
for (int i = 1; i <= 100000; i++)
for (int j = i + i; j <= 100000; j += i) Divisors[j].push_back(i);
Factorial[0] = 1;
for (int i = 1; i <= 100000; i++)
Factorial[i] = (1LL * Factorial[i - 1] * i) % 1000000007;
for (int i = 1; i <= 100000; i++)
Inverse[i] = power(Factorial[i], 1000000007 - 2);
}
int C(int k, int n) {
if (k == 0) return 1;
if (k > n) return 0;
int temp = (1LL * Factorial[n] * Inverse[k]) % 1000000007;
return (1LL * temp * Inverse[n - k]) % 1000000007;
}
int cal(int T, int m, int n) {
if (m == n) return 1;
if (n > m) return 0;
if (used[m] == T) return F[m];
used[m] = T;
int res = C(n - 1, m - 1);
for (auto x : Divisors[m]) {
res = (res - cal(T, x, n)) % 1000000007;
}
res = (res + 1000000007) % 1000000007;
return F[m] = res;
}
int main() {
if (fopen("BIRTHDAY"
".inp",
"r")) {
freopen(
"BIRTHDAY"
".inp",
"r", stdin);
freopen(
"BIRTHDAY"
".out",
"w", stdout);
} else
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
init();
cin >> q;
for (int i = 1; i <= q; i++) {
int m, n;
cin >> m >> n;
cout << cal(i, m, n) << '\n';
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int modl = 1e9 + 7;
vector<int> divisor[100010];
long long fact[100010], rfact[100010];
long long pow(long long n, int k) {
if (k == 0) return 1;
long long ret = pow(n, k / 2);
ret = (ret * ret) % modl;
if (k & 1) ret = (ret * n) % modl;
return ret;
}
int C(int n, int k) {
if (n < 0 || k < 0 || n < k) return 0;
return (((fact[n] * rfact[k]) % modl) * rfact[n - k]) % modl;
}
int bit(int n, int k) { return (n >> k) & 1; }
int main() {
for (int i = 2; i <= 100000; i++)
if (divisor[i].empty())
for (int j = i + i; j <= 100000; j += i) divisor[j].push_back(i);
fact[0] = rfact[0] = 1;
for (int i = 1; i <= 100000; i++) {
fact[i] = (fact[i - 1] * i) % modl;
rfact[i] = pow(fact[i], modl - 2);
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int n, f;
scanf("%d%d", &n, &f);
int ret = 0;
for (int t = 0; t < (1 << divisor[n].size()); t++) {
int product = 1, cntBit = 0;
for (int j = 0; j < divisor[n].size(); j++)
if (bit(t, j)) {
cntBit++;
product *= divisor[n][j];
}
if (cntBit & 1)
ret = (ret - C(n / product - 1, f - 1)) % modl;
else
ret = (ret + C(n / product - 1, f - 1)) % modl;
}
if (ret < 0) ret += modl;
if (f == 1) ret = (n == 1);
printf("%d\n", ret);
}
}
|
### Prompt
Please create a solution in CPP to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int modl = 1e9 + 7;
vector<int> divisor[100010];
long long fact[100010], rfact[100010];
long long pow(long long n, int k) {
if (k == 0) return 1;
long long ret = pow(n, k / 2);
ret = (ret * ret) % modl;
if (k & 1) ret = (ret * n) % modl;
return ret;
}
int C(int n, int k) {
if (n < 0 || k < 0 || n < k) return 0;
return (((fact[n] * rfact[k]) % modl) * rfact[n - k]) % modl;
}
int bit(int n, int k) { return (n >> k) & 1; }
int main() {
for (int i = 2; i <= 100000; i++)
if (divisor[i].empty())
for (int j = i + i; j <= 100000; j += i) divisor[j].push_back(i);
fact[0] = rfact[0] = 1;
for (int i = 1; i <= 100000; i++) {
fact[i] = (fact[i - 1] * i) % modl;
rfact[i] = pow(fact[i], modl - 2);
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int n, f;
scanf("%d%d", &n, &f);
int ret = 0;
for (int t = 0; t < (1 << divisor[n].size()); t++) {
int product = 1, cntBit = 0;
for (int j = 0; j < divisor[n].size(); j++)
if (bit(t, j)) {
cntBit++;
product *= divisor[n][j];
}
if (cntBit & 1)
ret = (ret - C(n / product - 1, f - 1)) % modl;
else
ret = (ret + C(n / product - 1, f - 1)) % modl;
}
if (ret < 0) ret += modl;
if (f == 1) ret = (n == 1);
printf("%d\n", ret);
}
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int MAX = 1e5 + 5;
const long long MAX2 = 11;
const long long MOD = 1000000007;
const long long MOD2 = 1000005329;
const long long INF = 2e18;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 555;
long long tc, n, k, f[MAX], pre[MAX], p[MAX], dp[MAX], ans;
vector<int> fc[MAX];
inline long long pw(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1) ret = ret * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return ret;
}
inline long long cb(int n, int k) {
--n, --k;
return f[n] * pre[k] % MOD * pre[n - k] % MOD;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int i = 2; i <= 316; ++i) {
if (p[i]) continue;
for (int j = i; j <= 100000; j += i) p[j] = i;
}
f[0] = pre[0] = 1;
for (int i = 1; i <= 100000; ++i) {
f[i] = i * f[i - 1] % MOD, pre[i] = pw(f[i], MOD - 2);
if (!p[i]) p[i] = i;
}
dp[1] = 1;
fc[1].push_back(1);
for (int i = 2; i <= 100000; ++i) {
n = i / p[i];
if (dp[n] && p[n] != p[i]) dp[i] = -dp[n];
n = sqrt(i);
for (int j = 1; j <= n; ++j)
if (i % j == 0) {
if (dp[j]) fc[i].push_back(j);
if (dp[i / j]) fc[i].push_back(i / j);
}
if (n * n == i && dp[n]) fc[i].pop_back();
}
cin >> tc;
for (int ntc = 1; ntc <= tc; ++ntc) {
cin >> k >> n;
ans = 0;
for (int j : fc[k])
if (n <= k / j) ans += dp[j] * cb(k / j, n);
ans %= MOD;
if (ans < 0) ans += MOD;
cout << ans << '\n';
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int MAX = 1e5 + 5;
const long long MAX2 = 11;
const long long MOD = 1000000007;
const long long MOD2 = 1000005329;
const long long INF = 2e18;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 555;
long long tc, n, k, f[MAX], pre[MAX], p[MAX], dp[MAX], ans;
vector<int> fc[MAX];
inline long long pw(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1) ret = ret * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return ret;
}
inline long long cb(int n, int k) {
--n, --k;
return f[n] * pre[k] % MOD * pre[n - k] % MOD;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int i = 2; i <= 316; ++i) {
if (p[i]) continue;
for (int j = i; j <= 100000; j += i) p[j] = i;
}
f[0] = pre[0] = 1;
for (int i = 1; i <= 100000; ++i) {
f[i] = i * f[i - 1] % MOD, pre[i] = pw(f[i], MOD - 2);
if (!p[i]) p[i] = i;
}
dp[1] = 1;
fc[1].push_back(1);
for (int i = 2; i <= 100000; ++i) {
n = i / p[i];
if (dp[n] && p[n] != p[i]) dp[i] = -dp[n];
n = sqrt(i);
for (int j = 1; j <= n; ++j)
if (i % j == 0) {
if (dp[j]) fc[i].push_back(j);
if (dp[i / j]) fc[i].push_back(i / j);
}
if (n * n == i && dp[n]) fc[i].pop_back();
}
cin >> tc;
for (int ntc = 1; ntc <= tc; ++ntc) {
cin >> k >> n;
ans = 0;
for (int j : fc[k])
if (n <= k / j) ans += dp[j] * cb(k / j, n);
ans %= MOD;
if (ans < 0) ans += MOD;
cout << ans << '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100010];
long long fac[100010], revfac[100010];
long long pow(long long a, int k) {
if (!k) return 1;
long long ans = pow(a, k >> 1);
ans = ans * ans % 1000000007;
if (k & 1) ans = ans * a % 1000000007;
return ans;
}
long long H(int n, int f) {
if (n < f) return 0;
return fac[n - 1] * revfac[n - f] % 1000000007 * revfac[f - 1] % 1000000007;
}
long long val[100010];
int calc(int n, int f) {
if (f == 1) return n == 1;
long long ans = H(n, f);
for (int i = 0, sz = adj[n].size(); i < sz; i++) {
int d = adj[n][i];
if (d < f) {
val[d] = 0;
continue;
}
val[d] = H(d, f);
for (int j = adj[d].size(); j--;) {
int dd = adj[d][j];
val[d] -= val[dd];
}
val[d] %= 1000000007;
if (val[d] < 0) val[d] += 1000000007;
ans -= val[d];
}
ans %= 1000000007;
if (ans < 0) ans += 1000000007;
return ans;
}
void pre() {
fac[0] = revfac[0] = 1;
for (int i = 1; i < 100010; i++) {
fac[i] = fac[i - 1] * i % 1000000007;
revfac[i] = pow(fac[i], 1000000007 - 2);
}
for (int i = 2, j; i < 100010; i++) {
for (j = 2; j * j < i; j++) {
if (i % j == 0) adj[i].push_back(j);
}
if (j * j == i) adj[i].push_back(j);
for (j--; j > 1; j--) {
if (i % j == 0) adj[i].push_back(i / j);
}
}
}
int main() {
pre();
int q, n, f;
cin >> q;
while (q--) {
scanf("%d %d", &n, &f);
printf("%d\n", calc(n, f));
}
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100010];
long long fac[100010], revfac[100010];
long long pow(long long a, int k) {
if (!k) return 1;
long long ans = pow(a, k >> 1);
ans = ans * ans % 1000000007;
if (k & 1) ans = ans * a % 1000000007;
return ans;
}
long long H(int n, int f) {
if (n < f) return 0;
return fac[n - 1] * revfac[n - f] % 1000000007 * revfac[f - 1] % 1000000007;
}
long long val[100010];
int calc(int n, int f) {
if (f == 1) return n == 1;
long long ans = H(n, f);
for (int i = 0, sz = adj[n].size(); i < sz; i++) {
int d = adj[n][i];
if (d < f) {
val[d] = 0;
continue;
}
val[d] = H(d, f);
for (int j = adj[d].size(); j--;) {
int dd = adj[d][j];
val[d] -= val[dd];
}
val[d] %= 1000000007;
if (val[d] < 0) val[d] += 1000000007;
ans -= val[d];
}
ans %= 1000000007;
if (ans < 0) ans += 1000000007;
return ans;
}
void pre() {
fac[0] = revfac[0] = 1;
for (int i = 1; i < 100010; i++) {
fac[i] = fac[i - 1] * i % 1000000007;
revfac[i] = pow(fac[i], 1000000007 - 2);
}
for (int i = 2, j; i < 100010; i++) {
for (j = 2; j * j < i; j++) {
if (i % j == 0) adj[i].push_back(j);
}
if (j * j == i) adj[i].push_back(j);
for (j--; j > 1; j--) {
if (i % j == 0) adj[i].push_back(i / j);
}
}
}
int main() {
pre();
int q, n, f;
cin >> q;
while (q--) {
scanf("%d %d", &n, &f);
printf("%d\n", calc(n, f));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const long long MOD = 1e9 + 7;
int n, q, m;
int prime[MAXN], primeN;
int minPrimeCnt[MAXN], divisor[MAXN];
bool isPrime[MAXN];
void getDivisor(int n) {
for (int i = 1; i <= n; i++) isPrime[i] = true;
isPrime[1] = false;
primeN = 0;
divisor[1] = 1;
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
prime[++primeN] = i;
divisor[i] = 2;
minPrimeCnt[i] = 2;
}
for (int j = 1; j <= primeN; j++) {
int nxt = prime[j] * i;
if (nxt > n) break;
divisor[nxt] = divisor[i] * 2;
minPrimeCnt[nxt] = 2;
isPrime[nxt] = false;
if (i % prime[j] == 0) {
minPrimeCnt[nxt] = minPrimeCnt[i] + 1;
divisor[nxt] = divisor[i] / minPrimeCnt[i] * minPrimeCnt[nxt];
break;
}
}
}
}
long long inv[MAXN];
long long getinv(long long a) {
if (inv[a] != -1) return inv[a];
inv[a] = (a == 1 ? 1 : (MOD - MOD / a) * getinv(MOD % a) % MOD);
return inv[a];
}
long long f[MAXN], invf[MAXN];
long long c(int n, int m) {
if (n < m) return 0;
if (n == m)
return 1;
else
return f[n] * invf[m] % MOD * invf[n - m] % MOD;
}
int di[MAXN][200], dn[MAXN];
void pre() {
n = 1e5;
getDivisor(n);
for (int i = 1; i <= n; i++) inv[i] = -1;
for (int i = 1; i <= n; i++) getinv(i);
f[0] = invf[0] = 1;
for (int i = 1; i <= n; i++) {
f[i] = f[i - 1] * i % MOD;
invf[i] = invf[i - 1] * inv[i] % MOD;
}
memset(dn, 0, sizeof(dn));
memset(di, 0, sizeof(di));
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i) {
di[j][++dn[j]] = i;
}
}
}
long long ans[MAXN];
int cnt[MAXN];
int factor[MAXN], fn;
int main() {
pre();
while (scanf("%d", &q) != EOF) {
for (int iq = 1; iq <= q; iq++) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= dn[n]; i++) cnt[di[n][i]] = 0;
cnt[n] = 1;
for (int i = 1; i <= dn[n] - 1; i++) cnt[di[n][i]] = -1;
for (int i = dn[n] - 1; i > 0; i--) {
int x = di[n][i];
if (cnt[x] == 0) continue;
for (int i = 1; i <= dn[x] - 1; i++) cnt[di[x][i]] += -cnt[x];
}
long long res = 0;
for (int i = 1; i <= dn[n]; i++) {
int x = di[n][i];
res += cnt[x] * c(x - 1, m - 1) % MOD;
if (res < 0) res += MOD;
if (res >= MOD) res -= MOD;
}
printf("%I64d\n", res);
}
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const long long MOD = 1e9 + 7;
int n, q, m;
int prime[MAXN], primeN;
int minPrimeCnt[MAXN], divisor[MAXN];
bool isPrime[MAXN];
void getDivisor(int n) {
for (int i = 1; i <= n; i++) isPrime[i] = true;
isPrime[1] = false;
primeN = 0;
divisor[1] = 1;
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
prime[++primeN] = i;
divisor[i] = 2;
minPrimeCnt[i] = 2;
}
for (int j = 1; j <= primeN; j++) {
int nxt = prime[j] * i;
if (nxt > n) break;
divisor[nxt] = divisor[i] * 2;
minPrimeCnt[nxt] = 2;
isPrime[nxt] = false;
if (i % prime[j] == 0) {
minPrimeCnt[nxt] = minPrimeCnt[i] + 1;
divisor[nxt] = divisor[i] / minPrimeCnt[i] * minPrimeCnt[nxt];
break;
}
}
}
}
long long inv[MAXN];
long long getinv(long long a) {
if (inv[a] != -1) return inv[a];
inv[a] = (a == 1 ? 1 : (MOD - MOD / a) * getinv(MOD % a) % MOD);
return inv[a];
}
long long f[MAXN], invf[MAXN];
long long c(int n, int m) {
if (n < m) return 0;
if (n == m)
return 1;
else
return f[n] * invf[m] % MOD * invf[n - m] % MOD;
}
int di[MAXN][200], dn[MAXN];
void pre() {
n = 1e5;
getDivisor(n);
for (int i = 1; i <= n; i++) inv[i] = -1;
for (int i = 1; i <= n; i++) getinv(i);
f[0] = invf[0] = 1;
for (int i = 1; i <= n; i++) {
f[i] = f[i - 1] * i % MOD;
invf[i] = invf[i - 1] * inv[i] % MOD;
}
memset(dn, 0, sizeof(dn));
memset(di, 0, sizeof(di));
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i) {
di[j][++dn[j]] = i;
}
}
}
long long ans[MAXN];
int cnt[MAXN];
int factor[MAXN], fn;
int main() {
pre();
while (scanf("%d", &q) != EOF) {
for (int iq = 1; iq <= q; iq++) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= dn[n]; i++) cnt[di[n][i]] = 0;
cnt[n] = 1;
for (int i = 1; i <= dn[n] - 1; i++) cnt[di[n][i]] = -1;
for (int i = dn[n] - 1; i > 0; i--) {
int x = di[n][i];
if (cnt[x] == 0) continue;
for (int i = 1; i <= dn[x] - 1; i++) cnt[di[x][i]] += -cnt[x];
}
long long res = 0;
for (int i = 1; i <= dn[n]; i++) {
int x = di[n][i];
res += cnt[x] * c(x - 1, m - 1) % MOD;
if (res < 0) res += MOD;
if (res >= MOD) res -= MOD;
}
printf("%I64d\n", res);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long P = 1000000007;
const int N = 100001;
long long fac[N], facr[N];
int mobius[N];
bool prime[N];
long long inv(long long a) {
long long pow = a, ret = 1, exp = P - 2, bit = 1;
while (exp > 0) {
if (exp & bit) {
ret = (ret * pow) % P;
exp ^= bit;
}
bit <<= 1;
pow = (pow * pow) % P;
}
return ret;
}
long long C(int n, int r) {
if (n < r)
return 0;
else
return ((fac[n] * facr[r]) % P * facr[n - r]) % P;
}
int main() {
fac[0] = 1;
facr[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = (fac[i - 1] * i) % P;
facr[i] = inv(fac[i]);
}
fill_n(prime, N, true);
for (int i = 2; i * i < N; i++)
if (prime[i])
for (int j = i * i; j < N; j += i) prime[j] = false;
fill_n(mobius, N, 1);
for (int i = 2; i * i < N; i++)
if (prime[i])
for (int j = i * i; j < N; j += i * i) mobius[j] = 0;
for (int i = 2; i < N; i++)
if (prime[i])
for (int j = i; j < N; j += i) mobius[j] = -mobius[j];
int q, n, f;
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
long long ans = 0;
for (int x = 1; x * x <= n; x++)
if (n % x == 0) {
ans += P + C(n / x - 1, f - 1) * mobius[x];
if (x * x < n) ans += P + C(x - 1, f - 1) * mobius[n / x];
}
printf("%lld\n", ans % P);
}
}
|
### Prompt
In Cpp, your task is to solve the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long P = 1000000007;
const int N = 100001;
long long fac[N], facr[N];
int mobius[N];
bool prime[N];
long long inv(long long a) {
long long pow = a, ret = 1, exp = P - 2, bit = 1;
while (exp > 0) {
if (exp & bit) {
ret = (ret * pow) % P;
exp ^= bit;
}
bit <<= 1;
pow = (pow * pow) % P;
}
return ret;
}
long long C(int n, int r) {
if (n < r)
return 0;
else
return ((fac[n] * facr[r]) % P * facr[n - r]) % P;
}
int main() {
fac[0] = 1;
facr[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = (fac[i - 1] * i) % P;
facr[i] = inv(fac[i]);
}
fill_n(prime, N, true);
for (int i = 2; i * i < N; i++)
if (prime[i])
for (int j = i * i; j < N; j += i) prime[j] = false;
fill_n(mobius, N, 1);
for (int i = 2; i * i < N; i++)
if (prime[i])
for (int j = i * i; j < N; j += i * i) mobius[j] = 0;
for (int i = 2; i < N; i++)
if (prime[i])
for (int j = i; j < N; j += i) mobius[j] = -mobius[j];
int q, n, f;
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
long long ans = 0;
for (int x = 1; x * x <= n; x++)
if (n % x == 0) {
ans += P + C(n / x - 1, f - 1) * mobius[x];
if (x * x < n) ans += P + C(x - 1, f - 1) * mobius[n / x];
}
printf("%lld\n", ans % P);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int prime[N], mu[N], m, bo[N];
void mobius() {
mu[1] = 1;
memset(bo, 0, sizeof(bo));
for (int i = 2; i < N; ++i) {
if (!bo[i]) {
mu[i] = -1;
prime[m++] = i;
}
for (int j = 0; j < m && prime[j] * i < N; ++j) {
bo[i * prime[j]] = 1;
if (i % prime[j])
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
}
const int Q = 1e9 + 7;
int I[N], P[N];
void init() {
I[0] = I[1] = P[0] = P[1] = 1;
for (int i = 2; i < N; ++i) {
I[i] = ((long long)Q - Q / i) * I[Q % i] % Q;
P[i] = i;
}
for (int i = 2; i < N; ++i) {
I[i] = (long long)I[i - 1] * I[i] % Q;
P[i] = (long long)P[i - 1] * P[i] % Q;
}
}
int C(int n, int m) { return (long long)P[n] * I[m] % Q * I[n - m] % Q; }
void work() {
int n, f;
scanf("%d%d", &n, &f);
int ans = 0;
for (int i = 1; i * i <= n; ++i) {
if (n % i) continue;
int m = n / i;
if (m >= f) ans += C(m - 1, f - 1) * mu[i];
if (ans < 0) ans += Q;
if (ans >= Q) ans -= Q;
if (n / i == i) continue;
m = i;
if (m >= f) ans += C(m - 1, f - 1) * mu[n / i];
if (ans < 0) ans += Q;
if (ans >= Q) ans -= Q;
}
printf("%d\n", ans);
}
int main() {
mobius();
init();
int t;
scanf("%d", &t);
while (t--) work();
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int prime[N], mu[N], m, bo[N];
void mobius() {
mu[1] = 1;
memset(bo, 0, sizeof(bo));
for (int i = 2; i < N; ++i) {
if (!bo[i]) {
mu[i] = -1;
prime[m++] = i;
}
for (int j = 0; j < m && prime[j] * i < N; ++j) {
bo[i * prime[j]] = 1;
if (i % prime[j])
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
}
const int Q = 1e9 + 7;
int I[N], P[N];
void init() {
I[0] = I[1] = P[0] = P[1] = 1;
for (int i = 2; i < N; ++i) {
I[i] = ((long long)Q - Q / i) * I[Q % i] % Q;
P[i] = i;
}
for (int i = 2; i < N; ++i) {
I[i] = (long long)I[i - 1] * I[i] % Q;
P[i] = (long long)P[i - 1] * P[i] % Q;
}
}
int C(int n, int m) { return (long long)P[n] * I[m] % Q * I[n - m] % Q; }
void work() {
int n, f;
scanf("%d%d", &n, &f);
int ans = 0;
for (int i = 1; i * i <= n; ++i) {
if (n % i) continue;
int m = n / i;
if (m >= f) ans += C(m - 1, f - 1) * mu[i];
if (ans < 0) ans += Q;
if (ans >= Q) ans -= Q;
if (n / i == i) continue;
m = i;
if (m >= f) ans += C(m - 1, f - 1) * mu[n / i];
if (ans < 0) ans += Q;
if (ans >= Q) ans -= Q;
}
printf("%d\n", ans);
}
int main() {
mobius();
init();
int t;
scanf("%d", &t);
while (t--) work();
return 0;
}
```
|
#include <bits/stdc++.h>
const int mo = 1000000007;
const int MAX = 100000;
using namespace std;
int p[100005], ny[100005], Hash[100005];
int ss[100005][30], n, f, q, i, j, ans;
int sumi(int a, int b) {
int ret = 1, c = a;
while (b) {
if (b & 1) ret = (long long)ret * c % mo;
c = (long long)c * c % mo;
b >>= 1;
}
return ret;
}
int C(int n, int m) {
if (n < m) return 0;
return (long long)p[n] * ny[m] % mo * ny[n - m] % mo;
}
void dfs(int i, int s, int odd) {
if (i > ss[n][0]) {
if (odd)
ans -= C(n / s - 1, f - 1);
else
ans += C(n / s - 1, f - 1);
ans %= mo;
return;
}
dfs(i + 1, s, odd);
dfs(i + 1, s * ss[n][i], 1 - odd);
}
int main() {
for (i = 2; i <= MAX; i++) {
if (Hash[i]) continue;
ss[i][++ss[i][0]] = i;
for (j = i + i; j <= MAX; j += i) {
ss[j][++ss[j][0]] = i;
Hash[j] = 1;
}
}
p[0] = ny[0] = 1;
for (i = 1; i <= MAX; i++) {
p[i] = (long long)p[i - 1] * i % mo;
ny[i] = (long long)ny[i - 1] * sumi(i, mo - 2) % mo;
}
scanf("%d", &q);
while (q--) {
scanf("%d %d", &n, &f);
if (n == 1) {
printf("1\n");
continue;
}
ans = 0;
dfs(1, 1, 0);
while (ans < 0) ans += mo;
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
const int mo = 1000000007;
const int MAX = 100000;
using namespace std;
int p[100005], ny[100005], Hash[100005];
int ss[100005][30], n, f, q, i, j, ans;
int sumi(int a, int b) {
int ret = 1, c = a;
while (b) {
if (b & 1) ret = (long long)ret * c % mo;
c = (long long)c * c % mo;
b >>= 1;
}
return ret;
}
int C(int n, int m) {
if (n < m) return 0;
return (long long)p[n] * ny[m] % mo * ny[n - m] % mo;
}
void dfs(int i, int s, int odd) {
if (i > ss[n][0]) {
if (odd)
ans -= C(n / s - 1, f - 1);
else
ans += C(n / s - 1, f - 1);
ans %= mo;
return;
}
dfs(i + 1, s, odd);
dfs(i + 1, s * ss[n][i], 1 - odd);
}
int main() {
for (i = 2; i <= MAX; i++) {
if (Hash[i]) continue;
ss[i][++ss[i][0]] = i;
for (j = i + i; j <= MAX; j += i) {
ss[j][++ss[j][0]] = i;
Hash[j] = 1;
}
}
p[0] = ny[0] = 1;
for (i = 1; i <= MAX; i++) {
p[i] = (long long)p[i - 1] * i % mo;
ny[i] = (long long)ny[i - 1] * sumi(i, mo - 2) % mo;
}
scanf("%d", &q);
while (q--) {
scanf("%d %d", &n, &f);
if (n == 1) {
printf("1\n");
continue;
}
ans = 0;
dfs(1, 1, 0);
while (ans < 0) ans += mo;
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
map<pair<int, int>, long long> m;
map<pair<int, int>, long long>::iterator it;
long long fa[100007], ifa[100007];
long long bpow(long long x, long long n) {
long long res = 1;
while (n) {
if (n & 1) res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
long long C(int n, int k) {
return (((fa[n] * ifa[k]) % mod) * ifa[n - k]) % mod;
}
long long getans(int n, int k) {
if (n < k) return 0;
if (n == k) return 1;
if (k == 1) return 0;
it = m.find(make_pair(n, k));
if (it != m.end()) return (*it).second;
long long res = C(n - 1, k - 1);
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
if (i != 1) res = (res - getans(n / i, k) + mod) % mod;
if (i * i != n) res = (res - getans(i, k) + mod) % mod;
}
}
return m[make_pair(n, k)] = res;
}
int main() {
fa[0] = ifa[0] = 1;
for (int i = (1); i < (1e5 + 3); ++i) {
fa[i] = (fa[i - 1] * i) % mod;
ifa[i] = bpow(fa[i], mod - 2);
}
int q, n, f;
scanf("%d", &q);
while (q--) {
scanf("%d", &n);
scanf("%d", &f);
printf("%I64d\n", getans(n, f));
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
map<pair<int, int>, long long> m;
map<pair<int, int>, long long>::iterator it;
long long fa[100007], ifa[100007];
long long bpow(long long x, long long n) {
long long res = 1;
while (n) {
if (n & 1) res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
long long C(int n, int k) {
return (((fa[n] * ifa[k]) % mod) * ifa[n - k]) % mod;
}
long long getans(int n, int k) {
if (n < k) return 0;
if (n == k) return 1;
if (k == 1) return 0;
it = m.find(make_pair(n, k));
if (it != m.end()) return (*it).second;
long long res = C(n - 1, k - 1);
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
if (i != 1) res = (res - getans(n / i, k) + mod) % mod;
if (i * i != n) res = (res - getans(i, k) + mod) % mod;
}
}
return m[make_pair(n, k)] = res;
}
int main() {
fa[0] = ifa[0] = 1;
for (int i = (1); i < (1e5 + 3); ++i) {
fa[i] = (fa[i - 1] * i) % mod;
ifa[i] = bpow(fa[i], mod - 2);
}
int q, n, f;
scanf("%d", &q);
while (q--) {
scanf("%d", &n);
scanf("%d", &f);
printf("%I64d\n", getans(n, f));
}
return 0;
}
```
|
#include <bits/stdc++.h>
const double PI = acos(-1);
using namespace std;
int n, q, f;
vector<int> pr[100100];
bool pr1[100100];
long long fc[100100];
long long pw(long long x, long long p) {
if (p == 0) return 1;
long long z = pw(x, p / 2);
z *= z;
z %= 1000000007;
if (p % 2 == 0) return z;
z *= x;
z %= 1000000007;
return z;
}
long long inv(long long y) { return pw(y, 1000000007 - 2); }
long long c(long long x, long long y) {
if (x < y) swap(x, y);
long long r = fc[y] * fc[x - y];
r %= 1000000007;
r = fc[x] * inv(r);
r %= 1000000007;
return r;
}
long long mi(long long x) {
x %= 1000000007;
x += 1000000007;
x %= 1000000007;
return x;
}
int main() {
for (int i = 2; i < 100100; i++)
if (!pr1[i]) {
for (int j = i; j < 100100; j += i) {
pr1[j] = 1;
pr[j].push_back(i);
}
}
fc[0] = 1;
for (int i = 1; i < 100100; i++) {
fc[i] = fc[i - 1] * i;
fc[i] %= 1000000007;
}
cin >> q;
while (q--) {
scanf("%d%d", &n, &f);
long long res = c(n - 1, f - 1);
int en = 1 << pr[n].size();
for (int i = 1; i < en; i++) {
long long r1 = 1;
bool b = 0;
for (int j = 0; j < pr[n].size(); j++) {
if (i & (1 << j)) {
r1 *= pr[n][j];
b = !b;
}
}
if (n / r1 < f) continue;
if (b) {
res = mi(res - c(n / r1 - 1, f - 1));
} else {
res += c(n / r1 - 1, f - 1);
res %= 1000000007;
}
}
printf("%I64d\n", res);
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
const double PI = acos(-1);
using namespace std;
int n, q, f;
vector<int> pr[100100];
bool pr1[100100];
long long fc[100100];
long long pw(long long x, long long p) {
if (p == 0) return 1;
long long z = pw(x, p / 2);
z *= z;
z %= 1000000007;
if (p % 2 == 0) return z;
z *= x;
z %= 1000000007;
return z;
}
long long inv(long long y) { return pw(y, 1000000007 - 2); }
long long c(long long x, long long y) {
if (x < y) swap(x, y);
long long r = fc[y] * fc[x - y];
r %= 1000000007;
r = fc[x] * inv(r);
r %= 1000000007;
return r;
}
long long mi(long long x) {
x %= 1000000007;
x += 1000000007;
x %= 1000000007;
return x;
}
int main() {
for (int i = 2; i < 100100; i++)
if (!pr1[i]) {
for (int j = i; j < 100100; j += i) {
pr1[j] = 1;
pr[j].push_back(i);
}
}
fc[0] = 1;
for (int i = 1; i < 100100; i++) {
fc[i] = fc[i - 1] * i;
fc[i] %= 1000000007;
}
cin >> q;
while (q--) {
scanf("%d%d", &n, &f);
long long res = c(n - 1, f - 1);
int en = 1 << pr[n].size();
for (int i = 1; i < en; i++) {
long long r1 = 1;
bool b = 0;
for (int j = 0; j < pr[n].size(); j++) {
if (i & (1 << j)) {
r1 *= pr[n][j];
b = !b;
}
}
if (n / r1 < f) continue;
if (b) {
res = mi(res - c(n / r1 - 1, f - 1));
} else {
res += c(n / r1 - 1, f - 1);
res %= 1000000007;
}
}
printf("%I64d\n", res);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int f[100001];
long long int invf[100001];
int count_factors[100001];
vector<int> factors[100001];
bool isprime[100001];
long long int power(long long int b, long long int e) {
if (e == 0) {
return 1;
} else if (e == 1) {
return b;
} else {
long long int p = power(b, e / 2);
if (e % 2 == 0) {
return (p * p) % 1000000007;
} else {
p = (p * p) % 1000000007;
p = (p * b) % 1000000007;
return p;
}
}
}
void init() {
f[0] = 1LL;
for (int i = 1; i <= 100000; i++) {
f[i] = (i * f[i - 1]) % 1000000007;
}
invf[100000] = power(f[100000], 1000000007 - 2);
for (int i = 99999; i >= 1; i--) {
invf[i] = ((i + 1) * invf[i + 1]) % 1000000007;
}
invf[0] = 1LL;
}
void init1() {
for (int i = 2; i <= 100000; i++) {
isprime[i] = true;
}
for (int i = 2; i <= 100000; i++) {
if (isprime[i]) {
factors[i].push_back(i);
for (int j = i + i; j <= 100000; j = j + i) {
isprime[j] = false;
factors[j].push_back(i);
}
}
}
}
long long int nCr(long long int n, long long int r) {
if (r > n) {
return 0;
} else {
long long int temp = f[n];
temp = (temp * invf[r]) % 1000000007;
temp = (temp * invf[n - r]) % 1000000007;
return temp;
}
}
int main() {
int q;
scanf("%d", &q);
init();
init1();
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
if (f == 1 && n != 1) {
printf("0\n");
continue;
} else if (f == 1 && n == 1) {
printf("1\n");
continue;
}
long long int answer = 0;
int size1 = factors[n].size();
for (int i = 0; i < (1 << size1); i++) {
int bitcount = 0;
long long int temp1 = 1;
for (int j = 0; j < size1; j++) {
if (i & (1 << j)) {
bitcount++;
temp1 = temp1 * factors[n][j];
}
}
if (bitcount % 2 == 1) {
answer =
((answer - nCr(n / temp1 - 1, f - 1)) % 1000000007 + 1000000007) %
1000000007;
} else {
answer = (answer + nCr(n / temp1 - 1, f - 1)) % 1000000007;
}
}
printf("%lld\n", answer);
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int f[100001];
long long int invf[100001];
int count_factors[100001];
vector<int> factors[100001];
bool isprime[100001];
long long int power(long long int b, long long int e) {
if (e == 0) {
return 1;
} else if (e == 1) {
return b;
} else {
long long int p = power(b, e / 2);
if (e % 2 == 0) {
return (p * p) % 1000000007;
} else {
p = (p * p) % 1000000007;
p = (p * b) % 1000000007;
return p;
}
}
}
void init() {
f[0] = 1LL;
for (int i = 1; i <= 100000; i++) {
f[i] = (i * f[i - 1]) % 1000000007;
}
invf[100000] = power(f[100000], 1000000007 - 2);
for (int i = 99999; i >= 1; i--) {
invf[i] = ((i + 1) * invf[i + 1]) % 1000000007;
}
invf[0] = 1LL;
}
void init1() {
for (int i = 2; i <= 100000; i++) {
isprime[i] = true;
}
for (int i = 2; i <= 100000; i++) {
if (isprime[i]) {
factors[i].push_back(i);
for (int j = i + i; j <= 100000; j = j + i) {
isprime[j] = false;
factors[j].push_back(i);
}
}
}
}
long long int nCr(long long int n, long long int r) {
if (r > n) {
return 0;
} else {
long long int temp = f[n];
temp = (temp * invf[r]) % 1000000007;
temp = (temp * invf[n - r]) % 1000000007;
return temp;
}
}
int main() {
int q;
scanf("%d", &q);
init();
init1();
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
if (f == 1 && n != 1) {
printf("0\n");
continue;
} else if (f == 1 && n == 1) {
printf("1\n");
continue;
}
long long int answer = 0;
int size1 = factors[n].size();
for (int i = 0; i < (1 << size1); i++) {
int bitcount = 0;
long long int temp1 = 1;
for (int j = 0; j < size1; j++) {
if (i & (1 << j)) {
bitcount++;
temp1 = temp1 * factors[n][j];
}
}
if (bitcount % 2 == 1) {
answer =
((answer - nCr(n / temp1 - 1, f - 1)) % 1000000007 + 1000000007) %
1000000007;
} else {
answer = (answer + nCr(n / temp1 - 1, f - 1)) % 1000000007;
}
}
printf("%lld\n", answer);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long fac[110000], ifac[110000];
int p[110000], np, flag[110000];
long long Pow(long long x, long long y) {
if (y == 0) return 1;
long long res = Pow(x, y / 2);
res = res * res % 1000000007;
if (y & 1) res = res * x % 1000000007;
return res;
}
void prepare() {
int i, j;
fac[0] = 1;
for (i = 1; i < 110000; i++) fac[i] = fac[i - 1] * i % 1000000007;
for (i = 0; i < 110000; i++) ifac[i] = Pow(fac[i], 1000000007 - 2);
for (i = 2; i < 110000; i++) {
if (flag[i]) continue;
flag[i] = i;
p[np++] = i;
if (i > 1000) continue;
for (j = i * i; j < 110000; j += i) flag[j] = i;
}
}
int f[20], a[20], nf;
int d[1000], val[1000], nd;
void make(int dp, int cur) {
if (dp == nf) {
d[nd++] = cur;
return;
}
int i;
for (i = 0; i <= a[dp]; i++) {
make(dp + 1, cur);
cur *= f[dp];
}
}
int main() {
int T, N, F, i, j, x;
prepare();
for (scanf("%d", &T); T--;) {
scanf("%d%d", &N, &F);
nf = 0;
while (N > 1) {
f[nf] = flag[N];
a[nf] = 0;
while (N % f[nf] == 0) N /= f[nf], a[nf]++;
nf++;
}
nd = 0;
make(0, 1);
for (i = 0; i < nd; i++) val[i] = 0;
for (i = 0; i < nd; i++) {
if (d[i] < F) continue;
x = fac[d[i] - 1] * (ifac[F - 1] * ifac[d[i] - F] % 1000000007) %
1000000007;
val[i] = (val[i] + x) % 1000000007;
for (j = i + 1; j < nd; j++)
if (d[j] % d[i] == 0)
val[j] = (val[j] - val[i] + 1000000007) % 1000000007;
}
printf("%d\n", val[nd - 1]);
}
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long fac[110000], ifac[110000];
int p[110000], np, flag[110000];
long long Pow(long long x, long long y) {
if (y == 0) return 1;
long long res = Pow(x, y / 2);
res = res * res % 1000000007;
if (y & 1) res = res * x % 1000000007;
return res;
}
void prepare() {
int i, j;
fac[0] = 1;
for (i = 1; i < 110000; i++) fac[i] = fac[i - 1] * i % 1000000007;
for (i = 0; i < 110000; i++) ifac[i] = Pow(fac[i], 1000000007 - 2);
for (i = 2; i < 110000; i++) {
if (flag[i]) continue;
flag[i] = i;
p[np++] = i;
if (i > 1000) continue;
for (j = i * i; j < 110000; j += i) flag[j] = i;
}
}
int f[20], a[20], nf;
int d[1000], val[1000], nd;
void make(int dp, int cur) {
if (dp == nf) {
d[nd++] = cur;
return;
}
int i;
for (i = 0; i <= a[dp]; i++) {
make(dp + 1, cur);
cur *= f[dp];
}
}
int main() {
int T, N, F, i, j, x;
prepare();
for (scanf("%d", &T); T--;) {
scanf("%d%d", &N, &F);
nf = 0;
while (N > 1) {
f[nf] = flag[N];
a[nf] = 0;
while (N % f[nf] == 0) N /= f[nf], a[nf]++;
nf++;
}
nd = 0;
make(0, 1);
for (i = 0; i < nd; i++) val[i] = 0;
for (i = 0; i < nd; i++) {
if (d[i] < F) continue;
x = fac[d[i] - 1] * (ifac[F - 1] * ifac[d[i] - F] % 1000000007) %
1000000007;
val[i] = (val[i] + x) % 1000000007;
for (j = i + 1; j < nd; j++)
if (d[j] % d[i] == 0)
val[j] = (val[j] - val[i] + 1000000007) % 1000000007;
}
printf("%d\n", val[nd - 1]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
int color, vis[100009];
long long dp[100009];
long long F[100009], P[100009];
long long mod(long long x) {
while (x < 0) x += 1000000007;
return (x % 1000000007);
}
long long Fe(long long x, int y) {
if (!y) return 1LL;
long long h = Fe(x, y / 2);
h = mod(h * h);
if (y & 1) h = mod(h * x);
return h;
}
long long C(int x, int y) {
if (y > x) return 0;
return mod(mod(F[x] * P[y]) * P[x - y]);
}
vector<int> dv[100009];
long long rec(int x, int y) {
if (x <= y) return (x == y);
long long& ret = dp[x];
if (vis[x] == color) return ret;
vis[x] = color;
ret = C(x - 1, y - 1);
for (typeof((dv[x]).begin()) it = (dv[x]).begin(); it != (dv[x]).end(); it++)
ret = mod(ret - rec(x / (*it), y));
return ret;
}
int main() {
F[0] = P[0] = 1LL;
for (int i = 2; i < 100009; i++)
for (int j = i; j < 100009; j += i) dv[j].push_back(i);
for (int i = 1; i < 100009; i++) {
F[i] = mod(F[i - 1] * i);
P[i] = Fe(F[i], 1000000007 - 2);
}
int q;
scanf("%d", &q);
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
color++;
printf("%lld\n", rec(n, f));
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
int color, vis[100009];
long long dp[100009];
long long F[100009], P[100009];
long long mod(long long x) {
while (x < 0) x += 1000000007;
return (x % 1000000007);
}
long long Fe(long long x, int y) {
if (!y) return 1LL;
long long h = Fe(x, y / 2);
h = mod(h * h);
if (y & 1) h = mod(h * x);
return h;
}
long long C(int x, int y) {
if (y > x) return 0;
return mod(mod(F[x] * P[y]) * P[x - y]);
}
vector<int> dv[100009];
long long rec(int x, int y) {
if (x <= y) return (x == y);
long long& ret = dp[x];
if (vis[x] == color) return ret;
vis[x] = color;
ret = C(x - 1, y - 1);
for (typeof((dv[x]).begin()) it = (dv[x]).begin(); it != (dv[x]).end(); it++)
ret = mod(ret - rec(x / (*it), y));
return ret;
}
int main() {
F[0] = P[0] = 1LL;
for (int i = 2; i < 100009; i++)
for (int j = i; j < 100009; j += i) dv[j].push_back(i);
for (int i = 1; i < 100009; i++) {
F[i] = mod(F[i - 1] * i);
P[i] = Fe(F[i], 1000000007 - 2);
}
int q;
scanf("%d", &q);
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
color++;
printf("%lld\n", rec(n, f));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long ff[100100], inv_ff[100100];
int q, n, f, prime[100100], sqr[100100], mu[100100], ans;
vector<int> divi[100100];
long long inv_mod(long long x) {
long long ret = 1LL, y = MOD - 2;
while (y) {
if (y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD, y >>= 1;
}
return ret;
}
long long C(int x, int y) {
return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD;
}
long long solve(int x, int y) {
if (y > x) return 0;
return C(x - 1, y - 1);
}
void precompute() {
inv_ff[0] = 1LL;
ff[0] = 1LL;
for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD;
for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]);
for (int i = 1; i <= 100099; i++) {
for (int j = i; j <= 100099; j += i) {
divi[j].push_back(i);
}
}
for (int i = 0; i <= 100099; i++) prime[i] = 1;
prime[0] = 0, prime[1] = 0;
for (int i = 2; i <= 100099; i++) {
if (prime[i] == 1) {
for (int j = i + i; j <= 100099; j += i) prime[j] = 0;
}
}
for (int i = 2; i * i <= 100099; i++) {
int t = i * i;
for (int j = t; j <= 100099; j += t) sqr[j] = 1;
}
for (int i = 1; i <= 100099; i++) {
if (sqr[i] == 1) continue;
int cnt = 0;
for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++)
if (prime[*d] == 1) cnt++;
mu[i] = (cnt % 2 == 1) ? -1 : 1;
}
return;
}
int main() {
precompute();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
if (n == 1 and f == 1) {
printf("1\n");
continue;
}
if (n > 1 and f == 1) {
printf("0\n");
continue;
}
ans = 0;
for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end();
d++) {
ans = (ans + mu[*d] * solve(n / (*d), f)) % MOD;
if (ans < 0)
ans += MOD;
else if (ans >= MOD)
ans -= MOD;
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long ff[100100], inv_ff[100100];
int q, n, f, prime[100100], sqr[100100], mu[100100], ans;
vector<int> divi[100100];
long long inv_mod(long long x) {
long long ret = 1LL, y = MOD - 2;
while (y) {
if (y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD, y >>= 1;
}
return ret;
}
long long C(int x, int y) {
return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD;
}
long long solve(int x, int y) {
if (y > x) return 0;
return C(x - 1, y - 1);
}
void precompute() {
inv_ff[0] = 1LL;
ff[0] = 1LL;
for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD;
for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]);
for (int i = 1; i <= 100099; i++) {
for (int j = i; j <= 100099; j += i) {
divi[j].push_back(i);
}
}
for (int i = 0; i <= 100099; i++) prime[i] = 1;
prime[0] = 0, prime[1] = 0;
for (int i = 2; i <= 100099; i++) {
if (prime[i] == 1) {
for (int j = i + i; j <= 100099; j += i) prime[j] = 0;
}
}
for (int i = 2; i * i <= 100099; i++) {
int t = i * i;
for (int j = t; j <= 100099; j += t) sqr[j] = 1;
}
for (int i = 1; i <= 100099; i++) {
if (sqr[i] == 1) continue;
int cnt = 0;
for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++)
if (prime[*d] == 1) cnt++;
mu[i] = (cnt % 2 == 1) ? -1 : 1;
}
return;
}
int main() {
precompute();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
if (n == 1 and f == 1) {
printf("1\n");
continue;
}
if (n > 1 and f == 1) {
printf("0\n");
continue;
}
ans = 0;
for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end();
d++) {
ans = (ans + mu[*d] * solve(n / (*d), f)) % MOD;
if (ans < 0)
ans += MOD;
else if (ans >= MOD)
ans -= MOD;
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
const int mod = 1e9 + 7;
int a[101010];
int ft[202020];
int pmod(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (1ll * res * a) % mod;
a = (1ll * a * a) % mod;
b >>= 1;
}
return res;
}
int calc(int n, int m) {
if (m < 0) return 0;
return (1ll * ft[n + m - 1] *
pmod((1ll * ft[m] * ft[n - 1]) % mod, mod - 2)) %
mod;
}
std::map<std::pair<int, int>, int> st;
int solve(int n, int m) {
if (m < n) return 0;
if (st.find(std::pair<int, int>(n, m)) != st.end())
return st[std::pair<int, int>(n, m)];
int res = calc(n, m - n);
for (int i = 2; i * i <= m; ++i) {
if (m % i == 0) {
res = (res - solve(n, m / i)) % mod;
if (i * i < m) res = (res - solve(n, i)) % mod;
}
}
return st[std::pair<int, int>(n, m)] = (res % mod + mod) % mod;
}
int main() {
ft[0] = 1;
for (int i = 1; i <= 200000; ++i) ft[i] = (1ll * ft[i - 1] * i) % mod;
int tt;
for (scanf("%d", &tt); tt--;) {
int n, m;
scanf("%d %d", &m, &n);
if (n == 1) {
if (m == 1) {
puts("1");
} else {
puts("0");
}
} else {
int ans = solve(n, m);
printf("%d\n", ans);
}
}
}
|
### Prompt
Develop a solution in CPP to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
const int mod = 1e9 + 7;
int a[101010];
int ft[202020];
int pmod(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (1ll * res * a) % mod;
a = (1ll * a * a) % mod;
b >>= 1;
}
return res;
}
int calc(int n, int m) {
if (m < 0) return 0;
return (1ll * ft[n + m - 1] *
pmod((1ll * ft[m] * ft[n - 1]) % mod, mod - 2)) %
mod;
}
std::map<std::pair<int, int>, int> st;
int solve(int n, int m) {
if (m < n) return 0;
if (st.find(std::pair<int, int>(n, m)) != st.end())
return st[std::pair<int, int>(n, m)];
int res = calc(n, m - n);
for (int i = 2; i * i <= m; ++i) {
if (m % i == 0) {
res = (res - solve(n, m / i)) % mod;
if (i * i < m) res = (res - solve(n, i)) % mod;
}
}
return st[std::pair<int, int>(n, m)] = (res % mod + mod) % mod;
}
int main() {
ft[0] = 1;
for (int i = 1; i <= 200000; ++i) ft[i] = (1ll * ft[i - 1] * i) % mod;
int tt;
for (scanf("%d", &tt); tt--;) {
int n, m;
scanf("%d %d", &m, &n);
if (n == 1) {
if (m == 1) {
puts("1");
} else {
puts("0");
}
} else {
int ans = solve(n, m);
printf("%d\n", ans);
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
vector<int> P[N];
const int MOD = 1000000007;
long long d[N], e[N];
long long quick_pow(long long b, long long n) {
long long ret = 1;
while (n != 0) {
if (n % 2 == 1) ret = ret * b % MOD;
b = b * b % MOD;
n /= 2;
}
return ret;
}
long long C(long long n, long long 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;
long long F(int n) { return C(n - 1, f - 1); }
long long solve() {
long long 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;
}
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
vector<int> P[N];
const int MOD = 1000000007;
long long d[N], e[N];
long long quick_pow(long long b, long long n) {
long long ret = 1;
while (n != 0) {
if (n % 2 == 1) ret = ret * b % MOD;
b = b * b % MOD;
n /= 2;
}
return ret;
}
long long C(long long n, long long 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;
long long F(int n) { return C(n - 1, f - 1); }
long long solve() {
long long 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;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
const long long MOD = 1e9 + 7;
long long mpow(long long a, long long x) {
if (x == 0) return 1;
long long t = mpow(a, x >> 1);
if (x % 2 == 0) return t * t % MOD;
return t * t % MOD * a % MOD;
}
long long fac[N], inv[N], mu[N];
int q, n, f;
long long c(long long x, long long y) {
return fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
long long solve(long long d) {
if (n / d - 1 >= f - 1) return c(n / d - 1, f - 1);
return 0;
}
int main() {
fac[0] = 1, inv[0] = 1;
for (int i = 1; i < N; i++) fac[i] = fac[i - 1] * i % MOD;
for (int i = 1; i < N; i++) inv[i] = mpow(fac[i], MOD - 2);
mu[1] = 1;
for (int i = 1; i < N; i++) {
for (int j = i + i; j < N; j += i) {
mu[j] -= mu[i];
}
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
long long res = 0;
for (int d = 1; d * d <= n; d++) {
if (n % d == 0) {
res = (res + solve(d) * mu[d]) % MOD;
if (n / d != 1 && n / d != d)
res = (res + solve(n / d) * mu[n / d]) % MOD;
}
}
res = (res % MOD + MOD) % MOD;
printf("%lld\n", res);
}
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
const long long MOD = 1e9 + 7;
long long mpow(long long a, long long x) {
if (x == 0) return 1;
long long t = mpow(a, x >> 1);
if (x % 2 == 0) return t * t % MOD;
return t * t % MOD * a % MOD;
}
long long fac[N], inv[N], mu[N];
int q, n, f;
long long c(long long x, long long y) {
return fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
long long solve(long long d) {
if (n / d - 1 >= f - 1) return c(n / d - 1, f - 1);
return 0;
}
int main() {
fac[0] = 1, inv[0] = 1;
for (int i = 1; i < N; i++) fac[i] = fac[i - 1] * i % MOD;
for (int i = 1; i < N; i++) inv[i] = mpow(fac[i], MOD - 2);
mu[1] = 1;
for (int i = 1; i < N; i++) {
for (int j = i + i; j < N; j += i) {
mu[j] -= mu[i];
}
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
long long res = 0;
for (int d = 1; d * d <= n; d++) {
if (n % d == 0) {
res = (res + solve(d) * mu[d]) % MOD;
if (n / d != 1 && n / d != d)
res = (res + solve(n / d) * mu[n / d]) % MOD;
}
}
res = (res % MOD + MOD) % MOD;
printf("%lld\n", res);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(2e5 + 123);
const long long mod = (long long)(1e9 + 7);
bool u[maxn];
int pn, p[maxn];
void resheto(int N) {
for (int i = 3; i * i <= N; i += 2)
if (!u[i])
for (int j = i * i; j <= N; j += 2 * i) u[j] = 1;
p[pn++] = 2;
for (int i = 3; i <= N; i += 2)
if (!u[i]) p[pn++] = i;
}
inline long long binpow(long long a, long long b, long long c) {
if (!b) return 1;
long long res = binpow(a, b >> 1, c);
res = res * res % c;
if (b & 1) res = res * a % c;
return res;
}
long long add(long long a, long long b) {
long long c = a + b;
if (c >= mod) return c - mod;
return a + b;
}
long long sub(long long a, long long b) {
long long c = a - b;
if (c < 0) return c + mod;
return c;
}
int q, n[maxn], f[maxn], m[maxn], d[maxn][50];
void dividers(int k) {
int N = n[k];
for (int i = 0; i < pn; ++i) {
if (n[k] % p[i]) continue;
if (n[k] / p[i] >= f[k]) {
d[k][m[k]++] = p[i];
}
while (N % p[i] == 0) {
N /= p[i];
}
}
if (N != 1 && n[k] / N >= f[k]) d[k][m[k]++] = N;
}
long long fact[maxn];
long long cnk(int n, int k) {
if (n < k) return 0;
long long val = fact[n - k] * fact[k] % mod;
return fact[n] * binpow(val, mod - 2, mod) % mod;
}
int main() {
resheto(1000);
fact[0] = 1;
for (int i = 1; i < maxn; ++i) {
fact[i] = fact[i - 1] * i % mod;
}
scanf("%d", &q);
for (int i = 0; i < q; ++i) {
scanf("%d %d", &n[i], &f[i]);
dividers(i);
int m_ = m[i];
long long ans = 0;
if (f[i] == 1) {
if (n[i] == 1) ans = 1;
} else {
for (int mask = 0; mask < (1 << m_); ++mask) {
int a = 1, cnt = 0;
for (int j = 0; j < m_; ++j) {
if ((mask >> j) & 1) {
a *= d[i][j];
cnt++;
}
}
long long C = cnk(n[i] / a - 1, f[i] - 1);
if (cnt & 1)
ans = sub(ans, C);
else
ans = add(ans, C);
}
}
printf("%I64d\n", ans);
}
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(2e5 + 123);
const long long mod = (long long)(1e9 + 7);
bool u[maxn];
int pn, p[maxn];
void resheto(int N) {
for (int i = 3; i * i <= N; i += 2)
if (!u[i])
for (int j = i * i; j <= N; j += 2 * i) u[j] = 1;
p[pn++] = 2;
for (int i = 3; i <= N; i += 2)
if (!u[i]) p[pn++] = i;
}
inline long long binpow(long long a, long long b, long long c) {
if (!b) return 1;
long long res = binpow(a, b >> 1, c);
res = res * res % c;
if (b & 1) res = res * a % c;
return res;
}
long long add(long long a, long long b) {
long long c = a + b;
if (c >= mod) return c - mod;
return a + b;
}
long long sub(long long a, long long b) {
long long c = a - b;
if (c < 0) return c + mod;
return c;
}
int q, n[maxn], f[maxn], m[maxn], d[maxn][50];
void dividers(int k) {
int N = n[k];
for (int i = 0; i < pn; ++i) {
if (n[k] % p[i]) continue;
if (n[k] / p[i] >= f[k]) {
d[k][m[k]++] = p[i];
}
while (N % p[i] == 0) {
N /= p[i];
}
}
if (N != 1 && n[k] / N >= f[k]) d[k][m[k]++] = N;
}
long long fact[maxn];
long long cnk(int n, int k) {
if (n < k) return 0;
long long val = fact[n - k] * fact[k] % mod;
return fact[n] * binpow(val, mod - 2, mod) % mod;
}
int main() {
resheto(1000);
fact[0] = 1;
for (int i = 1; i < maxn; ++i) {
fact[i] = fact[i - 1] * i % mod;
}
scanf("%d", &q);
for (int i = 0; i < q; ++i) {
scanf("%d %d", &n[i], &f[i]);
dividers(i);
int m_ = m[i];
long long ans = 0;
if (f[i] == 1) {
if (n[i] == 1) ans = 1;
} else {
for (int mask = 0; mask < (1 << m_); ++mask) {
int a = 1, cnt = 0;
for (int j = 0; j < m_; ++j) {
if ((mask >> j) & 1) {
a *= d[i][j];
cnt++;
}
}
long long C = cnk(n[i] / a - 1, f[i] - 1);
if (cnt & 1)
ans = sub(ans, C);
else
ans = add(ans, C);
}
}
printf("%I64d\n", ans);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
const int mod = 1e9 + 7;
int q, n, f;
map<int, int> d[maxn + 1];
int fac[maxn + 1], rev[maxn + 1];
int pow(int a, int p) {
int ans = 1;
while (p) {
if (p & 1) {
ans = (1ll * ans * a) % mod;
}
a = (1ll * a * a) % mod;
p >>= 1;
}
return ans;
}
void init() {
fac[0] = 1;
rev[0] = 1;
for (int i = 1; i <= maxn; i++) {
fac[i] = (1ll * fac[i - 1] * i) % mod;
rev[i] = pow(fac[i], mod - 2);
}
}
int comb(int a, int b) {
if (a == b) return 1;
if (a < b) return 0;
return (1ll * ((1ll * fac[a] * rev[b]) % mod) * rev[a - b]) % mod;
}
int dp(int n, int f) {
if (f > n) return 0;
if (f == n) return 1;
if (f == 1) return n == 1;
if (d[n].count(f)) return d[n][f];
long long ans = comb(n - 1, f - 1);
int i;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
ans = ans - dp(n / i, f);
if (ans < 0) ans += mod;
if (i * i == n) break;
ans = ans - dp(i, f);
if (ans < 0) ans += mod;
}
}
return d[n][f] = (int)(ans % mod);
}
int main() {
init();
int q;
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int n, f;
scanf("%d%d", &n, &f);
printf("%d\n", (dp(n, f) + mod) % mod);
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
const int mod = 1e9 + 7;
int q, n, f;
map<int, int> d[maxn + 1];
int fac[maxn + 1], rev[maxn + 1];
int pow(int a, int p) {
int ans = 1;
while (p) {
if (p & 1) {
ans = (1ll * ans * a) % mod;
}
a = (1ll * a * a) % mod;
p >>= 1;
}
return ans;
}
void init() {
fac[0] = 1;
rev[0] = 1;
for (int i = 1; i <= maxn; i++) {
fac[i] = (1ll * fac[i - 1] * i) % mod;
rev[i] = pow(fac[i], mod - 2);
}
}
int comb(int a, int b) {
if (a == b) return 1;
if (a < b) return 0;
return (1ll * ((1ll * fac[a] * rev[b]) % mod) * rev[a - b]) % mod;
}
int dp(int n, int f) {
if (f > n) return 0;
if (f == n) return 1;
if (f == 1) return n == 1;
if (d[n].count(f)) return d[n][f];
long long ans = comb(n - 1, f - 1);
int i;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
ans = ans - dp(n / i, f);
if (ans < 0) ans += mod;
if (i * i == n) break;
ans = ans - dp(i, f);
if (ans < 0) ans += mod;
}
}
return d[n][f] = (int)(ans % mod);
}
int main() {
init();
int q;
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int n, f;
scanf("%d%d", &n, &f);
printf("%d\n", (dp(n, f) + mod) % mod);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 100111;
const long long MOD = 1000000007;
long long power(long long x, long long k) {
if (k == 0) return 1;
if (k == 1) return x % MOD;
long long mid = power(x, k >> 1);
mid = (mid * mid) % MOD;
if (k & 1)
return mid * x % MOD;
else
return mid;
}
inline long long inverse(long long x) { return power(x, MOD - 2); }
long long gt[MN], rev_gt[MN];
long long C(int n, int k) {
if (k < 0 || k > n) return 0;
return (gt[n] * inverse(gt[k])) % MOD * inverse(gt[n - k]) % MOD;
}
int sieve[MN], cnt[MN];
void init() {
sieve[1] = 1;
for (int i = 2; i * i <= 100000; ++i)
if (!sieve[i]) {
int j = i * i;
while (j < MN) {
sieve[j] = i;
j += i;
}
}
cnt[1] = 0;
for (int i = (2), _b = (100000); i <= _b; i++) {
int u = i;
while (sieve[u]) {
int t = u / sieve[u];
if (t % sieve[u] == 0) {
cnt[i] = -1;
break;
}
u = t;
++cnt[i];
}
if (cnt[i] >= 0) ++cnt[i];
}
}
void update(long long &res, int n, int f, int x) {
if (cnt[x] < 0) return;
if (cnt[x] % 2 == 0)
res = (res + C(n - 1, f - 1)) % MOD;
else
res = (res - C(n - 1, f - 1) + MOD) % MOD;
}
int main() {
ios ::sync_with_stdio(false);
gt[0] = 1;
for (int i = (1), _b = (100000); i <= _b; i++) gt[i] = (gt[i - 1] * i) % MOD;
for (int i = (0), _b = (100000); i <= _b; i++) rev_gt[i] = inverse(gt[i]);
init();
int ntest;
cin >> ntest;
for (int test = (1), _b = (ntest); test <= _b; test++) {
int n, f;
cin >> n >> f;
if (f == 1) {
if (n == 1)
cout << 1 << endl;
else
cout << 0 << endl;
continue;
}
long long res = 0;
for (int i = 1; i * i <= n; ++i)
if (n % i == 0) {
update(res, n / i, f, i);
if (i * i != n) update(res, i, f, n / i);
}
cout << res % MOD << endl;
}
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MN = 100111;
const long long MOD = 1000000007;
long long power(long long x, long long k) {
if (k == 0) return 1;
if (k == 1) return x % MOD;
long long mid = power(x, k >> 1);
mid = (mid * mid) % MOD;
if (k & 1)
return mid * x % MOD;
else
return mid;
}
inline long long inverse(long long x) { return power(x, MOD - 2); }
long long gt[MN], rev_gt[MN];
long long C(int n, int k) {
if (k < 0 || k > n) return 0;
return (gt[n] * inverse(gt[k])) % MOD * inverse(gt[n - k]) % MOD;
}
int sieve[MN], cnt[MN];
void init() {
sieve[1] = 1;
for (int i = 2; i * i <= 100000; ++i)
if (!sieve[i]) {
int j = i * i;
while (j < MN) {
sieve[j] = i;
j += i;
}
}
cnt[1] = 0;
for (int i = (2), _b = (100000); i <= _b; i++) {
int u = i;
while (sieve[u]) {
int t = u / sieve[u];
if (t % sieve[u] == 0) {
cnt[i] = -1;
break;
}
u = t;
++cnt[i];
}
if (cnt[i] >= 0) ++cnt[i];
}
}
void update(long long &res, int n, int f, int x) {
if (cnt[x] < 0) return;
if (cnt[x] % 2 == 0)
res = (res + C(n - 1, f - 1)) % MOD;
else
res = (res - C(n - 1, f - 1) + MOD) % MOD;
}
int main() {
ios ::sync_with_stdio(false);
gt[0] = 1;
for (int i = (1), _b = (100000); i <= _b; i++) gt[i] = (gt[i - 1] * i) % MOD;
for (int i = (0), _b = (100000); i <= _b; i++) rev_gt[i] = inverse(gt[i]);
init();
int ntest;
cin >> ntest;
for (int test = (1), _b = (ntest); test <= _b; test++) {
int n, f;
cin >> n >> f;
if (f == 1) {
if (n == 1)
cout << 1 << endl;
else
cout << 0 << endl;
continue;
}
long long res = 0;
for (int i = 1; i * i <= n; ++i)
if (n % i == 0) {
update(res, n / i, f, i);
if (i * i != n) update(res, i, f, n / i);
}
cout << res % MOD << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
const int M = 100005;
long long Fac[M], Ifac[M];
int mu[M], Son[M][67], Sz[M];
int _, n, k;
int main() {
Ifac[0] = Ifac[1] = 1;
for (int i = Fac[0] = 1; i < M; ++i) Fac[i] = i * Fac[i - 1] % 1000000007;
for (int i = 2; i < M; ++i)
Ifac[i] = -1000000007 / i * Ifac[1000000007 % i] % 1000000007;
for (int i = 1; i < M; ++i) Ifac[i] = Ifac[i] * Ifac[i - 1] % 1000000007;
for (int i = mu[1] = 1; i < M; ++i)
if (mu[i]) {
Son[i][Sz[i]++] = i;
for (int j = i + i; j < M; j += i) mu[j] -= mu[i], Son[j][Sz[j]++] = i;
}
for (scanf("%d", &_); _; --_) {
scanf("%d %d", &n, &k);
long long ans = 0;
for (int i = 0; i < Sz[n]; ++i) {
int x = Son[n][i], y = n / x;
if (y < k) break;
ans += mu[x] * Fac[y - 1] * Ifac[k - 1] % 1000000007 * Ifac[y - k] %
1000000007;
}
printf("%lld\n", (ans % 1000000007 + 1000000007) % 1000000007);
}
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
const int M = 100005;
long long Fac[M], Ifac[M];
int mu[M], Son[M][67], Sz[M];
int _, n, k;
int main() {
Ifac[0] = Ifac[1] = 1;
for (int i = Fac[0] = 1; i < M; ++i) Fac[i] = i * Fac[i - 1] % 1000000007;
for (int i = 2; i < M; ++i)
Ifac[i] = -1000000007 / i * Ifac[1000000007 % i] % 1000000007;
for (int i = 1; i < M; ++i) Ifac[i] = Ifac[i] * Ifac[i - 1] % 1000000007;
for (int i = mu[1] = 1; i < M; ++i)
if (mu[i]) {
Son[i][Sz[i]++] = i;
for (int j = i + i; j < M; j += i) mu[j] -= mu[i], Son[j][Sz[j]++] = i;
}
for (scanf("%d", &_); _; --_) {
scanf("%d %d", &n, &k);
long long ans = 0;
for (int i = 0; i < Sz[n]; ++i) {
int x = Son[n][i], y = n / x;
if (y < k) break;
ans += mu[x] * Fac[y - 1] * Ifac[k - 1] % 1000000007 * Ifac[y - k] %
1000000007;
}
printf("%lld\n", (ans % 1000000007 + 1000000007) % 1000000007);
}
return 0;
}
```
|
#include <bits/stdc++.h>
long long N = 100005;
namespace mu {
long long mu[100005], vis[100005], prim[100005], cnt;
inline long long read() {
long long num = 0, f = 1;
char c = getchar();
while (c < 48 || c > 57) {
if (c == '-') f = -1;
c = getchar();
}
while (c >= 48 && c <= 57)
num = (num << 3) + (num << 1) + (c ^ 48), c = getchar();
return num * f;
}
void M(long long n) {
mu[1] = 1;
for (long long i = 2; i <= n; i++) {
if (!vis[i]) prim[++cnt] = i, mu[i] = -1;
for (long long j = 1; j <= cnt; j++) {
if (prim[j] * i > n) break;
vis[prim[j] * i] = 1;
if (i % prim[j] == 0) break;
mu[i * prim[j]] = -mu[i];
}
}
}
long long N = 100005;
std::unordered_map<long long, long long> sum;
long long Getsum(long long x) {
if (x <= N) return mu[x];
if (sum[x]) return sum[x];
long long res = 1;
for (long long l = 2, r = x; l <= x; l = r + 1) {
r = x / (x / l);
res -= (r - l + 1) * Getsum(x / l);
}
return sum[x] = res;
}
}; // namespace mu
inline long long qp(long long x, long long p) {
long long res = 1;
while (p) {
if (p & 1) res = res * x % 1000000007;
x = x * x % 1000000007;
p >>= 1;
}
return res;
}
long long fac[10000005];
inline long long slv(long long q, long long f) {
if (q < f) return 0;
q--, f--;
return fac[q] * qp(fac[f], 1000000007 - 2) % 1000000007 *
qp(fac[q - f], 1000000007 - 2);
}
signed main() {
long long T = mu::read(), ans = 0;
mu::M(N);
fac[0] = 1;
for (long long i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % 1000000007;
while (T--) {
long long n = mu::read(), f = mu::read(), ans = 0;
for (long long i = 1; i * i <= n; i++) {
if (n % i) continue;
ans = (ans + mu::mu[i] * slv(n / i, f)) % 1000000007;
if (i * i != n) ans = (ans + mu::mu[n / i] * slv(i, f)) % 1000000007;
}
printf("%lld\n", ans < 0 ? ans + 1000000007 : ans);
}
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
long long N = 100005;
namespace mu {
long long mu[100005], vis[100005], prim[100005], cnt;
inline long long read() {
long long num = 0, f = 1;
char c = getchar();
while (c < 48 || c > 57) {
if (c == '-') f = -1;
c = getchar();
}
while (c >= 48 && c <= 57)
num = (num << 3) + (num << 1) + (c ^ 48), c = getchar();
return num * f;
}
void M(long long n) {
mu[1] = 1;
for (long long i = 2; i <= n; i++) {
if (!vis[i]) prim[++cnt] = i, mu[i] = -1;
for (long long j = 1; j <= cnt; j++) {
if (prim[j] * i > n) break;
vis[prim[j] * i] = 1;
if (i % prim[j] == 0) break;
mu[i * prim[j]] = -mu[i];
}
}
}
long long N = 100005;
std::unordered_map<long long, long long> sum;
long long Getsum(long long x) {
if (x <= N) return mu[x];
if (sum[x]) return sum[x];
long long res = 1;
for (long long l = 2, r = x; l <= x; l = r + 1) {
r = x / (x / l);
res -= (r - l + 1) * Getsum(x / l);
}
return sum[x] = res;
}
}; // namespace mu
inline long long qp(long long x, long long p) {
long long res = 1;
while (p) {
if (p & 1) res = res * x % 1000000007;
x = x * x % 1000000007;
p >>= 1;
}
return res;
}
long long fac[10000005];
inline long long slv(long long q, long long f) {
if (q < f) return 0;
q--, f--;
return fac[q] * qp(fac[f], 1000000007 - 2) % 1000000007 *
qp(fac[q - f], 1000000007 - 2);
}
signed main() {
long long T = mu::read(), ans = 0;
mu::M(N);
fac[0] = 1;
for (long long i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % 1000000007;
while (T--) {
long long n = mu::read(), f = mu::read(), ans = 0;
for (long long i = 1; i * i <= n; i++) {
if (n % i) continue;
ans = (ans + mu::mu[i] * slv(n / i, f)) % 1000000007;
if (i * i != n) ans = (ans + mu::mu[n / i] * slv(i, f)) % 1000000007;
}
printf("%lld\n", ans < 0 ? ans + 1000000007 : ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int popcount(T t) {
if (std::numeric_limits<T>::digits <=
std::numeric_limits<unsigned int>::digits) {
return __builtin_popcount(t);
} else {
return __builtin_popcountll(t);
}
}
const long double EPS = 1e-8;
const long double PI = acosl(0.0) * 2;
const long long MOD = 1000000007;
const int NMAX = 100400;
long long fact[NMAX];
long long pow(long long a, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret = ret * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return ret;
}
long long inv(long long a) { return pow(a, MOD - 2); }
long long c(long long n, long long k) {
return fact[n] * inv(fact[k]) % MOD * inv(fact[n - k]) % MOD;
}
vector<int> getPrimes(int n) {
vector<int> ret;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
while (n % i == 0) n /= i;
ret.push_back(i);
}
}
if (n > 1) ret.push_back(n);
return ret;
}
long long calc(long long n, long long f) {
if (n < f) return 0;
n -= f;
return c(n + f - 1, f - 1);
}
void solve() {
fact[0] = 1;
for (int i = 1; i <= int(NMAX - 1); ++i) fact[i] = fact[i - 1] * i % MOD;
int q;
cin >> q;
for (int it = 0; it < int(q); ++it) {
int n, f;
cin >> n >> f;
long long ans = 0;
vector<int> primes = getPrimes(n);
int m = primes.size();
for (int mask = 0; mask < int(1 << m); ++mask) {
int sign = (popcount(mask) % 2 == 0) ? 1 : -1;
long long div = 1;
for (int i = 0; i < int(m); ++i)
if (mask & (1 << i)) div *= primes[i];
assert(n % div == 0);
ans += sign * calc(n / div, f);
ans = (ans + MOD) % MOD;
}
cout << ans << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int popcount(T t) {
if (std::numeric_limits<T>::digits <=
std::numeric_limits<unsigned int>::digits) {
return __builtin_popcount(t);
} else {
return __builtin_popcountll(t);
}
}
const long double EPS = 1e-8;
const long double PI = acosl(0.0) * 2;
const long long MOD = 1000000007;
const int NMAX = 100400;
long long fact[NMAX];
long long pow(long long a, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret = ret * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return ret;
}
long long inv(long long a) { return pow(a, MOD - 2); }
long long c(long long n, long long k) {
return fact[n] * inv(fact[k]) % MOD * inv(fact[n - k]) % MOD;
}
vector<int> getPrimes(int n) {
vector<int> ret;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
while (n % i == 0) n /= i;
ret.push_back(i);
}
}
if (n > 1) ret.push_back(n);
return ret;
}
long long calc(long long n, long long f) {
if (n < f) return 0;
n -= f;
return c(n + f - 1, f - 1);
}
void solve() {
fact[0] = 1;
for (int i = 1; i <= int(NMAX - 1); ++i) fact[i] = fact[i - 1] * i % MOD;
int q;
cin >> q;
for (int it = 0; it < int(q); ++it) {
int n, f;
cin >> n >> f;
long long ans = 0;
vector<int> primes = getPrimes(n);
int m = primes.size();
for (int mask = 0; mask < int(1 << m); ++mask) {
int sign = (popcount(mask) % 2 == 0) ? 1 : -1;
long long div = 1;
for (int i = 0; i < int(m); ++i)
if (mask & (1 << i)) div *= primes[i];
assert(n % div == 0);
ans += sign * calc(n / div, f);
ans = (ans + MOD) % MOD;
}
cout << ans << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, mod = 1e9 + 7;
long long p[maxn];
long long pv[maxn];
int t;
int cnt[maxn];
long long dp[maxn];
int n, m;
long long pow_mod(long long a, long long n) {
if (!n) return 1;
long long ret = pow_mod(a, n / 2);
ret = ret * ret % mod;
if (n & 1) ret = ret * a % mod;
return ret;
}
void init() {
p[0] = 1;
pv[0] = 1;
for (int i = 1; i < maxn; i++) {
p[i] = p[i - 1] * i % mod;
pv[i] = pow_mod(p[i], mod - 2);
}
}
long long f(int n) {
if (n < m) return 0;
if (n == m) return 1;
if (cnt[n] == t) return dp[n];
cnt[n] = t;
dp[n] = p[n - 1] * pv[n - m] % mod * pv[m - 1] % mod;
int M = sqrt(n + 0.5);
for (int i = 2; i <= M; i++) {
if (n % i) continue;
dp[n] = (dp[n] + mod - f(i)) % mod;
if (n != i * i) dp[n] = (dp[n] + mod - f(n / i)) % mod;
}
return dp[n];
}
int main() {
init();
int q;
scanf("%d", &q);
while (q--) {
t++;
scanf("%d%d", &n, &m);
if (m == 1) {
puts(n == 1 ? "1" : "0");
continue;
}
printf("%I64d\n", f(n));
}
}
|
### Prompt
Develop a solution in CPP to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, mod = 1e9 + 7;
long long p[maxn];
long long pv[maxn];
int t;
int cnt[maxn];
long long dp[maxn];
int n, m;
long long pow_mod(long long a, long long n) {
if (!n) return 1;
long long ret = pow_mod(a, n / 2);
ret = ret * ret % mod;
if (n & 1) ret = ret * a % mod;
return ret;
}
void init() {
p[0] = 1;
pv[0] = 1;
for (int i = 1; i < maxn; i++) {
p[i] = p[i - 1] * i % mod;
pv[i] = pow_mod(p[i], mod - 2);
}
}
long long f(int n) {
if (n < m) return 0;
if (n == m) return 1;
if (cnt[n] == t) return dp[n];
cnt[n] = t;
dp[n] = p[n - 1] * pv[n - m] % mod * pv[m - 1] % mod;
int M = sqrt(n + 0.5);
for (int i = 2; i <= M; i++) {
if (n % i) continue;
dp[n] = (dp[n] + mod - f(i)) % mod;
if (n != i * i) dp[n] = (dp[n] + mod - f(n / i)) % mod;
}
return dp[n];
}
int main() {
init();
int q;
scanf("%d", &q);
while (q--) {
t++;
scanf("%d%d", &n, &m);
if (m == 1) {
puts(n == 1 ? "1" : "0");
continue;
}
printf("%I64d\n", f(n));
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double eps(1e-8);
const double pi(3.14159265358979);
const int mod = 1000000007, N = 200200, MaxN = 200000;
long long d[N] = {1}, re[N] = {0, 1}, red[N] = {1, 1};
int prime[N] = {}, ptot = 0, minp[N] = {}, n, f;
int a[N] = {}, tot = 0;
void init() {
bool v[N] = {};
for (int i = 2; i <= MaxN; ++i) {
if (!v[i]) prime[++ptot] = i, minp[i] = i;
for (int j = 1; j <= ptot && i * prime[j] <= MaxN; ++j) {
v[i * prime[j]] = true;
minp[i * prime[j]] = prime[j];
if (i % prime[j] == 0) break;
}
}
for (int i = 1; i <= MaxN; ++i) d[i] = d[i - 1] * i % mod;
for (int i = 2; i <= MaxN; ++i) {
long long a = mod % i, b = mod / i;
re[i] = ((-b * re[a]) % mod + mod) % mod;
red[i] = red[i - 1] * re[i] % mod;
}
}
void get_factor(int x) {
while (x != 1) {
if (minp[x] != a[tot]) a[++tot] = minp[x];
x /= minp[x];
}
}
long long tryy(int t, int x) {
if (t > tot) {
if (n / x < f) return 0ll;
int x1 = n / x - 1, x2 = f - 1;
long long s = d[x1] * red[x2] % mod * red[x1 - x2] % mod;
return s;
}
long long s1 = tryy(t + 1, x);
long long s2 = tryy(t + 1, x * a[t]);
return (s1 - s2 + mod) % mod;
}
void work() {
tot = 0;
get_factor(n);
printf("%I64d\n", tryy(1, 1));
}
int main() {
init();
int q;
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
work();
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double eps(1e-8);
const double pi(3.14159265358979);
const int mod = 1000000007, N = 200200, MaxN = 200000;
long long d[N] = {1}, re[N] = {0, 1}, red[N] = {1, 1};
int prime[N] = {}, ptot = 0, minp[N] = {}, n, f;
int a[N] = {}, tot = 0;
void init() {
bool v[N] = {};
for (int i = 2; i <= MaxN; ++i) {
if (!v[i]) prime[++ptot] = i, minp[i] = i;
for (int j = 1; j <= ptot && i * prime[j] <= MaxN; ++j) {
v[i * prime[j]] = true;
minp[i * prime[j]] = prime[j];
if (i % prime[j] == 0) break;
}
}
for (int i = 1; i <= MaxN; ++i) d[i] = d[i - 1] * i % mod;
for (int i = 2; i <= MaxN; ++i) {
long long a = mod % i, b = mod / i;
re[i] = ((-b * re[a]) % mod + mod) % mod;
red[i] = red[i - 1] * re[i] % mod;
}
}
void get_factor(int x) {
while (x != 1) {
if (minp[x] != a[tot]) a[++tot] = minp[x];
x /= minp[x];
}
}
long long tryy(int t, int x) {
if (t > tot) {
if (n / x < f) return 0ll;
int x1 = n / x - 1, x2 = f - 1;
long long s = d[x1] * red[x2] % mod * red[x1 - x2] % mod;
return s;
}
long long s1 = tryy(t + 1, x);
long long s2 = tryy(t + 1, x * a[t]);
return (s1 - s2 + mod) % mod;
}
void work() {
tot = 0;
get_factor(n);
printf("%I64d\n", tryy(1, 1));
}
int main() {
init();
int q;
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
work();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmax(T &x, T y) {
if (x < y) x = y;
}
template <class T>
inline void chkmin(T &x, T y) {
if (x > y) x = y;
}
int q, n, f, inv[100100], fac[100100], p[6];
void Euclid(int a, int b, int &x, int &y) {
if (!a)
x = 0, y = 1;
else {
int q = b / a, r = b % a;
Euclid(r, a, y, x);
x -= q * y;
}
}
int cal(int n, int m) {
if (m > n) return 0;
return 1ll * fac[n - 1] * inv[m - 1] % 1000000007 * inv[n - m] % 1000000007;
}
int main() {
fac[0] = 1;
for (int i = 1; i < 100100; i++) fac[i] = 1ll * fac[i - 1] * i % 1000000007;
int x, y;
Euclid(fac[100100 - 1], 1000000007, x, y);
inv[100100 - 1] = x;
for (int i = 100100 - 1; i > 0; i--)
inv[i - 1] = 1ll * inv[i] * i % 1000000007;
scanf("%d", &q);
while (q--) {
scanf("%d %d", &n, &f);
x = 0, y = n;
for (int i = 2; i <= sqrt(y); i++) {
if (y % i) continue;
p[x++] = i;
while (y % i == 0) y /= i;
}
if (y > 1) p[x++] = y;
y = 0;
for (int i = 0; i < (1 << x); i++) {
int tmp = n, cnt = 0;
for (int j = 0; j < x; j++)
if (i & (1 << j)) cnt++, tmp /= p[j];
if (cnt & 1)
y = (y - cal(tmp, f) + 1000000007) % 1000000007;
else
y = (y + cal(tmp, f)) % 1000000007;
}
printf("%d\n", y);
}
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmax(T &x, T y) {
if (x < y) x = y;
}
template <class T>
inline void chkmin(T &x, T y) {
if (x > y) x = y;
}
int q, n, f, inv[100100], fac[100100], p[6];
void Euclid(int a, int b, int &x, int &y) {
if (!a)
x = 0, y = 1;
else {
int q = b / a, r = b % a;
Euclid(r, a, y, x);
x -= q * y;
}
}
int cal(int n, int m) {
if (m > n) return 0;
return 1ll * fac[n - 1] * inv[m - 1] % 1000000007 * inv[n - m] % 1000000007;
}
int main() {
fac[0] = 1;
for (int i = 1; i < 100100; i++) fac[i] = 1ll * fac[i - 1] * i % 1000000007;
int x, y;
Euclid(fac[100100 - 1], 1000000007, x, y);
inv[100100 - 1] = x;
for (int i = 100100 - 1; i > 0; i--)
inv[i - 1] = 1ll * inv[i] * i % 1000000007;
scanf("%d", &q);
while (q--) {
scanf("%d %d", &n, &f);
x = 0, y = n;
for (int i = 2; i <= sqrt(y); i++) {
if (y % i) continue;
p[x++] = i;
while (y % i == 0) y /= i;
}
if (y > 1) p[x++] = y;
y = 0;
for (int i = 0; i < (1 << x); i++) {
int tmp = n, cnt = 0;
for (int j = 0; j < x; j++)
if (i & (1 << j)) cnt++, tmp /= p[j];
if (cnt & 1)
y = (y - cal(tmp, f) + 1000000007) % 1000000007;
else
y = (y + cal(tmp, f)) % 1000000007;
}
printf("%d\n", y);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int limite = 100001;
long long int modulo = 1e9 + 7;
long long int eleva(long long int b, long long int e) {
if (e == 0) return 1;
if (e % 2 == 0) return eleva(b * b % modulo, e / 2);
return b * eleva(b * b % modulo, e / 2) % modulo;
}
long long int inverso(long long int x) { return eleva(x, modulo - 2); }
long long int fact[limite];
long long int invfact[limite];
long long int combi(long long int n, long long int k) {
if (k < 0) return 0;
if (n < k) return 0;
return fact[n] * invfact[k] % modulo * invfact[n - k] % modulo;
}
int np;
int p[limite];
long long int calcula(int ip, int n, int k, int signo) {
if (n < k) return 0;
if (ip == np) return signo * combi(n - 1, k - 1);
long long int sol = calcula(ip + 1, n, k, signo);
if (n % p[ip] == 0)
sol = (sol + calcula(ip + 1, n / p[ip], k, -signo)) % modulo;
return sol;
}
int esprimo[limite];
int factor[limite];
int main() {
ios::sync_with_stdio(false);
fact[0] = 1;
invfact[0] = 1;
for (int i = 1; i < limite; i++) {
fact[i] = (fact[i - 1] * i) % modulo;
invfact[i] = inverso(fact[i]);
}
for (int i = 2; i < limite; i++) esprimo[i] = 1;
for (int i = 2; i < limite; i++) {
if (esprimo[i]) {
factor[i] = i;
for (int j = i + i; j < limite; j += i) {
esprimo[j] = 0;
factor[j] = i;
}
}
}
int casos;
cin >> casos;
for (int cas = 0; cas < casos; cas++) {
int n, k;
cin >> n >> k;
int nn = n;
np = 0;
while (nn > 1) {
int f = factor[nn];
if (np == 0 or p[np - 1] != f) p[np++] = f;
nn /= f;
}
cout << (calcula(0, n, k, 1) + modulo) % modulo << endl;
}
}
|
### Prompt
In Cpp, your task is to solve the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int limite = 100001;
long long int modulo = 1e9 + 7;
long long int eleva(long long int b, long long int e) {
if (e == 0) return 1;
if (e % 2 == 0) return eleva(b * b % modulo, e / 2);
return b * eleva(b * b % modulo, e / 2) % modulo;
}
long long int inverso(long long int x) { return eleva(x, modulo - 2); }
long long int fact[limite];
long long int invfact[limite];
long long int combi(long long int n, long long int k) {
if (k < 0) return 0;
if (n < k) return 0;
return fact[n] * invfact[k] % modulo * invfact[n - k] % modulo;
}
int np;
int p[limite];
long long int calcula(int ip, int n, int k, int signo) {
if (n < k) return 0;
if (ip == np) return signo * combi(n - 1, k - 1);
long long int sol = calcula(ip + 1, n, k, signo);
if (n % p[ip] == 0)
sol = (sol + calcula(ip + 1, n / p[ip], k, -signo)) % modulo;
return sol;
}
int esprimo[limite];
int factor[limite];
int main() {
ios::sync_with_stdio(false);
fact[0] = 1;
invfact[0] = 1;
for (int i = 1; i < limite; i++) {
fact[i] = (fact[i - 1] * i) % modulo;
invfact[i] = inverso(fact[i]);
}
for (int i = 2; i < limite; i++) esprimo[i] = 1;
for (int i = 2; i < limite; i++) {
if (esprimo[i]) {
factor[i] = i;
for (int j = i + i; j < limite; j += i) {
esprimo[j] = 0;
factor[j] = i;
}
}
}
int casos;
cin >> casos;
for (int cas = 0; cas < casos; cas++) {
int n, k;
cin >> n >> k;
int nn = n;
np = 0;
while (nn > 1) {
int f = factor[nn];
if (np == 0 or p[np - 1] != f) p[np++] = f;
nn /= f;
}
cout << (calcula(0, n, k, 1) + modulo) % modulo << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << "(";
os << v.first << "," << v.second << ")";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
long long sz = v.size();
for (long long i = 0; i < sz; ++i) {
os << v[i];
if (i != sz - 1) os << ",";
}
os << "]\n";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
T last = *v.rbegin();
os << "[";
for (auto it : v) {
os << it;
if (it != last) os << ",";
}
os << "]\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
for (auto it : v) {
os << it.first << " : " << it.second << "\n";
}
return os;
}
long long power(long long a, long long b) {
long long res = 1;
while (b) {
if (b % 2) {
res = (res * a) % 1000000007;
}
b /= 2;
a = (a * a) % 1000000007;
}
return res;
}
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const long long maxn = 1e5 + 6;
long long mu[maxn], pr[maxn], cnt[maxn], dp[maxn];
long long fact[maxn], inv[maxn];
long long get(long long n, long long r) {
if (n < r) return 0;
long long ans = fact[n];
ans *= inv[r];
ans %= 1000000007;
ans *= inv[n - r];
return ans % 1000000007;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
mu[1] = fact[0] = fact[1] = inv[0] = 1;
for (long long i = 2; i < maxn; ++i) pr[i] = 1, mu[i] = 1;
for (long long i = 2; i < maxn; ++i) {
if (pr[i]) {
mu[i] = -1;
for (long long j = 2; (i * j) < maxn; ++j) {
if (j % i == 0)
mu[i * j] = 0;
else
mu[i * j] *= -1;
pr[i * j] = 0;
}
}
fact[i] = fact[i - 1] * i;
fact[i] %= 1000000007;
}
inv[maxn - 1] = power(fact[maxn - 1], 1000000007 - 2);
for (long long i = maxn - 2; i > 0; --i) {
inv[i] = inv[i + 1] * (i + 1);
inv[i] %= 1000000007;
}
long long q;
cin >> q;
while (q--) {
long long n, ans = 0, f;
cin >> n >> f;
for (long long i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ans += mu[i] * get(n / i - 1, f - 1);
while (ans < 1000000007) ans += 1000000007;
ans %= 1000000007;
if (i * i != n) {
ans += mu[n / i] * get(i - 1, f - 1);
while (ans < 1000000007) ans += 1000000007;
ans %= 1000000007;
}
}
}
cout << ans << endl;
}
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << "(";
os << v.first << "," << v.second << ")";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
long long sz = v.size();
for (long long i = 0; i < sz; ++i) {
os << v[i];
if (i != sz - 1) os << ",";
}
os << "]\n";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
T last = *v.rbegin();
os << "[";
for (auto it : v) {
os << it;
if (it != last) os << ",";
}
os << "]\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
for (auto it : v) {
os << it.first << " : " << it.second << "\n";
}
return os;
}
long long power(long long a, long long b) {
long long res = 1;
while (b) {
if (b % 2) {
res = (res * a) % 1000000007;
}
b /= 2;
a = (a * a) % 1000000007;
}
return res;
}
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const long long maxn = 1e5 + 6;
long long mu[maxn], pr[maxn], cnt[maxn], dp[maxn];
long long fact[maxn], inv[maxn];
long long get(long long n, long long r) {
if (n < r) return 0;
long long ans = fact[n];
ans *= inv[r];
ans %= 1000000007;
ans *= inv[n - r];
return ans % 1000000007;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
mu[1] = fact[0] = fact[1] = inv[0] = 1;
for (long long i = 2; i < maxn; ++i) pr[i] = 1, mu[i] = 1;
for (long long i = 2; i < maxn; ++i) {
if (pr[i]) {
mu[i] = -1;
for (long long j = 2; (i * j) < maxn; ++j) {
if (j % i == 0)
mu[i * j] = 0;
else
mu[i * j] *= -1;
pr[i * j] = 0;
}
}
fact[i] = fact[i - 1] * i;
fact[i] %= 1000000007;
}
inv[maxn - 1] = power(fact[maxn - 1], 1000000007 - 2);
for (long long i = maxn - 2; i > 0; --i) {
inv[i] = inv[i + 1] * (i + 1);
inv[i] %= 1000000007;
}
long long q;
cin >> q;
while (q--) {
long long n, ans = 0, f;
cin >> n >> f;
for (long long i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ans += mu[i] * get(n / i - 1, f - 1);
while (ans < 1000000007) ans += 1000000007;
ans %= 1000000007;
if (i * i != n) {
ans += mu[n / i] * get(i - 1, f - 1);
while (ans < 1000000007) ans += 1000000007;
ans %= 1000000007;
}
}
}
cout << ans << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000000 + 7;
const int MAX = 100000 * 2 + 5;
long long fact[MAX], revfact[MAX];
vector<int> primes[MAX];
long long power(long long b, long long p) {
if (!p) return 1;
long long ret = power(b, p >> 1);
ret = ret * ret % MOD;
if (p & 1) ret = ret * b % MOD;
return ret;
}
long long nCr(long long n, long long r) {
if (n < r) return 0;
return fact[n] * revfact[r] % MOD * revfact[n - r] % MOD;
}
int n, f;
long long solve(int idx = 0, int taken = 1, int numTaken = 0, int sign = 1) {
if (idx == primes[n].size())
return (nCr(n / taken - f + f - 1, f - 1) * sign + MOD) % MOD;
return (solve(idx + 1, taken, numTaken, sign) +
solve(idx + 1, taken * primes[n][idx], numTaken + 1, -sign)) %
MOD;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
fact[0] = 1;
for (int i = 1; i < MAX; i++) fact[i] = fact[i - 1] * i % MOD;
revfact[MAX - 1] = power(fact[MAX - 1], MOD - 2);
for (int i = MAX - 2; i >= 0; i--)
revfact[i] = revfact[i + 1] * (i + 1) % MOD;
for (int i = 2; i < MAX; i++)
if (primes[i].size() == 0)
for (int j = i; j < MAX; j += i) primes[j].push_back(i);
int q;
cin >> q;
while (q--) {
cin >> n >> f;
cout << solve() << "\n";
}
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000000 + 7;
const int MAX = 100000 * 2 + 5;
long long fact[MAX], revfact[MAX];
vector<int> primes[MAX];
long long power(long long b, long long p) {
if (!p) return 1;
long long ret = power(b, p >> 1);
ret = ret * ret % MOD;
if (p & 1) ret = ret * b % MOD;
return ret;
}
long long nCr(long long n, long long r) {
if (n < r) return 0;
return fact[n] * revfact[r] % MOD * revfact[n - r] % MOD;
}
int n, f;
long long solve(int idx = 0, int taken = 1, int numTaken = 0, int sign = 1) {
if (idx == primes[n].size())
return (nCr(n / taken - f + f - 1, f - 1) * sign + MOD) % MOD;
return (solve(idx + 1, taken, numTaken, sign) +
solve(idx + 1, taken * primes[n][idx], numTaken + 1, -sign)) %
MOD;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
fact[0] = 1;
for (int i = 1; i < MAX; i++) fact[i] = fact[i - 1] * i % MOD;
revfact[MAX - 1] = power(fact[MAX - 1], MOD - 2);
for (int i = MAX - 2; i >= 0; i--)
revfact[i] = revfact[i + 1] * (i + 1) % MOD;
for (int i = 2; i < MAX; i++)
if (primes[i].size() == 0)
for (int j = i; j < MAX; j += i) primes[j].push_back(i);
int q;
cin >> q;
while (q--) {
cin >> n >> f;
cout << solve() << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
const int mod = 1e9 + 7, N = 1e5 + 9;
int n, m, prime[N], tot, mu[N];
int fac[N], inv[N];
bool isprime[N];
inline void sieve() {
mu[1] = 1;
for (int i = 2; i < N; i++) {
if (!isprime[i]) prime[++tot] = i, mu[i] = -1;
for (int j = 1; j <= tot; j++) {
int k = prime[j];
if (i * k > N) break;
isprime[i * k] = true;
if (i % k == 0) {
mu[i * k] = 0;
break;
}
mu[i * k] = -mu[i];
}
}
fac[0] = fac[1] = inv[0] = inv[1] = 1;
for (int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
for (int i = 2; i < N; i++)
inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
for (int i = 1; i < N; i++) inv[i] = 1ll * inv[i - 1] * inv[i] % mod;
}
inline int C(int n, int m) {
if (m > n) return 0;
return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
sieve();
int T;
scanf("%d", &T);
while (T--) {
int ans = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i * i <= n; i++)
if (n % i == 0) {
ans = (ans + 1ll * mu[i] * C(n / i - 1, m - 1) + mod) % mod;
if (i * i != n)
ans = (ans + 1ll * mu[n / i] * C(i - 1, m - 1) + mod) % mod;
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
const int mod = 1e9 + 7, N = 1e5 + 9;
int n, m, prime[N], tot, mu[N];
int fac[N], inv[N];
bool isprime[N];
inline void sieve() {
mu[1] = 1;
for (int i = 2; i < N; i++) {
if (!isprime[i]) prime[++tot] = i, mu[i] = -1;
for (int j = 1; j <= tot; j++) {
int k = prime[j];
if (i * k > N) break;
isprime[i * k] = true;
if (i % k == 0) {
mu[i * k] = 0;
break;
}
mu[i * k] = -mu[i];
}
}
fac[0] = fac[1] = inv[0] = inv[1] = 1;
for (int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
for (int i = 2; i < N; i++)
inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
for (int i = 1; i < N; i++) inv[i] = 1ll * inv[i - 1] * inv[i] % mod;
}
inline int C(int n, int m) {
if (m > n) return 0;
return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
sieve();
int T;
scanf("%d", &T);
while (T--) {
int ans = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i * i <= n; i++)
if (n % i == 0) {
ans = (ans + 1ll * mu[i] * C(n / i - 1, m - 1) + mod) % mod;
if (i * i != n)
ans = (ans + 1ll * mu[n / i] * C(i - 1, m - 1) + mod) % mod;
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
long long fact[MAXN];
long long inv_fact[MAXN];
long long nchoosek(int n, int k) {
long long ans = fact[n] * inv_fact[n - k];
ans %= MOD;
ans = ans * inv_fact[k];
ans %= MOD;
return ans;
}
long long exp(long long base, long long exponent) {
if (exponent == 0) return 1;
if (exponent & 1) {
long long ans = base * exp(base, exponent - 1);
ans %= MOD;
return ans;
}
long long ans = exp(base, exponent / 2);
ans = ans * ans;
ans %= MOD;
return ans;
}
void gen() {
fact[0] = inv_fact[0] = 1;
for (int i = 1; i < MAXN; i++) {
fact[i] = fact[i - 1] * i;
fact[i] %= MOD;
inv_fact[i] = exp(fact[i], MOD - 2);
}
}
vector<int> divisors[MAXN];
bool composite[MAXN];
int mobius[MAXN];
int prime_count[MAXN];
void sieve() {
for (int i = 2; i < MAXN; i++) mobius[i] = 1;
for (int i = 2; i < MAXN; i++) {
if (composite[i]) continue;
prime_count[i] = 1;
for (int j = i + i; j < MAXN; j += i) {
composite[j] = true;
if (j % (1LL * i * i) == 0) mobius[j] = 0;
prime_count[j]++;
}
}
for (int i = 2; i < MAXN; i++) {
if (!mobius[i]) continue;
if (prime_count[i] & 1) {
mobius[i] = -1;
} else {
mobius[i] = 1;
}
}
for (int i = 2; i < MAXN; i++) {
for (int j = i; j < MAXN; j += i) divisors[j].push_back(i);
}
}
int main() {
ios::sync_with_stdio(0);
gen();
sieve();
int q;
cin >> q;
while (q--) {
int total, buckets;
cin >> total >> buckets;
if (buckets == 1) {
if (total == 1)
cout << 1 << endl;
else
cout << 0 << endl;
continue;
}
if (total < buckets) {
cout << 0 << endl;
continue;
}
long long ans = nchoosek(total - 1, buckets - 1);
for (int i = 0; i < divisors[total].size(); i++) {
int left = total / divisors[total][i];
int current = divisors[total][i];
if (buckets > left) continue;
if (left == 1) continue;
ans += mobius[current] * nchoosek(left - 1, buckets - 1);
ans += mod;
ans %= mod;
}
cout << ans << endl;
}
}
|
### Prompt
Please formulate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
long long fact[MAXN];
long long inv_fact[MAXN];
long long nchoosek(int n, int k) {
long long ans = fact[n] * inv_fact[n - k];
ans %= MOD;
ans = ans * inv_fact[k];
ans %= MOD;
return ans;
}
long long exp(long long base, long long exponent) {
if (exponent == 0) return 1;
if (exponent & 1) {
long long ans = base * exp(base, exponent - 1);
ans %= MOD;
return ans;
}
long long ans = exp(base, exponent / 2);
ans = ans * ans;
ans %= MOD;
return ans;
}
void gen() {
fact[0] = inv_fact[0] = 1;
for (int i = 1; i < MAXN; i++) {
fact[i] = fact[i - 1] * i;
fact[i] %= MOD;
inv_fact[i] = exp(fact[i], MOD - 2);
}
}
vector<int> divisors[MAXN];
bool composite[MAXN];
int mobius[MAXN];
int prime_count[MAXN];
void sieve() {
for (int i = 2; i < MAXN; i++) mobius[i] = 1;
for (int i = 2; i < MAXN; i++) {
if (composite[i]) continue;
prime_count[i] = 1;
for (int j = i + i; j < MAXN; j += i) {
composite[j] = true;
if (j % (1LL * i * i) == 0) mobius[j] = 0;
prime_count[j]++;
}
}
for (int i = 2; i < MAXN; i++) {
if (!mobius[i]) continue;
if (prime_count[i] & 1) {
mobius[i] = -1;
} else {
mobius[i] = 1;
}
}
for (int i = 2; i < MAXN; i++) {
for (int j = i; j < MAXN; j += i) divisors[j].push_back(i);
}
}
int main() {
ios::sync_with_stdio(0);
gen();
sieve();
int q;
cin >> q;
while (q--) {
int total, buckets;
cin >> total >> buckets;
if (buckets == 1) {
if (total == 1)
cout << 1 << endl;
else
cout << 0 << endl;
continue;
}
if (total < buckets) {
cout << 0 << endl;
continue;
}
long long ans = nchoosek(total - 1, buckets - 1);
for (int i = 0; i < divisors[total].size(); i++) {
int left = total / divisors[total][i];
int current = divisors[total][i];
if (buckets > left) continue;
if (left == 1) continue;
ans += mobius[current] * nchoosek(left - 1, buckets - 1);
ans += mod;
ans %= mod;
}
cout << ans << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = (int)(1e9 + 7);
int n, k, q, M, pn, p[200], l_p[100005];
long long res, first[100005], g[100005];
long long mod_pow(long long a, int b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
long long c(int n, int k) {
long long res = g[k] * g[n - k] % mod;
res = res * first[n] % mod;
return res;
}
inline void rec(int pos, int val, bool odd) {
if (val > M) return;
if (pos == pn) {
int nn = n / val;
if (odd)
res -= c(nn - 1, k - 1);
else
res += c(nn - 1, k - 1);
return;
}
rec(pos + 1, val, odd);
rec(pos + 1, val * p[pos], !odd);
}
int calc(int n, int k) {
if (n == 1) return 1;
if (k == 1) return 0;
pn = 0, M = n / k;
int nn = n, j;
while (nn > 1) {
j = l_p[nn];
p[pn++] = j;
while (nn % j == 0) nn /= j;
}
res = 0;
rec(0, 1, 0);
res %= mod;
res = (res + mod) % mod;
return res;
}
int main() {
for (int i = 2; i <= 100000; ++i)
if (!l_p[i]) {
for (int j = i; j <= 100000; j += i)
if (!l_p[j]) l_p[j] = i;
}
first[0] = 1;
g[0] = 1;
for (int i = 1; i <= 100000; ++i) {
first[i] = first[i - 1] * i % mod;
g[i] = mod_pow(first[i], mod - 2);
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &k);
printf("%d\n", calc(n, k));
}
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = (int)(1e9 + 7);
int n, k, q, M, pn, p[200], l_p[100005];
long long res, first[100005], g[100005];
long long mod_pow(long long a, int b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
long long c(int n, int k) {
long long res = g[k] * g[n - k] % mod;
res = res * first[n] % mod;
return res;
}
inline void rec(int pos, int val, bool odd) {
if (val > M) return;
if (pos == pn) {
int nn = n / val;
if (odd)
res -= c(nn - 1, k - 1);
else
res += c(nn - 1, k - 1);
return;
}
rec(pos + 1, val, odd);
rec(pos + 1, val * p[pos], !odd);
}
int calc(int n, int k) {
if (n == 1) return 1;
if (k == 1) return 0;
pn = 0, M = n / k;
int nn = n, j;
while (nn > 1) {
j = l_p[nn];
p[pn++] = j;
while (nn % j == 0) nn /= j;
}
res = 0;
rec(0, 1, 0);
res %= mod;
res = (res + mod) % mod;
return res;
}
int main() {
for (int i = 2; i <= 100000; ++i)
if (!l_p[i]) {
for (int j = i; j <= 100000; j += i)
if (!l_p[j]) l_p[j] = i;
}
first[0] = 1;
g[0] = 1;
for (int i = 1; i <= 100000; ++i) {
first[i] = first[i - 1] * i % mod;
g[i] = mod_pow(first[i], mod - 2);
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &k);
printf("%d\n", calc(n, k));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long base = 1e9 + 7;
const int N = 1e5 + 5;
long long gt[N], ndm[N];
int q, m, n;
map<pair<long long, long long>, long long> M;
vector<int> ds[N];
long long pw(long long a, long long b) {
if (b == 1) return a;
if (b == 0) return 1;
long long t = pw(a, b / 2);
if (b % 2 == 0) return (t * t) % base;
return ((t * t) % base * a) % base;
}
void start() {
gt[0] = 1;
for (int i = 1; i <= 1e5; i++) gt[i] = (gt[i - 1] * i) % base;
ndm[0] = 1;
for (int i = 1; i <= 1e5; i++) {
ndm[i] = pw(gt[i], base - 2);
}
for (int i = 2; i <= 1e5; i++) {
for (int j = i + i; j <= 1e5; j += i) {
ds[j].push_back(i);
}
}
}
long long try1(int m, int n) {
if (m < n) return 0;
if (M[make_pair(m, n)] != 0) return M[make_pair(m, n)];
int k = sqrt(m) + 1, p = m;
long long ans = ((gt[m - 1] * ndm[n - 1]) % base * ndm[m - n]) % base;
for (int i = 0; i < ds[m].size(); i++) {
ans = (ans - try1(m / ds[m][i], n) + base) % base;
}
M[make_pair(m, n)] = ans;
return ans;
}
int main() {
start();
cin >> q;
while (q--) {
cin >> m >> n;
if (m == n) {
cout << 1 << '\n';
goto go;
}
if (n == 1) {
cout << 0 << '\n';
goto go;
}
cout << try1(m, n) << '\n';
go:;
m = 0;
n = 0;
}
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long base = 1e9 + 7;
const int N = 1e5 + 5;
long long gt[N], ndm[N];
int q, m, n;
map<pair<long long, long long>, long long> M;
vector<int> ds[N];
long long pw(long long a, long long b) {
if (b == 1) return a;
if (b == 0) return 1;
long long t = pw(a, b / 2);
if (b % 2 == 0) return (t * t) % base;
return ((t * t) % base * a) % base;
}
void start() {
gt[0] = 1;
for (int i = 1; i <= 1e5; i++) gt[i] = (gt[i - 1] * i) % base;
ndm[0] = 1;
for (int i = 1; i <= 1e5; i++) {
ndm[i] = pw(gt[i], base - 2);
}
for (int i = 2; i <= 1e5; i++) {
for (int j = i + i; j <= 1e5; j += i) {
ds[j].push_back(i);
}
}
}
long long try1(int m, int n) {
if (m < n) return 0;
if (M[make_pair(m, n)] != 0) return M[make_pair(m, n)];
int k = sqrt(m) + 1, p = m;
long long ans = ((gt[m - 1] * ndm[n - 1]) % base * ndm[m - n]) % base;
for (int i = 0; i < ds[m].size(); i++) {
ans = (ans - try1(m / ds[m][i], n) + base) % base;
}
M[make_pair(m, n)] = ans;
return ans;
}
int main() {
start();
cin >> q;
while (q--) {
cin >> m >> n;
if (m == n) {
cout << 1 << '\n';
goto go;
}
if (n == 1) {
cout << 0 << '\n';
goto go;
}
cout << try1(m, n) << '\n';
go:;
m = 0;
n = 0;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1001000;
const int MOD = 1e9 + 7;
int cnt;
int a[maxn], Count[maxn];
long long Pow[maxn], INV[maxn];
bool check[maxn];
int prime[maxn], mu[maxn], tot;
void Moblus() {
memset(check, false, sizeof(check));
mu[1] = 1, tot = 0;
for (int i = 2; i < maxn; i++) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
check[i * prime[j]] = true;
if (i % prime[j])
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
}
void solve(int n) {
int num = sqrt(n + 1);
for (int i = 1; i <= num; i++) {
if (n % i == 0) {
a[cnt++] = i;
if (i * i != n) a[cnt++] = n / i;
}
}
}
long long inv(long long a, long long m) {
if (a == 1) return 1;
return inv(m % a, m) * (m - m / a) % m;
}
int main() {
Moblus();
int _, n, f;
Pow[0] = 1;
for (int i = 1; i < maxn; i++) Pow[i] = Pow[i - 1] * i % MOD;
for (int i = 0; i < maxn; i++) INV[i] = inv(Pow[i], MOD);
scanf("%d", &_);
while (_--) {
scanf("%d%d", &n, &f);
cnt = 0;
solve(n);
long long ans = 0;
for (int i = 0; i < cnt; i++) {
if (n / a[i] < f) continue;
Count[a[i]] =
Pow[n / a[i] - 1] * INV[n / a[i] - f] % MOD * INV[f - 1] % MOD;
ans = (ans + 1LL * Count[a[i]] * mu[a[i]]) % MOD;
}
printf("%lld\n", (ans + MOD) % MOD);
}
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1001000;
const int MOD = 1e9 + 7;
int cnt;
int a[maxn], Count[maxn];
long long Pow[maxn], INV[maxn];
bool check[maxn];
int prime[maxn], mu[maxn], tot;
void Moblus() {
memset(check, false, sizeof(check));
mu[1] = 1, tot = 0;
for (int i = 2; i < maxn; i++) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
check[i * prime[j]] = true;
if (i % prime[j])
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
}
void solve(int n) {
int num = sqrt(n + 1);
for (int i = 1; i <= num; i++) {
if (n % i == 0) {
a[cnt++] = i;
if (i * i != n) a[cnt++] = n / i;
}
}
}
long long inv(long long a, long long m) {
if (a == 1) return 1;
return inv(m % a, m) * (m - m / a) % m;
}
int main() {
Moblus();
int _, n, f;
Pow[0] = 1;
for (int i = 1; i < maxn; i++) Pow[i] = Pow[i - 1] * i % MOD;
for (int i = 0; i < maxn; i++) INV[i] = inv(Pow[i], MOD);
scanf("%d", &_);
while (_--) {
scanf("%d%d", &n, &f);
cnt = 0;
solve(n);
long long ans = 0;
for (int i = 0; i < cnt; i++) {
if (n / a[i] < f) continue;
Count[a[i]] =
Pow[n / a[i] - 1] * INV[n / a[i] - f] % MOD * INV[f - 1] % MOD;
ans = (ans + 1LL * Count[a[i]] * mu[a[i]]) % MOD;
}
printf("%lld\n", (ans + MOD) % MOD);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int mu[100005], isp[100005], n, m, t;
int fact[100005], inv[100005];
vector<int> ft[100005];
int po(int a, int b) {
int c = 1, d = a;
while (b) {
if (b & 1) c = 1LL * c * d % 1000000007;
d = 1LL * d * d % 1000000007;
b >>= 1;
}
return c;
}
int ncr(int a, int b) {
if (b < 0 || b > a) return 0;
int ret = fact[a];
ret = 1LL * ret * inv[b] % 1000000007;
ret = 1LL * ret * inv[a - b] % 1000000007;
return ret;
}
int main() {
for (int i = (1); i <= (100000); i++) mu[i] = isp[i] = 1;
for (int i = (1); i <= (100000); i++)
for (int j = i; j <= 100000; j += i) ft[j].push_back(i);
for (int i = (2); i <= (100000); i++)
if (isp[i]) {
for (int j = i; j <= 100000; j += i) {
mu[j] *= -1;
if (j % (i * i) == 0) mu[j] = 0;
isp[j] = 0;
}
}
fact[0] = inv[0] = 1;
for (int i = (1); i <= (100000); i++)
fact[i] = 1LL * fact[i - 1] * i % 1000000007;
for (int i = (1); i <= (100000); i++)
inv[i] = 1LL * inv[i - 1] * po(i, 1000000007 - 2) % 1000000007;
scanf("%d", &t);
for (int i = (1); i <= (t); i++) {
scanf("%d %d", &n, &m);
int ans = 0;
for (int j = (0); j <= ((int)ft[n].size() - 1); j++) {
int gg = ft[n][j];
int bsum = n / gg;
ans += 1LL * mu[gg] * ncr(bsum - 1, m - 1) % 1000000007;
ans = (ans % 1000000007 + 1000000007) % 1000000007;
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int mu[100005], isp[100005], n, m, t;
int fact[100005], inv[100005];
vector<int> ft[100005];
int po(int a, int b) {
int c = 1, d = a;
while (b) {
if (b & 1) c = 1LL * c * d % 1000000007;
d = 1LL * d * d % 1000000007;
b >>= 1;
}
return c;
}
int ncr(int a, int b) {
if (b < 0 || b > a) return 0;
int ret = fact[a];
ret = 1LL * ret * inv[b] % 1000000007;
ret = 1LL * ret * inv[a - b] % 1000000007;
return ret;
}
int main() {
for (int i = (1); i <= (100000); i++) mu[i] = isp[i] = 1;
for (int i = (1); i <= (100000); i++)
for (int j = i; j <= 100000; j += i) ft[j].push_back(i);
for (int i = (2); i <= (100000); i++)
if (isp[i]) {
for (int j = i; j <= 100000; j += i) {
mu[j] *= -1;
if (j % (i * i) == 0) mu[j] = 0;
isp[j] = 0;
}
}
fact[0] = inv[0] = 1;
for (int i = (1); i <= (100000); i++)
fact[i] = 1LL * fact[i - 1] * i % 1000000007;
for (int i = (1); i <= (100000); i++)
inv[i] = 1LL * inv[i - 1] * po(i, 1000000007 - 2) % 1000000007;
scanf("%d", &t);
for (int i = (1); i <= (t); i++) {
scanf("%d %d", &n, &m);
int ans = 0;
for (int j = (0); j <= ((int)ft[n].size() - 1); j++) {
int gg = ft[n][j];
int bsum = n / gg;
ans += 1LL * mu[gg] * ncr(bsum - 1, m - 1) % 1000000007;
ans = (ans % 1000000007 + 1000000007) % 1000000007;
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double EPS = 1e-7;
int INF = 1000000000;
int MOD = 1000000007;
int MAXINT = 2147483647;
long long INFLL = 1000000000000000000LL;
long long MAXLL = 9223372036854775807LL;
int mx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int my[8] = {0, 0, -1, 1, -1, 1, -1, 1};
long long inv[400005];
long long fac[400005];
long long mod = 1000000007;
int num[400005];
int prime[400005];
int ignored[400005];
void sieve() {
for (int(x) = (2); (x) <= (100000); (x)++) {
if (!prime[x]) {
num[x] = 1;
for (int y = 2; y * x <= 100000; y++) {
int d = y * x;
prime[d] = 1;
int in = 0;
while (d % x == 0) {
in++;
num[y * x]++;
d /= x;
}
if (in > 1) ignored[y * x] = 1;
}
}
}
}
inline long long ipow(long long base, int exp) {
long long result = 1;
while (exp) {
if (exp & 1) result *= base;
exp >>= 1;
base *= base;
result %= mod;
base %= mod;
}
return result;
}
long long comb(int n, int k) {
if (n < k) return 0;
return ((fac[n] * inv[k]) % mod * inv[n - k]) % mod;
}
int main() {
inv[0] = 1;
fac[0] = 1;
inv[1] = 1;
fac[1] = 1;
for (int(a) = (2); (a) <= (400000); (a)++) {
inv[a] = (inv[a - 1] * ipow(a, mod - 2)) % mod;
fac[a] = (fac[a - 1] * (long long)a) % mod;
}
sieve();
int q;
scanf("%d", &q);
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
long long ans = 0;
for (int x = 1; x * x <= n; x++) {
if (n % x == 0) {
int y = n / x;
if (!ignored[x]) {
if (num[x] % 2 == 1)
ans += (mod - comb(y - 1, f - 1));
else
ans += (comb(y - 1, f - 1));
}
ans %= mod;
if (x != y) {
if (!ignored[y]) {
if (num[y] % 2 == 1)
ans += (mod - comb(x - 1, f - 1));
else
ans += (comb(x - 1, f - 1));
}
ans %= mod;
}
}
}
printf("%I64d\n", ans);
}
}
|
### Prompt
In Cpp, your task is to solve the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double EPS = 1e-7;
int INF = 1000000000;
int MOD = 1000000007;
int MAXINT = 2147483647;
long long INFLL = 1000000000000000000LL;
long long MAXLL = 9223372036854775807LL;
int mx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int my[8] = {0, 0, -1, 1, -1, 1, -1, 1};
long long inv[400005];
long long fac[400005];
long long mod = 1000000007;
int num[400005];
int prime[400005];
int ignored[400005];
void sieve() {
for (int(x) = (2); (x) <= (100000); (x)++) {
if (!prime[x]) {
num[x] = 1;
for (int y = 2; y * x <= 100000; y++) {
int d = y * x;
prime[d] = 1;
int in = 0;
while (d % x == 0) {
in++;
num[y * x]++;
d /= x;
}
if (in > 1) ignored[y * x] = 1;
}
}
}
}
inline long long ipow(long long base, int exp) {
long long result = 1;
while (exp) {
if (exp & 1) result *= base;
exp >>= 1;
base *= base;
result %= mod;
base %= mod;
}
return result;
}
long long comb(int n, int k) {
if (n < k) return 0;
return ((fac[n] * inv[k]) % mod * inv[n - k]) % mod;
}
int main() {
inv[0] = 1;
fac[0] = 1;
inv[1] = 1;
fac[1] = 1;
for (int(a) = (2); (a) <= (400000); (a)++) {
inv[a] = (inv[a - 1] * ipow(a, mod - 2)) % mod;
fac[a] = (fac[a - 1] * (long long)a) % mod;
}
sieve();
int q;
scanf("%d", &q);
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
long long ans = 0;
for (int x = 1; x * x <= n; x++) {
if (n % x == 0) {
int y = n / x;
if (!ignored[x]) {
if (num[x] % 2 == 1)
ans += (mod - comb(y - 1, f - 1));
else
ans += (comb(y - 1, f - 1));
}
ans %= mod;
if (x != y) {
if (!ignored[y]) {
if (num[y] % 2 == 1)
ans += (mod - comb(x - 1, f - 1));
else
ans += (comb(x - 1, f - 1));
}
ans %= mod;
}
}
}
printf("%I64d\n", ans);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long pow(long long a, long long b, long long c) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % c;
a = (a * a) % c;
b >>= 1;
}
return ans;
}
long long modInv(long long p) { return pow(p, 1000000007 - 2, 1000000007); }
long long F[100000 + 5], FP[100000 + 5];
void initFact() {
F[0] = 1;
for (int i = 1; i <= 100000; ++i)
F[i] = ((long long)i * F[i - 1]) % 1000000007;
FP[100000] = modInv(F[100000]);
for (int i = 100000 - 1; i >= 0; --i)
FP[i] = (FP[i + 1] * (long long)(i + 1)) % 1000000007;
}
long long comb(int n, int k) {
if (k > n) return 0;
long long ans = (F[n] * FP[n - k]) % 1000000007;
ans = (ans * FP[k]) % 1000000007;
return ans;
}
int P[100000 + 5];
vector<int> D[100000 + 5];
void initSieve() {
memset(P, -1, sizeof(P));
for (int i = 2; i * i <= 100000; ++i)
if (P[i] == -1)
for (int j = i * i; j <= 100000; j += i) P[j] = i;
}
void primefact(int n, vector<int> &p, vector<int> &e) {
while (1) {
if (n == 1) break;
if (P[n] == -1) {
p.push_back(n);
e.push_back(1);
return;
}
int pk = P[n], exp = 0;
while (n % pk == 0) n /= pk, exp++;
p.push_back(pk);
e.push_back(exp);
}
}
vector<int> getDiv(vector<int> &p, vector<int> &e) {
int n = p.size();
vector<int> d(1, 1);
for (int i = 0; i < n; ++i) {
int m = ((int)d.size());
for (int j = 0; j < e[i]; ++j) {
for (int k = 0; k < m; ++k) d.push_back(p[i] * d[k + j * m]);
}
}
return d;
}
void impr(vector<int> v) {
for (int i = 0; i < ((int)v.size()); ++i)
printf("%d%c", v[i], i + 1 == ((int)v.size()) ? 10 : 32);
}
int fix(int x) {
while (x >= 1000000007) x -= 1000000007;
while (x < 0) x += 1000000007;
return x;
}
int memo[100000 + 5];
bool used[100000 + 5];
int f;
vector<int> p, e, d;
int dp(int n) {
if (used[n]) return memo[n];
used[n] = 1;
int &dev = memo[n] = 0;
for (int i = 0; i < ((int)D[n].size()); ++i)
if (D[n][i] < n) {
if (n % D[n][i] == 0) dev = fix(dev + dp(D[n][i]));
} else
break;
dev = fix(comb(n - 1, f - 1) - dev);
return dev;
}
int main() {
initFact();
initSieve();
for (int n = 1; n <= 100000; ++n) {
p.clear();
e.clear();
primefact(n, p, e);
d = getDiv(p, e);
sort(d.begin(), d.end());
D[n] = d;
}
int cases, n;
scanf("%d", &cases);
for (int tc = 0; tc < cases; ++tc) {
scanf("%d", &n), scanf("%d", &f);
for (int i = 0; i < ((int)D[n].size()); ++i) used[D[n][i]] = 0;
printf("%d\n", dp(n));
}
}
|
### Prompt
In Cpp, your task is to solve the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long pow(long long a, long long b, long long c) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % c;
a = (a * a) % c;
b >>= 1;
}
return ans;
}
long long modInv(long long p) { return pow(p, 1000000007 - 2, 1000000007); }
long long F[100000 + 5], FP[100000 + 5];
void initFact() {
F[0] = 1;
for (int i = 1; i <= 100000; ++i)
F[i] = ((long long)i * F[i - 1]) % 1000000007;
FP[100000] = modInv(F[100000]);
for (int i = 100000 - 1; i >= 0; --i)
FP[i] = (FP[i + 1] * (long long)(i + 1)) % 1000000007;
}
long long comb(int n, int k) {
if (k > n) return 0;
long long ans = (F[n] * FP[n - k]) % 1000000007;
ans = (ans * FP[k]) % 1000000007;
return ans;
}
int P[100000 + 5];
vector<int> D[100000 + 5];
void initSieve() {
memset(P, -1, sizeof(P));
for (int i = 2; i * i <= 100000; ++i)
if (P[i] == -1)
for (int j = i * i; j <= 100000; j += i) P[j] = i;
}
void primefact(int n, vector<int> &p, vector<int> &e) {
while (1) {
if (n == 1) break;
if (P[n] == -1) {
p.push_back(n);
e.push_back(1);
return;
}
int pk = P[n], exp = 0;
while (n % pk == 0) n /= pk, exp++;
p.push_back(pk);
e.push_back(exp);
}
}
vector<int> getDiv(vector<int> &p, vector<int> &e) {
int n = p.size();
vector<int> d(1, 1);
for (int i = 0; i < n; ++i) {
int m = ((int)d.size());
for (int j = 0; j < e[i]; ++j) {
for (int k = 0; k < m; ++k) d.push_back(p[i] * d[k + j * m]);
}
}
return d;
}
void impr(vector<int> v) {
for (int i = 0; i < ((int)v.size()); ++i)
printf("%d%c", v[i], i + 1 == ((int)v.size()) ? 10 : 32);
}
int fix(int x) {
while (x >= 1000000007) x -= 1000000007;
while (x < 0) x += 1000000007;
return x;
}
int memo[100000 + 5];
bool used[100000 + 5];
int f;
vector<int> p, e, d;
int dp(int n) {
if (used[n]) return memo[n];
used[n] = 1;
int &dev = memo[n] = 0;
for (int i = 0; i < ((int)D[n].size()); ++i)
if (D[n][i] < n) {
if (n % D[n][i] == 0) dev = fix(dev + dp(D[n][i]));
} else
break;
dev = fix(comb(n - 1, f - 1) - dev);
return dev;
}
int main() {
initFact();
initSieve();
for (int n = 1; n <= 100000; ++n) {
p.clear();
e.clear();
primefact(n, p, e);
d = getDiv(p, e);
sort(d.begin(), d.end());
D[n] = d;
}
int cases, n;
scanf("%d", &cases);
for (int tc = 0; tc < cases; ++tc) {
scanf("%d", &n), scanf("%d", &f);
for (int i = 0; i < ((int)D[n].size()); ++i) used[D[n][i]] = 0;
printf("%d\n", dp(n));
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
long long f[MAXN];
long long jie[MAXN];
bool is[MAXN];
vector<int> dive;
map<pair<int, int>, int> rem;
void init() {
jie[0] = 1;
for (int i = 1; i < MAXN; i++) {
jie[i] = jie[i - 1] * i % MOD;
}
rem.clear();
}
void get_dive(int sum, int n) {
int e = (int)sqrt(sum + 0.0);
dive.clear();
int j;
for (int i = 1; i <= e; i++) {
if (sum % i == 0) {
if (sum / i >= n) dive.push_back(i);
j = sum / i;
if (j != i && sum / j >= n) dive.push_back(j);
}
}
sort(dive.begin(), dive.end());
for (int i = 0; i < dive.size(); i++) {
is[dive[i]] = true;
}
}
long long qp(long long x, long long y) {
long long res = 1LL;
while (y) {
if (y & 1) res = res * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return res;
}
long long comb(int x, int y) {
if (y < 0 || y > x) return 0;
if (y == 0 || y == x) return 1;
return jie[x] * qp(jie[y] * jie[x - y] % MOD, MOD - 2) % MOD;
}
void solve(int sum, int n) {
map<pair<int, int>, int>::iterator it;
it = rem.find(make_pair(sum, n));
if (it != rem.end()) {
printf("%d\n", (int)(it->second));
return;
}
memset(f, 0, sizeof f);
memset(is, false, sizeof is);
get_dive(sum, n);
int ma = dive.size();
for (int i = ma - 1; i >= 0; i--) {
int d = dive[i];
f[d] = comb(sum / d - 1, n - 1);
for (int j = 2 * d; j <= dive[ma - 1]; j += d) {
if (is[j]) {
f[d] = ((f[d] - f[j] + MOD) % MOD + MOD) % MOD;
}
}
}
printf("%d\n", (int)f[1]);
rem[make_pair(sum, n)] = f[1];
return;
}
int powerpowerpowerpowerpower = 1234123412;
int main() {
init();
int test;
scanf("%d", &test);
while (test--) {
int sum, n;
scanf("%d %d", &sum, &n);
solve(sum, n);
}
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
long long f[MAXN];
long long jie[MAXN];
bool is[MAXN];
vector<int> dive;
map<pair<int, int>, int> rem;
void init() {
jie[0] = 1;
for (int i = 1; i < MAXN; i++) {
jie[i] = jie[i - 1] * i % MOD;
}
rem.clear();
}
void get_dive(int sum, int n) {
int e = (int)sqrt(sum + 0.0);
dive.clear();
int j;
for (int i = 1; i <= e; i++) {
if (sum % i == 0) {
if (sum / i >= n) dive.push_back(i);
j = sum / i;
if (j != i && sum / j >= n) dive.push_back(j);
}
}
sort(dive.begin(), dive.end());
for (int i = 0; i < dive.size(); i++) {
is[dive[i]] = true;
}
}
long long qp(long long x, long long y) {
long long res = 1LL;
while (y) {
if (y & 1) res = res * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return res;
}
long long comb(int x, int y) {
if (y < 0 || y > x) return 0;
if (y == 0 || y == x) return 1;
return jie[x] * qp(jie[y] * jie[x - y] % MOD, MOD - 2) % MOD;
}
void solve(int sum, int n) {
map<pair<int, int>, int>::iterator it;
it = rem.find(make_pair(sum, n));
if (it != rem.end()) {
printf("%d\n", (int)(it->second));
return;
}
memset(f, 0, sizeof f);
memset(is, false, sizeof is);
get_dive(sum, n);
int ma = dive.size();
for (int i = ma - 1; i >= 0; i--) {
int d = dive[i];
f[d] = comb(sum / d - 1, n - 1);
for (int j = 2 * d; j <= dive[ma - 1]; j += d) {
if (is[j]) {
f[d] = ((f[d] - f[j] + MOD) % MOD + MOD) % MOD;
}
}
}
printf("%d\n", (int)f[1]);
rem[make_pair(sum, n)] = f[1];
return;
}
int powerpowerpowerpowerpower = 1234123412;
int main() {
init();
int test;
scanf("%d", &test);
while (test--) {
int sum, n;
scanf("%d %d", &sum, &n);
solve(sum, n);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long p[200010], fp[200010];
long long dp[200010];
vector<int> fac[200010], pfac[200010];
int cal(int A, int B) {
B--;
A += B;
if (A < 0 || B < 0 || A - B < 0) return 0;
return p[A] * fp[A - B] % 1000000007 * fp[B] % 1000000007;
}
long long quick_sqr(long long A, int K) {
long long ret = 1;
while (K) {
if (K & 1) ret = ret * A % 1000000007;
A = A * A % 1000000007;
K >>= 1;
}
return ret;
}
int main() {
p[0] = fp[0] = 1;
for (int i = 1; i < 200010; i++) {
p[i] = p[i - 1] * i % 1000000007;
fp[i] = quick_sqr(p[i], 1000000007 - 2);
}
for (int i = 1; i < 200010; i++)
for (int j = i; j < 200010; j += i) {
fac[j].push_back(i);
}
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d", &n, &m);
for (int i = 0; i < fac[n].size(); i++) {
int k = fac[n][i];
dp[k] = cal(k - m, m);
for (int j = 0; j < fac[k].size() - 1; j++)
dp[k] += 1000000007 - dp[fac[k][j]];
dp[k] %= 1000000007;
}
cout << dp[n] << endl;
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long p[200010], fp[200010];
long long dp[200010];
vector<int> fac[200010], pfac[200010];
int cal(int A, int B) {
B--;
A += B;
if (A < 0 || B < 0 || A - B < 0) return 0;
return p[A] * fp[A - B] % 1000000007 * fp[B] % 1000000007;
}
long long quick_sqr(long long A, int K) {
long long ret = 1;
while (K) {
if (K & 1) ret = ret * A % 1000000007;
A = A * A % 1000000007;
K >>= 1;
}
return ret;
}
int main() {
p[0] = fp[0] = 1;
for (int i = 1; i < 200010; i++) {
p[i] = p[i - 1] * i % 1000000007;
fp[i] = quick_sqr(p[i], 1000000007 - 2);
}
for (int i = 1; i < 200010; i++)
for (int j = i; j < 200010; j += i) {
fac[j].push_back(i);
}
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d", &n, &m);
for (int i = 0; i < fac[n].size(); i++) {
int k = fac[n][i];
dp[k] = cal(k - m, m);
for (int j = 0; j < fac[k].size() - 1; j++)
dp[k] += 1000000007 - dp[fac[k][j]];
dp[k] %= 1000000007;
}
cout << dp[n] << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 123;
const long long LINF = (long long)1e18 + 123;
void run();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = (int)1e5 + 123;
const long long MOD = (long long)1e9 + 7;
long long power(long long x, long long y) {
if (y == 0) {
return 1;
}
if (y & 1) {
return x * power(x, y - 1) % MOD;
} else {
long long t = power(x, y / 2);
return t * t % MOD;
}
}
int n, f;
int ptr;
int divs[N];
long long fact[N], rfact[N];
long long dp[N];
vector<int> precalc[N];
long long cnk(long long n, long long k) {
return fact[n] * rfact[k] % MOD * rfact[n - k] % MOD;
}
void add(long long &x, long long y) {
x += y;
if (x >= MOD) x -= MOD;
}
void sub(long long &x, long long y) {
x -= y;
if (x < 0) x += MOD;
}
void solve() {
ptr = 0;
for (int d : precalc[n]) {
dp[d] = 0;
}
for (int i = (int)precalc[n].size() - 1; i >= 0; i--) {
int x = precalc[n][i];
int have = n / x;
if (have >= f) {
add(dp[x], cnk(have - 1, have - f));
}
for (int d : precalc[x]) {
if (d != x) {
sub(dp[d], dp[x]);
}
}
}
cout << dp[1] << "\n";
}
void run() {
fact[0] = 1;
rfact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = fact[i - 1] * i % MOD;
rfact[i] = power(fact[i], MOD - 2);
for (int d = 1; d * d <= i; d++) {
if (i % d == 0) {
int subd = i / d;
precalc[i].push_back(d);
if (d != subd) {
precalc[i].push_back(subd);
}
}
}
sort(precalc[i].begin(), precalc[i].end());
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
cin >> n >> f;
solve();
}
}
|
### Prompt
Please create a solution in cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 123;
const long long LINF = (long long)1e18 + 123;
void run();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = (int)1e5 + 123;
const long long MOD = (long long)1e9 + 7;
long long power(long long x, long long y) {
if (y == 0) {
return 1;
}
if (y & 1) {
return x * power(x, y - 1) % MOD;
} else {
long long t = power(x, y / 2);
return t * t % MOD;
}
}
int n, f;
int ptr;
int divs[N];
long long fact[N], rfact[N];
long long dp[N];
vector<int> precalc[N];
long long cnk(long long n, long long k) {
return fact[n] * rfact[k] % MOD * rfact[n - k] % MOD;
}
void add(long long &x, long long y) {
x += y;
if (x >= MOD) x -= MOD;
}
void sub(long long &x, long long y) {
x -= y;
if (x < 0) x += MOD;
}
void solve() {
ptr = 0;
for (int d : precalc[n]) {
dp[d] = 0;
}
for (int i = (int)precalc[n].size() - 1; i >= 0; i--) {
int x = precalc[n][i];
int have = n / x;
if (have >= f) {
add(dp[x], cnk(have - 1, have - f));
}
for (int d : precalc[x]) {
if (d != x) {
sub(dp[d], dp[x]);
}
}
}
cout << dp[1] << "\n";
}
void run() {
fact[0] = 1;
rfact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = fact[i - 1] * i % MOD;
rfact[i] = power(fact[i], MOD - 2);
for (int d = 1; d * d <= i; d++) {
if (i % d == 0) {
int subd = i / d;
precalc[i].push_back(d);
if (d != subd) {
precalc[i].push_back(subd);
}
}
}
sort(precalc[i].begin(), precalc[i].end());
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
cin >> n >> f;
solve();
}
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int add(int a, int b) {
long long x = a + b;
if (x >= 1000000007) x -= 1000000007;
if (x < 0) x += 1000000007;
return x;
}
long long mul(long long a, long long b) { return (a * b) % 1000000007; }
long long pw(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return ans;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand_seed() {
long long a = rng();
return a;
}
vector<int> divi[100002];
void ciur() {
for (int i = 2; i <= 100000; ++i) {
if (divi[i].empty()) {
for (int j = i; j <= 100000; j += i) divi[j].push_back(i);
}
}
}
long long fact[200002], inv[200002];
long long C(int n, int k) {
if (n < k) return 0;
return mul(fact[n], mul(inv[k], inv[n - k]));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
fact[0] = inv[0] = 1;
for (int i = 1; i <= 200000; ++i) {
fact[i] = mul(fact[i - 1], i);
inv[i] = pw(fact[i], 1000000007 - 2);
}
ciur();
int q;
cin >> q;
for (; q; --q) {
int n, t;
cin >> n >> t;
long long ans = 0;
for (int i = 0; i < (1 << (divi[n].size())); ++i) {
int odd = 0;
int prod = 1;
for (int j = 0; j < divi[n].size(); ++j)
if (i & (1 << j)) {
prod = prod * divi[n][j];
odd ^= 1;
}
if (odd == 0)
ans = add(ans, C(n / prod - 1, t - 1));
else
ans = add(ans, -C(n / prod - 1, t - 1));
}
cout << ans << '\n';
}
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int add(int a, int b) {
long long x = a + b;
if (x >= 1000000007) x -= 1000000007;
if (x < 0) x += 1000000007;
return x;
}
long long mul(long long a, long long b) { return (a * b) % 1000000007; }
long long pw(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return ans;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand_seed() {
long long a = rng();
return a;
}
vector<int> divi[100002];
void ciur() {
for (int i = 2; i <= 100000; ++i) {
if (divi[i].empty()) {
for (int j = i; j <= 100000; j += i) divi[j].push_back(i);
}
}
}
long long fact[200002], inv[200002];
long long C(int n, int k) {
if (n < k) return 0;
return mul(fact[n], mul(inv[k], inv[n - k]));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
fact[0] = inv[0] = 1;
for (int i = 1; i <= 200000; ++i) {
fact[i] = mul(fact[i - 1], i);
inv[i] = pw(fact[i], 1000000007 - 2);
}
ciur();
int q;
cin >> q;
for (; q; --q) {
int n, t;
cin >> n >> t;
long long ans = 0;
for (int i = 0; i < (1 << (divi[n].size())); ++i) {
int odd = 0;
int prod = 1;
for (int j = 0; j < divi[n].size(); ++j)
if (i & (1 << j)) {
prod = prod * divi[n][j];
odd ^= 1;
}
if (odd == 0)
ans = add(ans, C(n / prod - 1, t - 1));
else
ans = add(ans, -C(n / prod - 1, t - 1));
}
cout << ans << '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long ff[100100], inv_ff[100100];
int q, n, f, prime[100100], sqr[100100], mu[100100], ans;
vector<int> divi[100100];
long long inv_mod(long long x) {
long long ret = 1LL, y = MOD - 2;
while (y) {
if (y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD, y >>= 1;
}
return ret;
}
long long C(int x, int y) {
if (y > x) return 0;
return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD;
}
void precompute() {
inv_ff[0] = 1LL;
ff[0] = 1LL;
for (int i = 1; i <= 100099; i++) {
ff[i] = (ff[i - 1] * i) % MOD;
inv_ff[i] = inv_mod(ff[i]);
prime[i] = 1;
}
for (int i = 1; i <= 100099; i++) {
for (int j = i; j <= 100099; j += i) {
divi[j].push_back(i);
}
}
prime[0] = 0, prime[1] = 0;
for (int i = 2; i <= 100099; i++) {
if (prime[i] == 1) {
for (int j = i + i; j <= 100099; j += i) prime[j] = 0;
}
}
for (int i = 2; i * i <= 100099; i++) {
for (int j = (i * i); j <= 100099; j += (i * i)) sqr[j] = 1;
}
for (int i = 1; i <= 100099; i++) {
if (sqr[i]) continue;
int cnt = 0;
for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++)
if (prime[*d]) cnt++;
mu[i] = (cnt % 2 == 1) ? -1 : 1;
}
return;
}
int main() {
precompute();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
if (n == 1 and f == 1) {
printf("1\n");
continue;
}
if (n > 1 and f == 1) {
printf("0\n");
continue;
}
ans = 0;
for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end();
d++) {
ans = ans + mu[*d] * C((n / (*d)) - 1, f - 1);
if (ans < 0)
ans += MOD;
else if (ans >= MOD)
ans -= MOD;
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long ff[100100], inv_ff[100100];
int q, n, f, prime[100100], sqr[100100], mu[100100], ans;
vector<int> divi[100100];
long long inv_mod(long long x) {
long long ret = 1LL, y = MOD - 2;
while (y) {
if (y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD, y >>= 1;
}
return ret;
}
long long C(int x, int y) {
if (y > x) return 0;
return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD;
}
void precompute() {
inv_ff[0] = 1LL;
ff[0] = 1LL;
for (int i = 1; i <= 100099; i++) {
ff[i] = (ff[i - 1] * i) % MOD;
inv_ff[i] = inv_mod(ff[i]);
prime[i] = 1;
}
for (int i = 1; i <= 100099; i++) {
for (int j = i; j <= 100099; j += i) {
divi[j].push_back(i);
}
}
prime[0] = 0, prime[1] = 0;
for (int i = 2; i <= 100099; i++) {
if (prime[i] == 1) {
for (int j = i + i; j <= 100099; j += i) prime[j] = 0;
}
}
for (int i = 2; i * i <= 100099; i++) {
for (int j = (i * i); j <= 100099; j += (i * i)) sqr[j] = 1;
}
for (int i = 1; i <= 100099; i++) {
if (sqr[i]) continue;
int cnt = 0;
for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++)
if (prime[*d]) cnt++;
mu[i] = (cnt % 2 == 1) ? -1 : 1;
}
return;
}
int main() {
precompute();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
if (n == 1 and f == 1) {
printf("1\n");
continue;
}
if (n > 1 and f == 1) {
printf("0\n");
continue;
}
ans = 0;
for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end();
d++) {
ans = ans + mu[*d] * C((n / (*d)) - 1, f - 1);
if (ans < 0)
ans += MOD;
else if (ans >= MOD)
ans -= MOD;
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int m, n, f, h[200000], fakt[200000] = {1};
vector<int> v[200000];
long long int fe(long long int m, long long int n) {
if (!n) return 1;
long long int t = fe(m, n / 2);
t = t * t % 1000000007LL;
if (n & 1) t = t * m % 1000000007LL;
return t;
}
long long int comb(long long int m, long long int n) {
return fakt[m] * fe(fakt[n], 1000000007LL - 2) % 1000000007LL *
fe(fakt[m - n], 1000000007LL - 2) % 1000000007LL;
}
long long int find(long long int n) { return comb(n - 1, f - 1); }
long long int rec(int ind, long long int carp, int s) {
if (n / carp < f) return 0;
if (ind == v[n].size()) {
if (s & 1)
return (-find(n / carp) + 1000000007LL) % 1000000007LL;
else
return find(n / carp);
}
return (rec(ind + 1, carp, s) + rec(ind + 1, carp * v[n][ind], s + 1)) %
1000000007LL;
}
int main() {
ios_base::sync_with_stdio(false);
for (int i = 2; i <= 100005; i++)
if (!h[i]) {
for (int j = 2 * i; j <= 100005; j += i) {
v[j].push_back(i);
h[j] = 1;
}
}
for (int i = 1; i <= 100005; i++) fakt[i] = fakt[i - 1] * i % 1000000007LL;
cin >> m;
while (m--) {
cin >> n >> f;
if (f == 1 and n != 1)
cout << 0 << endl;
else if (f == 1)
cout << 1 << endl;
else
cout << rec(0, 1, 0) << endl;
}
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int m, n, f, h[200000], fakt[200000] = {1};
vector<int> v[200000];
long long int fe(long long int m, long long int n) {
if (!n) return 1;
long long int t = fe(m, n / 2);
t = t * t % 1000000007LL;
if (n & 1) t = t * m % 1000000007LL;
return t;
}
long long int comb(long long int m, long long int n) {
return fakt[m] * fe(fakt[n], 1000000007LL - 2) % 1000000007LL *
fe(fakt[m - n], 1000000007LL - 2) % 1000000007LL;
}
long long int find(long long int n) { return comb(n - 1, f - 1); }
long long int rec(int ind, long long int carp, int s) {
if (n / carp < f) return 0;
if (ind == v[n].size()) {
if (s & 1)
return (-find(n / carp) + 1000000007LL) % 1000000007LL;
else
return find(n / carp);
}
return (rec(ind + 1, carp, s) + rec(ind + 1, carp * v[n][ind], s + 1)) %
1000000007LL;
}
int main() {
ios_base::sync_with_stdio(false);
for (int i = 2; i <= 100005; i++)
if (!h[i]) {
for (int j = 2 * i; j <= 100005; j += i) {
v[j].push_back(i);
h[j] = 1;
}
}
for (int i = 1; i <= 100005; i++) fakt[i] = fakt[i - 1] * i % 1000000007LL;
cin >> m;
while (m--) {
cin >> n >> f;
if (f == 1 and n != 1)
cout << 0 << endl;
else if (f == 1)
cout << 1 << endl;
else
cout << rec(0, 1, 0) << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5 + 5;
const int Mod = 1e9 + 7;
long long jc[Maxn], inv[Maxn];
int di[Maxn][300];
long long getInv(long long a) {
int b = Mod - 2;
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % Mod;
a = a * a % Mod;
b >>= 1;
}
return ret;
}
void prework() {
jc[0] = 1;
for (int i = 1; i <= 100000; i++) {
jc[i] = jc[i - 1] * i % Mod;
inv[i] = getInv(jc[i]);
}
for (int n = 1; n <= 100000; n++) {
di[n][++di[n][0]] = n;
for (int j = n << 1; j <= 100000; j += n) di[j][++di[j][0]] = n;
}
}
inline long long C(int n, int m) {
if (m > n) return 0;
if (n == m) return 1;
return jc[n] * inv[m] % Mod * inv[n - m] % Mod;
}
long long ans[Maxn];
int isd[Maxn];
int main() {
prework();
int q, n, f;
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
for (int i = 1; i <= di[n][0]; i++) ans[di[n][i]] = 0;
for (int i = 1; i <= di[n][0]; i++) {
ans[di[n][i]] += C(di[n][i] - 1, f - 1);
int d = n / di[n][i];
for (int j = 2; j <= di[d][0]; j++)
ans[di[d][j] * di[n][i]] -= ans[di[n][i]];
}
ans[n] = (ans[n] % Mod + Mod) % Mod;
if (f == 1) {
if (n == 1)
puts("1");
else
puts("0");
} else
printf("%I64d\n", ans[n]);
}
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5 + 5;
const int Mod = 1e9 + 7;
long long jc[Maxn], inv[Maxn];
int di[Maxn][300];
long long getInv(long long a) {
int b = Mod - 2;
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % Mod;
a = a * a % Mod;
b >>= 1;
}
return ret;
}
void prework() {
jc[0] = 1;
for (int i = 1; i <= 100000; i++) {
jc[i] = jc[i - 1] * i % Mod;
inv[i] = getInv(jc[i]);
}
for (int n = 1; n <= 100000; n++) {
di[n][++di[n][0]] = n;
for (int j = n << 1; j <= 100000; j += n) di[j][++di[j][0]] = n;
}
}
inline long long C(int n, int m) {
if (m > n) return 0;
if (n == m) return 1;
return jc[n] * inv[m] % Mod * inv[n - m] % Mod;
}
long long ans[Maxn];
int isd[Maxn];
int main() {
prework();
int q, n, f;
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
for (int i = 1; i <= di[n][0]; i++) ans[di[n][i]] = 0;
for (int i = 1; i <= di[n][0]; i++) {
ans[di[n][i]] += C(di[n][i] - 1, f - 1);
int d = n / di[n][i];
for (int j = 2; j <= di[d][0]; j++)
ans[di[d][j] * di[n][i]] -= ans[di[n][i]];
}
ans[n] = (ans[n] % Mod + Mod) % Mod;
if (f == 1) {
if (n == 1)
puts("1");
else
puts("0");
} else
printf("%I64d\n", ans[n]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int const MOD = 1e9 + 7;
int const MAXN = 1e5 + 7;
int is_prime[MAXN];
int fact[MAXN];
int invfact[MAXN];
int inv(int a, int p) {
int res = 1LL;
int b = p - 2;
int tmp = a;
while (b) {
if (b & 1) res = (res * 1LL * tmp) % p;
tmp = (tmp * 1LL * tmp) % p;
b >>= 1;
}
return res;
}
int getC(int n, int k) {
if (n < k) return 0;
long long ans = (fact[n] * 1LL * invfact[k]) % MOD;
ans = (ans * 1LL * invfact[n - k]) % MOD;
return ans;
}
int mu[MAXN] = {};
void init() {
for (int i = 0; i < (int)MAXN; ++i) {
is_prime[i] = 0;
mu[i] = 1;
}
for (int i = 2; i < MAXN; ++i) {
if (is_prime[i] == 0) {
mu[i] = -1;
for (int j = 2 * i; j < MAXN; j += i) {
if (is_prime[j] == 0) is_prime[j] = i;
if (j / i % i == 0) mu[j] = 0;
mu[j] *= -1;
}
}
}
fact[0] = invfact[0] = 1;
for (int i = 1; i < MAXN; ++i) {
fact[i] = (fact[i - 1] * 1LL * i) % MOD;
invfact[i] = inv(fact[i], MOD);
}
}
int der[MAXN];
pair<int, int> primes[30];
int pw(int a, int b) {
int ans = 1;
int tmp = a;
while (b) {
if (b & 1) ans *= tmp;
tmp *= tmp;
b >>= 1;
}
return ans;
}
int solve(int f, int n) {
if (f < n) return 0;
if (n == 1) return f == 1 ? 1 : 0;
if (n == f) return 1;
if (is_prime[f] == 0) {
int ans = getC(f - 1, n - 1);
return ans;
}
long long ans = getC(f - 1, n - 1);
int sz = 0;
int prsz = 0;
int tmp = f;
while (is_prime[tmp]) {
if (prsz == 0 || is_prime[tmp] != primes[prsz - 1].first)
primes[prsz++] = make_pair(is_prime[tmp], 1);
else
++primes[prsz - 1].second;
tmp /= is_prime[tmp];
}
if (tmp > 1) {
if ((prsz == 0 || tmp != primes[prsz - 1].first))
primes[prsz++] = make_pair(tmp, 1);
else
++primes[prsz - 1].second;
}
int cur = 1;
for (int i = 0; i < (int)prsz; ++i) cur *= ++primes[i].second;
cur -= 2;
for (; cur > 0; --cur) {
int r = cur;
int t = 1;
for (int i = 0; i < (int)prsz; ++i) {
t *= pw(primes[i].first, r % primes[i].second);
r /= primes[i].second;
}
der[sz++] = t;
}
for (int i = 0; i < (int)sz; ++i) {
ans = (ans + MOD + mu[f / der[i]] * getC(der[i] - 1, n - 1)) % MOD;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
init();
int q = 0;
cin >> q;
for (int i = 0; i < (int)q; ++i) {
int f, n;
cin >> f >> n;
cout << solve(f, n) << "\n";
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int const MOD = 1e9 + 7;
int const MAXN = 1e5 + 7;
int is_prime[MAXN];
int fact[MAXN];
int invfact[MAXN];
int inv(int a, int p) {
int res = 1LL;
int b = p - 2;
int tmp = a;
while (b) {
if (b & 1) res = (res * 1LL * tmp) % p;
tmp = (tmp * 1LL * tmp) % p;
b >>= 1;
}
return res;
}
int getC(int n, int k) {
if (n < k) return 0;
long long ans = (fact[n] * 1LL * invfact[k]) % MOD;
ans = (ans * 1LL * invfact[n - k]) % MOD;
return ans;
}
int mu[MAXN] = {};
void init() {
for (int i = 0; i < (int)MAXN; ++i) {
is_prime[i] = 0;
mu[i] = 1;
}
for (int i = 2; i < MAXN; ++i) {
if (is_prime[i] == 0) {
mu[i] = -1;
for (int j = 2 * i; j < MAXN; j += i) {
if (is_prime[j] == 0) is_prime[j] = i;
if (j / i % i == 0) mu[j] = 0;
mu[j] *= -1;
}
}
}
fact[0] = invfact[0] = 1;
for (int i = 1; i < MAXN; ++i) {
fact[i] = (fact[i - 1] * 1LL * i) % MOD;
invfact[i] = inv(fact[i], MOD);
}
}
int der[MAXN];
pair<int, int> primes[30];
int pw(int a, int b) {
int ans = 1;
int tmp = a;
while (b) {
if (b & 1) ans *= tmp;
tmp *= tmp;
b >>= 1;
}
return ans;
}
int solve(int f, int n) {
if (f < n) return 0;
if (n == 1) return f == 1 ? 1 : 0;
if (n == f) return 1;
if (is_prime[f] == 0) {
int ans = getC(f - 1, n - 1);
return ans;
}
long long ans = getC(f - 1, n - 1);
int sz = 0;
int prsz = 0;
int tmp = f;
while (is_prime[tmp]) {
if (prsz == 0 || is_prime[tmp] != primes[prsz - 1].first)
primes[prsz++] = make_pair(is_prime[tmp], 1);
else
++primes[prsz - 1].second;
tmp /= is_prime[tmp];
}
if (tmp > 1) {
if ((prsz == 0 || tmp != primes[prsz - 1].first))
primes[prsz++] = make_pair(tmp, 1);
else
++primes[prsz - 1].second;
}
int cur = 1;
for (int i = 0; i < (int)prsz; ++i) cur *= ++primes[i].second;
cur -= 2;
for (; cur > 0; --cur) {
int r = cur;
int t = 1;
for (int i = 0; i < (int)prsz; ++i) {
t *= pw(primes[i].first, r % primes[i].second);
r /= primes[i].second;
}
der[sz++] = t;
}
for (int i = 0; i < (int)sz; ++i) {
ans = (ans + MOD + mu[f / der[i]] * getC(der[i] - 1, n - 1)) % MOD;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
init();
int q = 0;
cin >> q;
for (int i = 0; i < (int)q; ++i) {
int f, n;
cin >> f >> n;
cout << solve(f, n) << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
return (b) == 0 ? (a) : gcd((b), ((a) % (b)));
}
template <class T>
inline T lcm(T a, T b) {
return ((a) / gcd((a), (b)) * (b));
}
template <class T>
inline T BigMod(T Base, T power, T M = 1000000007) {
if (power == 0) return 1;
if (power & 1)
return ((Base % M) * (BigMod(Base, power - 1, M) % M)) % M;
else {
T y = BigMod(Base, power / 2, M) % M;
return (y * y) % M;
}
}
template <class T>
inline T ModInv(T A, T M = 1000000007) {
return BigMod(A, M - 2, M);
}
int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0};
int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0};
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long long f[500005], inv[500005];
vector<int> v[500005];
int miu[500005], mark[500005];
void mobius(int n) {
for (int i = 0; i <= n; i++) miu[i] = 1;
miu[0] = 0;
miu[1] = 1;
for (int i = 2; i <= n; i++) {
if (mark[i]) continue;
miu[i] = -1;
for (int j = i * 2; j <= n; j += i) {
if (miu[j] != 0) {
if (j % (i * i) == 0)
miu[j] = 0;
else
miu[j] *= -1;
mark[j] = 1;
}
}
}
}
void pre() {
f[0] = 1;
for (int i = 1; i < 500005; i++) f[i] = (f[i - 1] * i) % 1000000007;
inv[500005 - 1] = ModInv<long long>(f[500005 - 1]);
for (int i = 500005 - 2; i >= 0; i--)
inv[i] = (inv[i + 1] * (i + 1)) % 1000000007;
for (int i = 1; i < 500005; i++) {
for (int j = i; j < 500005; j += i) v[j].push_back(i);
}
}
long long pk(int n, int r) {
n--;
r--;
if (n < r) return 0;
return ((f[n] * inv[r]) % 1000000007 * inv[n - r]) % 1000000007;
}
long long giveres(int n, int r) {
long long ans = 0;
for (auto it : v[n]) {
ans += (miu[it] * pk(n / it, r));
if (ans < 0) ans += 1000000007;
if (ans >= 1000000007) ans %= 1000000007;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
pre();
mobius(500005 - 1);
int t;
cin >> t;
while (t--) {
int n, r;
cin >> n >> r;
cout << giveres(n, r) << endl;
}
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
return (b) == 0 ? (a) : gcd((b), ((a) % (b)));
}
template <class T>
inline T lcm(T a, T b) {
return ((a) / gcd((a), (b)) * (b));
}
template <class T>
inline T BigMod(T Base, T power, T M = 1000000007) {
if (power == 0) return 1;
if (power & 1)
return ((Base % M) * (BigMod(Base, power - 1, M) % M)) % M;
else {
T y = BigMod(Base, power / 2, M) % M;
return (y * y) % M;
}
}
template <class T>
inline T ModInv(T A, T M = 1000000007) {
return BigMod(A, M - 2, M);
}
int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0};
int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0};
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long long f[500005], inv[500005];
vector<int> v[500005];
int miu[500005], mark[500005];
void mobius(int n) {
for (int i = 0; i <= n; i++) miu[i] = 1;
miu[0] = 0;
miu[1] = 1;
for (int i = 2; i <= n; i++) {
if (mark[i]) continue;
miu[i] = -1;
for (int j = i * 2; j <= n; j += i) {
if (miu[j] != 0) {
if (j % (i * i) == 0)
miu[j] = 0;
else
miu[j] *= -1;
mark[j] = 1;
}
}
}
}
void pre() {
f[0] = 1;
for (int i = 1; i < 500005; i++) f[i] = (f[i - 1] * i) % 1000000007;
inv[500005 - 1] = ModInv<long long>(f[500005 - 1]);
for (int i = 500005 - 2; i >= 0; i--)
inv[i] = (inv[i + 1] * (i + 1)) % 1000000007;
for (int i = 1; i < 500005; i++) {
for (int j = i; j < 500005; j += i) v[j].push_back(i);
}
}
long long pk(int n, int r) {
n--;
r--;
if (n < r) return 0;
return ((f[n] * inv[r]) % 1000000007 * inv[n - r]) % 1000000007;
}
long long giveres(int n, int r) {
long long ans = 0;
for (auto it : v[n]) {
ans += (miu[it] * pk(n / it, r));
if (ans < 0) ans += 1000000007;
if (ans >= 1000000007) ans %= 1000000007;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
pre();
mobius(500005 - 1);
int t;
cin >> t;
while (t--) {
int n, r;
cin >> n >> r;
cout << giveres(n, r) << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <class T>
ostream& prnt(ostream& out, T v) {
out << v.size() << '\n';
for (auto e : v) out << e << ' ';
return out;
}
template <class T>
ostream& operator<<(ostream& out, vector<T> v) {
return prnt(out, v);
}
template <class T>
ostream& operator<<(ostream& out, set<T> v) {
return prnt(out, v);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, map<T1, T2> v) {
return prnt(out, v);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> p) {
return out << '(' << p.first << ' ' << p.second << ')';
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
const long long MOD = 1e9 + 7;
const long long N = 100100;
long long n = 6, s, MAX = 31, nr, f[N];
map<pair<long long, long long>, long long> dp;
vector<long long> d[N];
long long powMod(long long base, long long exp) {
if (exp == 0) return 1;
long long tmp = powMod(base, exp / 2);
tmp = (1LL * tmp * tmp) % MOD;
if (exp % 2) tmp = (tmp * base) % MOD;
return tmp;
}
long long invMod(long long a) { return powMod(a, MOD - 2); }
long long comb(long long n, long long k) {
return (1LL * f[n] * invMod((1LL * f[k] * f[n - k]) % MOD)) % MOD;
}
long long F(long long n, long long f) {
if (n < f) return 0;
if (dp.count({n, f})) return dp[{n, f}];
long long ans = comb(n - 1, f - 1);
for (auto i : d[n]) {
ans -= F(n / i, f);
ans %= MOD;
}
dp[{n, f}] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
f[0] = 1;
for (long long i = 1; i < N; i++) f[i] = (i * f[i - 1]) % MOD;
for (long long i = 2; i < N; i++)
for (long long j = i; j < N; j += i) d[j].push_back(i);
long long q, a, b;
cin >> q;
for (long long i = 1; i <= q; i++) {
cin >> a >> b;
cout << (F(a, b) + MOD) % MOD << '\n';
}
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <class T>
ostream& prnt(ostream& out, T v) {
out << v.size() << '\n';
for (auto e : v) out << e << ' ';
return out;
}
template <class T>
ostream& operator<<(ostream& out, vector<T> v) {
return prnt(out, v);
}
template <class T>
ostream& operator<<(ostream& out, set<T> v) {
return prnt(out, v);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, map<T1, T2> v) {
return prnt(out, v);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> p) {
return out << '(' << p.first << ' ' << p.second << ')';
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
const long long MOD = 1e9 + 7;
const long long N = 100100;
long long n = 6, s, MAX = 31, nr, f[N];
map<pair<long long, long long>, long long> dp;
vector<long long> d[N];
long long powMod(long long base, long long exp) {
if (exp == 0) return 1;
long long tmp = powMod(base, exp / 2);
tmp = (1LL * tmp * tmp) % MOD;
if (exp % 2) tmp = (tmp * base) % MOD;
return tmp;
}
long long invMod(long long a) { return powMod(a, MOD - 2); }
long long comb(long long n, long long k) {
return (1LL * f[n] * invMod((1LL * f[k] * f[n - k]) % MOD)) % MOD;
}
long long F(long long n, long long f) {
if (n < f) return 0;
if (dp.count({n, f})) return dp[{n, f}];
long long ans = comb(n - 1, f - 1);
for (auto i : d[n]) {
ans -= F(n / i, f);
ans %= MOD;
}
dp[{n, f}] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
f[0] = 1;
for (long long i = 1; i < N; i++) f[i] = (i * f[i - 1]) % MOD;
for (long long i = 2; i < N; i++)
for (long long j = i; j < N; j += i) d[j].push_back(i);
long long q, a, b;
cin >> q;
for (long long i = 1; i <= q; i++) {
cin >> a >> b;
cout << (F(a, b) + MOD) % MOD << '\n';
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int const MOD = 1e9 + 7;
int const MAXN = 1e5 + 7;
bool is_prime[MAXN];
int fact[MAXN];
int invfact[MAXN];
int inv(int a, int p) {
int res = 1LL;
int b = p - 2;
int tmp = a;
while (b) {
if (b & 1) res = (res * 1LL * tmp) % p;
tmp = (tmp * 1LL * tmp) % p;
b >>= 1;
}
return res;
}
int getC(int n, int k) {
if (n < k) return 0;
int ans = (fact[n] * 1LL * invfact[k]) % MOD;
ans = (ans * 1LL * invfact[n - k]) % MOD;
return ans;
}
int mu[MAXN] = {};
void init() {
for (int i = 0; i < (int)MAXN; ++i) {
is_prime[i] = true;
mu[i] = 1;
}
for (int i = 2; i <= MAXN; ++i) {
if (is_prime[i]) {
mu[i] = -1;
for (int j = 2 * i; j < MAXN; j += i) {
is_prime[j] = false;
if (j / i % i == 0) mu[j] = 0;
mu[j] *= -1;
}
}
}
fact[0] = invfact[0] = 1;
for (int i = 1; i < MAXN; ++i) {
fact[i] = (fact[i - 1] * 1LL * i) % MOD;
invfact[i] = inv(fact[i], MOD);
}
}
int der[MAXN];
int sz = 0;
int primes[MAXN];
int prsz = 0;
int solve(int f, int n, int index = 2) {
if (f < n) return 0;
if (n == 1) return f == 1 ? 1 : 0;
if (n == f) return 1;
if (is_prime[f]) {
int ans = getC(f - 1, n - 1);
return ans;
}
long long ans = getC(f - 1, n - 1);
int tmp = f;
if (index == 2) {
sz = prsz = 0;
for (; index * 1LL * index <= f; ++index)
if (f % index == 0) {
der[sz++] = index;
if (index * 1LL * index < f) {
der[sz++] = f / index;
if (is_prime[f / index]) primes[prsz++] = f / index;
}
if (is_prime[index]) primes[prsz++] = index;
}
}
for (int i = 0; i < (int)sz; ++i) {
ans = (ans + MOD + mu[f / der[i]] * getC(der[i] - 1, n - 1)) % MOD;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
init();
int q = 0;
cin >> q;
for (int i = 0; i < (int)q; ++i) {
int f, n;
cin >> f >> n;
cout << solve(f, n) << "\n";
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int const MOD = 1e9 + 7;
int const MAXN = 1e5 + 7;
bool is_prime[MAXN];
int fact[MAXN];
int invfact[MAXN];
int inv(int a, int p) {
int res = 1LL;
int b = p - 2;
int tmp = a;
while (b) {
if (b & 1) res = (res * 1LL * tmp) % p;
tmp = (tmp * 1LL * tmp) % p;
b >>= 1;
}
return res;
}
int getC(int n, int k) {
if (n < k) return 0;
int ans = (fact[n] * 1LL * invfact[k]) % MOD;
ans = (ans * 1LL * invfact[n - k]) % MOD;
return ans;
}
int mu[MAXN] = {};
void init() {
for (int i = 0; i < (int)MAXN; ++i) {
is_prime[i] = true;
mu[i] = 1;
}
for (int i = 2; i <= MAXN; ++i) {
if (is_prime[i]) {
mu[i] = -1;
for (int j = 2 * i; j < MAXN; j += i) {
is_prime[j] = false;
if (j / i % i == 0) mu[j] = 0;
mu[j] *= -1;
}
}
}
fact[0] = invfact[0] = 1;
for (int i = 1; i < MAXN; ++i) {
fact[i] = (fact[i - 1] * 1LL * i) % MOD;
invfact[i] = inv(fact[i], MOD);
}
}
int der[MAXN];
int sz = 0;
int primes[MAXN];
int prsz = 0;
int solve(int f, int n, int index = 2) {
if (f < n) return 0;
if (n == 1) return f == 1 ? 1 : 0;
if (n == f) return 1;
if (is_prime[f]) {
int ans = getC(f - 1, n - 1);
return ans;
}
long long ans = getC(f - 1, n - 1);
int tmp = f;
if (index == 2) {
sz = prsz = 0;
for (; index * 1LL * index <= f; ++index)
if (f % index == 0) {
der[sz++] = index;
if (index * 1LL * index < f) {
der[sz++] = f / index;
if (is_prime[f / index]) primes[prsz++] = f / index;
}
if (is_prime[index]) primes[prsz++] = index;
}
}
for (int i = 0; i < (int)sz; ++i) {
ans = (ans + MOD + mu[f / der[i]] * getC(der[i] - 1, n - 1)) % MOD;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
init();
int q = 0;
cin >> q;
for (int i = 0; i < (int)q; ++i) {
int f, n;
cin >> f >> n;
cout << solve(f, n) << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const long long MOD = 1e9 + 7;
inline long long modulo(long long x) {
if (0 <= x and x < MOD) return x;
return (x > 0) ? x % MOD : (x % MOD) + MOD;
}
pair<long long, long long> gcd_ex(long long a, long long b) {
if (b == 0) return make_pair(1, 0);
pair<long long, long long> ans = gcd_ex(b, a % b);
return make_pair(ans.second, ans.first - ans.second * (a / b));
}
long long Mod_inv(long long x) { return modulo(gcd_ex(x, MOD).first); }
int u[MAX], primo[MAX];
long long fact[MAX], inv_fact[MAX];
vector<int> d[MAX];
void pre() {
fact[0] = inv_fact[0] = 1;
for (int i = 0; i < (int)(MAX); i++) {
fact[i + 1] = modulo((i + 1) * fact[i]);
inv_fact[i + 1] = Mod_inv(fact[i + 1]);
}
}
void criba() {
memset(primo, 1, sizeof(primo));
primo[0] = primo[1] = 0;
for (int i = 2; i * i < MAX; i++) {
if (primo[i])
for (int j = i * i; j < MAX; j += i) primo[j] = 0;
}
memset(u, 0, sizeof(u));
u[1] = 1;
for (int i = 2; i < MAX; i++) {
if (!primo[i]) continue;
u[i] = -1;
for (int j = 2 * i; j < MAX; j += i) {
int aux = j / i;
if (aux % i) u[j] = -u[aux];
}
}
for (int i = 1; i < MAX; i++) {
for (int j = i; j < MAX; j += i) d[j].push_back(i);
}
}
inline long long comb(int x, int f) {
if (x < f) return 0;
return modulo(modulo(fact[x] * inv_fact[f]) * inv_fact[x - f]);
}
int main() {
pre();
criba();
int t, n, f;
cin >> t;
while (t--) {
cin >> n >> f;
long long ans = 0;
for (int i = 0; i < (int)(d[n].size()); i++) {
int t = d[n][i];
ans = modulo(ans + u[t] * comb(n / t - 1, f - 1));
}
cout << ans << endl;
}
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const long long MOD = 1e9 + 7;
inline long long modulo(long long x) {
if (0 <= x and x < MOD) return x;
return (x > 0) ? x % MOD : (x % MOD) + MOD;
}
pair<long long, long long> gcd_ex(long long a, long long b) {
if (b == 0) return make_pair(1, 0);
pair<long long, long long> ans = gcd_ex(b, a % b);
return make_pair(ans.second, ans.first - ans.second * (a / b));
}
long long Mod_inv(long long x) { return modulo(gcd_ex(x, MOD).first); }
int u[MAX], primo[MAX];
long long fact[MAX], inv_fact[MAX];
vector<int> d[MAX];
void pre() {
fact[0] = inv_fact[0] = 1;
for (int i = 0; i < (int)(MAX); i++) {
fact[i + 1] = modulo((i + 1) * fact[i]);
inv_fact[i + 1] = Mod_inv(fact[i + 1]);
}
}
void criba() {
memset(primo, 1, sizeof(primo));
primo[0] = primo[1] = 0;
for (int i = 2; i * i < MAX; i++) {
if (primo[i])
for (int j = i * i; j < MAX; j += i) primo[j] = 0;
}
memset(u, 0, sizeof(u));
u[1] = 1;
for (int i = 2; i < MAX; i++) {
if (!primo[i]) continue;
u[i] = -1;
for (int j = 2 * i; j < MAX; j += i) {
int aux = j / i;
if (aux % i) u[j] = -u[aux];
}
}
for (int i = 1; i < MAX; i++) {
for (int j = i; j < MAX; j += i) d[j].push_back(i);
}
}
inline long long comb(int x, int f) {
if (x < f) return 0;
return modulo(modulo(fact[x] * inv_fact[f]) * inv_fact[x - f]);
}
int main() {
pre();
criba();
int t, n, f;
cin >> t;
while (t--) {
cin >> n >> f;
long long ans = 0;
for (int i = 0; i < (int)(d[n].size()); i++) {
int t = d[n][i];
ans = modulo(ans + u[t] * comb(n / t - 1, f - 1));
}
cout << ans << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long temp1, temp2, temp;
char tempc;
int mobius[100007];
int lpf[100007];
void least_prime_factor() {
for (int i = 2; i < 100007; i++)
if (!lpf[i])
for (int j = i; j < 100007; j += i)
if (!lpf[j]) lpf[j] = i;
}
void Mobius(int n) {
for (int i = 1; i < 100007; i++) {
if (i == 1)
mobius[i] = 1;
else {
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
}
long long binpow(long long a, long long b) {
a %= 1000000007;
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
b >>= 1;
}
return res % 1000000007;
}
vector<long long> fact;
long long inverse[100007];
long long inv(long long n) { return binpow(n, 1000000007 - 2) % 1000000007; }
void init() {
least_prime_factor();
Mobius(100007);
fact.push_back(1);
for (int i = 1; i < 100000 + 3; i++)
fact.push_back((fact[i - 1] * i) % 1000000007);
for (int i = 0; i < int(100002); i++) inverse[i] = inv(fact[i]);
}
long long choose(long long n, long long r) {
if (r > n) return 0;
temp = (inverse[r] * inverse[n - r]) % 1000000007;
return (fact[n] * temp) % 1000000007;
}
int main() {
init();
int q;
scanf("%d", &q);
while (q--) {
long long n, fi;
scanf("%lld%lld", &n, &fi);
if (fi == 1) {
if (n == 1)
cout << 1 << endl;
else
cout << 0 << endl;
continue;
}
long long ans = 0;
for (int i = 1; i <= sqrt(n); i++) {
if ((n % i == 0) && (i * i != n)) {
ans =
(ans + mobius[i] * choose(n / i - 1, fi - 1) +
mobius[n / i] * choose(i - 1, fi - 1) + 1000000007 + 1000000007) %
1000000007;
}
if (i * i == n) {
ans =
(ans + mobius[i] * choose(i - 1, fi - 1) + 1000000007) % 1000000007;
}
}
printf("%lld\n", ans);
}
}
|
### Prompt
Create a solution in cpp for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long temp1, temp2, temp;
char tempc;
int mobius[100007];
int lpf[100007];
void least_prime_factor() {
for (int i = 2; i < 100007; i++)
if (!lpf[i])
for (int j = i; j < 100007; j += i)
if (!lpf[j]) lpf[j] = i;
}
void Mobius(int n) {
for (int i = 1; i < 100007; i++) {
if (i == 1)
mobius[i] = 1;
else {
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
}
long long binpow(long long a, long long b) {
a %= 1000000007;
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
b >>= 1;
}
return res % 1000000007;
}
vector<long long> fact;
long long inverse[100007];
long long inv(long long n) { return binpow(n, 1000000007 - 2) % 1000000007; }
void init() {
least_prime_factor();
Mobius(100007);
fact.push_back(1);
for (int i = 1; i < 100000 + 3; i++)
fact.push_back((fact[i - 1] * i) % 1000000007);
for (int i = 0; i < int(100002); i++) inverse[i] = inv(fact[i]);
}
long long choose(long long n, long long r) {
if (r > n) return 0;
temp = (inverse[r] * inverse[n - r]) % 1000000007;
return (fact[n] * temp) % 1000000007;
}
int main() {
init();
int q;
scanf("%d", &q);
while (q--) {
long long n, fi;
scanf("%lld%lld", &n, &fi);
if (fi == 1) {
if (n == 1)
cout << 1 << endl;
else
cout << 0 << endl;
continue;
}
long long ans = 0;
for (int i = 1; i <= sqrt(n); i++) {
if ((n % i == 0) && (i * i != n)) {
ans =
(ans + mobius[i] * choose(n / i - 1, fi - 1) +
mobius[n / i] * choose(i - 1, fi - 1) + 1000000007 + 1000000007) %
1000000007;
}
if (i * i == n) {
ans =
(ans + mobius[i] * choose(i - 1, fi - 1) + 1000000007) % 1000000007;
}
}
printf("%lld\n", ans);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100500, MOD = 1e9 + 7;
struct {
int p, e;
} Fafa[10];
long long expmod(long long b, long long e) {
if (!e) return 1;
return (e % 2 ? b : 1) * expmod(b * b % MOD, e / 2) % MOD;
}
long long F[MAXN], FI[MAXN];
long long CMB(long long n, long long k) {
if (k < 0) return 0;
if (k > n) return 0;
return (F[n] * FI[k] % MOD) * FI[n - k] % MOD;
}
void solve(int m, int k) {}
int n, k, q;
map<int, int> P;
int main() {
F[0] = FI[0] = 1;
for (int i = (int)1; i < (int)MAXN; i++) F[i] = F[i - 1] * i % MOD;
for (int i = (int)1; i < (int)MAXN; i++)
FI[i] = FI[i - 1] * expmod(i, MOD - 2) % MOD;
int q;
cin >> q;
for (int cs = (int)0; cs < (int)q; cs++) {
P.clear();
scanf("%d %d", &n, &k);
int p = 2;
while (p * p <= n) {
if (n % p)
p++;
else {
if (!P.count(p))
P[p] = 1;
else
P[p]++;
;
n /= p;
}
}
if (n > 1)
if (!P.count(n))
P[n] = 1;
else
P[n]++;
;
int B = P.size();
int x = 0;
for (auto it = P.begin(); it != P.end(); ++it) {
Fafa[x].p = (it->first);
Fafa[x++].e = (it->second);
}
long long ans = 0;
for (int v = (int)0; v < (int)1 << B; v++) {
long long fact = 1;
for (int i = (int)0; i < (int)B; i++)
fact *= ((v & (1 << i)) ? expmod(Fafa[i].p, Fafa[i].e)
: -expmod(Fafa[i].p, Fafa[i].e - 1));
if (fact >= 0)
ans = (ans + CMB(fact - 1, k - 1)) % MOD;
else
ans = (ans - CMB(-fact - 1, k - 1)) % MOD;
}
printf("%lld\n", (ans + MOD) % MOD);
}
}
|
### Prompt
Create a solution in CPP for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100500, MOD = 1e9 + 7;
struct {
int p, e;
} Fafa[10];
long long expmod(long long b, long long e) {
if (!e) return 1;
return (e % 2 ? b : 1) * expmod(b * b % MOD, e / 2) % MOD;
}
long long F[MAXN], FI[MAXN];
long long CMB(long long n, long long k) {
if (k < 0) return 0;
if (k > n) return 0;
return (F[n] * FI[k] % MOD) * FI[n - k] % MOD;
}
void solve(int m, int k) {}
int n, k, q;
map<int, int> P;
int main() {
F[0] = FI[0] = 1;
for (int i = (int)1; i < (int)MAXN; i++) F[i] = F[i - 1] * i % MOD;
for (int i = (int)1; i < (int)MAXN; i++)
FI[i] = FI[i - 1] * expmod(i, MOD - 2) % MOD;
int q;
cin >> q;
for (int cs = (int)0; cs < (int)q; cs++) {
P.clear();
scanf("%d %d", &n, &k);
int p = 2;
while (p * p <= n) {
if (n % p)
p++;
else {
if (!P.count(p))
P[p] = 1;
else
P[p]++;
;
n /= p;
}
}
if (n > 1)
if (!P.count(n))
P[n] = 1;
else
P[n]++;
;
int B = P.size();
int x = 0;
for (auto it = P.begin(); it != P.end(); ++it) {
Fafa[x].p = (it->first);
Fafa[x++].e = (it->second);
}
long long ans = 0;
for (int v = (int)0; v < (int)1 << B; v++) {
long long fact = 1;
for (int i = (int)0; i < (int)B; i++)
fact *= ((v & (1 << i)) ? expmod(Fafa[i].p, Fafa[i].e)
: -expmod(Fafa[i].p, Fafa[i].e - 1));
if (fact >= 0)
ans = (ans + CMB(fact - 1, k - 1)) % MOD;
else
ans = (ans - CMB(-fact - 1, k - 1)) % MOD;
}
printf("%lld\n", (ans + MOD) % MOD);
}
}
```
|
#include <bits/stdc++.h>
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
int F[MAXN], iverF[MAXN];
int prime[30], sz;
int pow2(int n) { return 1 << n; }
int pow(int a, int n) {
int res = 1;
while (n) {
if (n & 1) res = 1ll * res * a % MOD;
a = 1ll * a * a % MOD;
n >>= 1;
}
return res;
}
void init() {
F[0] = 1;
for (int i = 1; i < MAXN; i++) F[i] = 1ll * F[i - 1] * i % MOD;
for (int i = 0; i < MAXN; i++) iverF[i] = pow(F[i], MOD - 2);
}
int C(int n, int m) {
if (n < m) return 0;
return ((1ll * F[n] * iverF[m]) % MOD) * 1ll * iverF[n - m] % MOD;
}
void find_prime(int n) {
sz = 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
prime[sz++] = i;
while (n % i == 0) {
n /= i;
}
}
}
if (n != 1) prime[sz++] = n;
}
void solve(int n, int f) {
find_prime(n);
int ans = 0;
for (int i = 0; i < pow2(sz); i++) {
int tmp = 1, flag = 1;
for (int j = 0; j < sz; j++) {
if (i & pow2(j)) {
tmp *= prime[j];
flag = -flag;
}
}
if (flag < 0) {
ans = (ans + MOD + flag * C(n / tmp - 1, f - 1)) % MOD;
} else {
ans = (ans + flag * C(n / tmp - 1, f - 1)) % MOD;
}
}
printf("%d\n", ans);
}
int main() {
init();
int q;
scanf("%d", &q);
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
solve(n, f);
}
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
int F[MAXN], iverF[MAXN];
int prime[30], sz;
int pow2(int n) { return 1 << n; }
int pow(int a, int n) {
int res = 1;
while (n) {
if (n & 1) res = 1ll * res * a % MOD;
a = 1ll * a * a % MOD;
n >>= 1;
}
return res;
}
void init() {
F[0] = 1;
for (int i = 1; i < MAXN; i++) F[i] = 1ll * F[i - 1] * i % MOD;
for (int i = 0; i < MAXN; i++) iverF[i] = pow(F[i], MOD - 2);
}
int C(int n, int m) {
if (n < m) return 0;
return ((1ll * F[n] * iverF[m]) % MOD) * 1ll * iverF[n - m] % MOD;
}
void find_prime(int n) {
sz = 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
prime[sz++] = i;
while (n % i == 0) {
n /= i;
}
}
}
if (n != 1) prime[sz++] = n;
}
void solve(int n, int f) {
find_prime(n);
int ans = 0;
for (int i = 0; i < pow2(sz); i++) {
int tmp = 1, flag = 1;
for (int j = 0; j < sz; j++) {
if (i & pow2(j)) {
tmp *= prime[j];
flag = -flag;
}
}
if (flag < 0) {
ans = (ans + MOD + flag * C(n / tmp - 1, f - 1)) % MOD;
} else {
ans = (ans + flag * C(n / tmp - 1, f - 1)) % MOD;
}
}
printf("%d\n", ans);
}
int main() {
init();
int q;
scanf("%d", &q);
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
solve(n, f);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long fact[(int)(1e5 + 5)], fact_inv[(int)(1e5 + 5)];
vector<int> prime[(int)(1e5 + 5)];
int *v;
int n, f;
long long power(long long a, int x) {
long long res = 1;
while (x) {
if (x % 2 == 1) res = (res * a) % (int)(1e9 + 7);
a = (a * a) % (int)(1e9 + 7);
x /= 2;
}
return res;
}
void eratos_sieve() {
for (int i = 2; i < (int)(1e5 + 5) / 2; ++i)
if (prime[i].empty()) {
for (int j = i; j < (int)(1e5 + 5); j += i) prime[j].push_back(i);
}
}
void init_fact() {
fact[0] = fact_inv[0] = 1;
for (int i = 1; i < (int)(1e5 + 5); ++i) {
fact[i] = (fact[i - 1] * i) % (int)(1e9 + 7);
fact_inv[i] = power(fact[i], (int)(1e9 + 7) - 2);
}
}
void init() {
eratos_sieve();
init_fact();
}
long long C(int n, int k) {
if (k > n) return 0;
long long res = fact[n];
res = (res * fact_inv[k]) % (int)(1e9 + 7);
res = (res * fact_inv[n - k]) % (int)(1e9 + 7);
return res;
}
int getbit(int x, int y) { return (x >> y) & 1; }
long long incl_excl() {
long long res = 0;
int m = prime[n].size();
v = (int *)calloc(m + 1, sizeof(int));
for (int i = 1; i <= m; ++i) v[i] = prime[n][i - 1];
int last = (1 << m) - 1;
for (int state = 1; state <= last; ++state) {
int bits = 0;
int tmp = 1;
for (int i = 1; i <= m; ++i)
if (getbit(state, i - 1)) {
++bits;
tmp *= v[i];
}
tmp = C(n / tmp - 1, f - 1);
if (bits % 2)
res += tmp;
else
res -= tmp;
if (res > (int)(1e9 + 7)) res -= (int)(1e9 + 7);
if (res < 0) res += (int)(1e9 + 7);
}
free(v);
return res;
}
long long sol() {
long long res =
(C(n - 1, f - 1) - incl_excl() + (int)(1e9 + 7)) % (int)(1e9 + 7);
return res;
}
void inp() {
init();
int q;
cin >> q;
while (q--) {
cin >> n >> f;
assert((n <= (int)(1e5 + 5) && f <= n));
cout << sol() << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inp();
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long fact[(int)(1e5 + 5)], fact_inv[(int)(1e5 + 5)];
vector<int> prime[(int)(1e5 + 5)];
int *v;
int n, f;
long long power(long long a, int x) {
long long res = 1;
while (x) {
if (x % 2 == 1) res = (res * a) % (int)(1e9 + 7);
a = (a * a) % (int)(1e9 + 7);
x /= 2;
}
return res;
}
void eratos_sieve() {
for (int i = 2; i < (int)(1e5 + 5) / 2; ++i)
if (prime[i].empty()) {
for (int j = i; j < (int)(1e5 + 5); j += i) prime[j].push_back(i);
}
}
void init_fact() {
fact[0] = fact_inv[0] = 1;
for (int i = 1; i < (int)(1e5 + 5); ++i) {
fact[i] = (fact[i - 1] * i) % (int)(1e9 + 7);
fact_inv[i] = power(fact[i], (int)(1e9 + 7) - 2);
}
}
void init() {
eratos_sieve();
init_fact();
}
long long C(int n, int k) {
if (k > n) return 0;
long long res = fact[n];
res = (res * fact_inv[k]) % (int)(1e9 + 7);
res = (res * fact_inv[n - k]) % (int)(1e9 + 7);
return res;
}
int getbit(int x, int y) { return (x >> y) & 1; }
long long incl_excl() {
long long res = 0;
int m = prime[n].size();
v = (int *)calloc(m + 1, sizeof(int));
for (int i = 1; i <= m; ++i) v[i] = prime[n][i - 1];
int last = (1 << m) - 1;
for (int state = 1; state <= last; ++state) {
int bits = 0;
int tmp = 1;
for (int i = 1; i <= m; ++i)
if (getbit(state, i - 1)) {
++bits;
tmp *= v[i];
}
tmp = C(n / tmp - 1, f - 1);
if (bits % 2)
res += tmp;
else
res -= tmp;
if (res > (int)(1e9 + 7)) res -= (int)(1e9 + 7);
if (res < 0) res += (int)(1e9 + 7);
}
free(v);
return res;
}
long long sol() {
long long res =
(C(n - 1, f - 1) - incl_excl() + (int)(1e9 + 7)) % (int)(1e9 + 7);
return res;
}
void inp() {
init();
int q;
cin >> q;
while (q--) {
cin >> n >> f;
assert((n <= (int)(1e5 + 5) && f <= n));
cout << sol() << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inp();
return 0;
}
```
|
#include <bits/stdc++.h>
long long pow(long long b, int exp) {
if (exp == 0)
return 1;
else if (exp % 2)
return b * pow(b, exp - 1) % 1000000007;
else {
long long half = pow(b, exp / 2);
return half * half % 1000000007;
}
}
long long int fact[100010];
long long int inv[100010];
long long bin(int n, int k) {
if (n < 0 || n < k || k < 0) return 0;
return fact[n] * inv[k] % 1000000007 * inv[n - k] % 1000000007;
}
int main() {
fact[0] = 1;
for (int i = 1; i < 100010; ++i) fact[i] = fact[i - 1] * i % 1000000007;
for (int i = 0; i < 100010; ++i) inv[i] = pow(fact[i], 1000000007 - 2);
int q;
scanf("%d", &q);
while (q--) {
int f, n;
scanf("%d%d", &n, &f);
int div[6];
int amo = 0;
int nn = n;
for (int i = 2; i * i <= nn; ++i) {
if (nn % i == 0) {
while (nn % i == 0) nn /= i;
div[amo] = i;
amo++;
}
}
if (nn > 1) {
div[amo] = nn;
amo++;
}
long long res = 0;
for (int mask = 0; mask < (1 << amo); ++mask) {
int divi = 1;
int deg = 0;
for (int k = 0; k < amo; ++k)
if (mask & (1 << k)) {
deg++;
divi *= div[k];
}
if (deg % 2 == 0) {
res += bin(n / divi - 1, f - 1);
} else {
res += 1000000007 - bin(n / divi - 1, f - 1);
}
res %= 1000000007;
}
printf("%I64d\n", res);
}
}
|
### Prompt
Develop a solution in cpp to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
long long pow(long long b, int exp) {
if (exp == 0)
return 1;
else if (exp % 2)
return b * pow(b, exp - 1) % 1000000007;
else {
long long half = pow(b, exp / 2);
return half * half % 1000000007;
}
}
long long int fact[100010];
long long int inv[100010];
long long bin(int n, int k) {
if (n < 0 || n < k || k < 0) return 0;
return fact[n] * inv[k] % 1000000007 * inv[n - k] % 1000000007;
}
int main() {
fact[0] = 1;
for (int i = 1; i < 100010; ++i) fact[i] = fact[i - 1] * i % 1000000007;
for (int i = 0; i < 100010; ++i) inv[i] = pow(fact[i], 1000000007 - 2);
int q;
scanf("%d", &q);
while (q--) {
int f, n;
scanf("%d%d", &n, &f);
int div[6];
int amo = 0;
int nn = n;
for (int i = 2; i * i <= nn; ++i) {
if (nn % i == 0) {
while (nn % i == 0) nn /= i;
div[amo] = i;
amo++;
}
}
if (nn > 1) {
div[amo] = nn;
amo++;
}
long long res = 0;
for (int mask = 0; mask < (1 << amo); ++mask) {
int divi = 1;
int deg = 0;
for (int k = 0; k < amo; ++k)
if (mask & (1 << k)) {
deg++;
divi *= div[k];
}
if (deg % 2 == 0) {
res += bin(n / divi - 1, f - 1);
} else {
res += 1000000007 - bin(n / divi - 1, f - 1);
}
res %= 1000000007;
}
printf("%I64d\n", res);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int ALL = 1e5;
int fac[MAXN], ifac[MAXN];
int is[MAXN], mu[MAXN], p[MAXN], t;
inline int read() {
int x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
inline int qpow(int x, int y = MOD - 2) {
long long r = 1, k = x;
while (y) {
if (y & 1) r *= k, r %= MOD;
k *= k, k %= MOD, y >>= 1;
}
return r;
}
inline void prepare() {
fac[0] = ifac[0] = 1;
for (int i = 1; i <= ALL; i++) fac[i] = 1ll * fac[i - 1] * i % MOD;
ifac[ALL] = qpow(fac[ALL]);
for (int i = ALL - 1; ~i; i--) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % MOD;
}
inline int C(int x, int y) {
if (x < y || x < 0 || y < 0) return 0;
return 1ll * fac[x] * ifac[y] % MOD * ifac[x - y] % MOD;
}
inline void init() {
is[1] = mu[1] = 1;
for (int i = 2; i <= ALL; i++) {
if (!is[i]) p[++t] = i, mu[i] = -1;
for (int j = 1; j <= t && i * p[j] <= ALL; j++) {
is[i * p[j]] = 1, mu[i * p[j]] = -mu[i];
if (i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
}
}
}
}
int main() {
int t = read();
prepare(), init();
while (t--) {
long long ans = 0;
int n = read(), m = read();
for (int i = 1; i * i <= n; i++) {
if (n % i) continue;
ans += mu[i] * C(n / i - 1, m - 1);
if (i * i != n) ans += mu[n / i] * C(i - 1, m - 1);
}
printf("%lld\n", ((ans % MOD) + MOD) % MOD);
}
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int ALL = 1e5;
int fac[MAXN], ifac[MAXN];
int is[MAXN], mu[MAXN], p[MAXN], t;
inline int read() {
int x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
inline int qpow(int x, int y = MOD - 2) {
long long r = 1, k = x;
while (y) {
if (y & 1) r *= k, r %= MOD;
k *= k, k %= MOD, y >>= 1;
}
return r;
}
inline void prepare() {
fac[0] = ifac[0] = 1;
for (int i = 1; i <= ALL; i++) fac[i] = 1ll * fac[i - 1] * i % MOD;
ifac[ALL] = qpow(fac[ALL]);
for (int i = ALL - 1; ~i; i--) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % MOD;
}
inline int C(int x, int y) {
if (x < y || x < 0 || y < 0) return 0;
return 1ll * fac[x] * ifac[y] % MOD * ifac[x - y] % MOD;
}
inline void init() {
is[1] = mu[1] = 1;
for (int i = 2; i <= ALL; i++) {
if (!is[i]) p[++t] = i, mu[i] = -1;
for (int j = 1; j <= t && i * p[j] <= ALL; j++) {
is[i * p[j]] = 1, mu[i * p[j]] = -mu[i];
if (i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
}
}
}
}
int main() {
int t = read();
prepare(), init();
while (t--) {
long long ans = 0;
int n = read(), m = read();
for (int i = 1; i * i <= n; i++) {
if (n % i) continue;
ans += mu[i] * C(n / i - 1, m - 1);
if (i * i != n) ans += mu[n / i] * C(i - 1, m - 1);
}
printf("%lld\n", ((ans % MOD) + MOD) % MOD);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int x;
scanf("%d", &x);
return x;
}
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const long double eps = 1e-12;
map<pair<int, int>, int> mp;
int q, fact[N], inv[N];
vector<int> v[N];
int power(int n, int f) {
if (f == 0) return 1;
if (f == 1) return n;
long long x = power(n, f >> 1);
x %= MOD;
x = x * x;
x %= MOD;
if (f & 1) {
long long s = x * n;
s %= MOD;
return s;
}
return (1LL * x) % MOD;
}
int choose(int n, int f) {
long long iv = (1LL * inv[f] * inv[n - f]) % MOD;
return (1LL * fact[n] * iv) % MOD;
}
int solve(int n, int f) {
if (mp.find({n, f}) != mp.end()) return mp[{n, f}];
if (n < f) return 0;
if (n == f) return 1;
int res = choose(n - 1, f - 1);
for (int i = 0; i < v[n].size(); i++) {
res -= solve(v[n][i], f);
if (res < 0) res += MOD;
}
mp[{n, f}] = res;
return res;
}
int main() {
for (int i = 2; i < N; i++)
for (int j = i + i; j < N; j += i) v[j].push_back(i);
fact[0] = 1;
for (int i = 1; i < N; i++)
fact[i] = (1LL * fact[i - 1] * i) % MOD, inv[i] = power(fact[i], MOD - 2);
q = in();
while (q--) {
int n = in(), f = in();
printf("%d\n", solve(n, f));
}
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int x;
scanf("%d", &x);
return x;
}
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const long double eps = 1e-12;
map<pair<int, int>, int> mp;
int q, fact[N], inv[N];
vector<int> v[N];
int power(int n, int f) {
if (f == 0) return 1;
if (f == 1) return n;
long long x = power(n, f >> 1);
x %= MOD;
x = x * x;
x %= MOD;
if (f & 1) {
long long s = x * n;
s %= MOD;
return s;
}
return (1LL * x) % MOD;
}
int choose(int n, int f) {
long long iv = (1LL * inv[f] * inv[n - f]) % MOD;
return (1LL * fact[n] * iv) % MOD;
}
int solve(int n, int f) {
if (mp.find({n, f}) != mp.end()) return mp[{n, f}];
if (n < f) return 0;
if (n == f) return 1;
int res = choose(n - 1, f - 1);
for (int i = 0; i < v[n].size(); i++) {
res -= solve(v[n][i], f);
if (res < 0) res += MOD;
}
mp[{n, f}] = res;
return res;
}
int main() {
for (int i = 2; i < N; i++)
for (int j = i + i; j < N; j += i) v[j].push_back(i);
fact[0] = 1;
for (int i = 1; i < N; i++)
fact[i] = (1LL * fact[i - 1] * i) % MOD, inv[i] = power(fact[i], MOD - 2);
q = in();
while (q--) {
int n = in(), f = in();
printf("%d\n", solve(n, f));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
template <class T>
typename T::value_type arr_sum(const T &v, int n) {
typename T::value_type sum = 0;
for (int i = (0); i < (n); ++i) sum += v[i];
return sum;
}
struct Sync_stdio {
Sync_stdio() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
}
} _sync_stdio;
vector<int> divisors(int n) {
int s = sqrt(n);
vector<int> div;
for (int i = (1); i < (s + 1); ++i) {
if (n % i == 0) {
div.push_back(i);
}
}
vector<int> div2;
for (auto i : div) {
div2.push_back(n / i);
}
if (div2.back() == div.back()) {
div2.pop_back();
}
reverse(div2.begin(), div2.end());
div.insert(div.end(), div2.begin(), div2.end());
return div;
}
int gcd_ext(int a, int b, int &x, int &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
int x1, y1;
int d = gcd_ext(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
long long inv(long long t, long long mod) {
if (t == 0) {
return 0;
}
int x;
int y;
gcd_ext(t, mod, x, y);
return (x % mod + mod) % mod;
}
vector<long long> fact(100002);
long long comb(int n, int k) {
if (n < k) {
return 0;
}
long long res = fact[n];
res *= inv(fact[k], MOD);
res %= MOD;
res *= inv(fact[n - k], MOD);
res %= MOD;
return res;
}
vector<vector<int>> divs(100002);
vector<int> m(100002);
int main() {
int q;
cin >> q;
fact[0] = 1;
for (int i = (1); i < (fact.size()); ++i) {
fact[i] = (fact[i - 1] * i) % MOD;
}
m[1] = 1;
for (int i = (2); i < (m.size()); ++i) {
int c = 0;
int t = i;
int sq = sqrt(t);
for (int j = (2); j < (sq + 1); ++j) {
if (t % (j * j) == 0) {
c = -10;
break;
}
while (t % j == 0) {
t /= j;
++c;
}
}
if (t != 1) {
++c;
}
if (c < 0) {
m[i] = 0;
continue;
}
if (c % 2) {
m[i] = -1;
} else {
m[i] = 1;
}
}
for (int i = (1); i < (100002); ++i) {
divs[i] = divisors(i);
}
for (int l = (0); l < (q); ++l) {
int n, f;
cin >> n >> f;
long long sum = 0;
for (auto i : divs[n]) {
sum += m[i] * comb(n / i - 1, f - 1);
sum += MOD;
sum %= MOD;
}
cout << sum << "\n";
}
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
template <class T>
typename T::value_type arr_sum(const T &v, int n) {
typename T::value_type sum = 0;
for (int i = (0); i < (n); ++i) sum += v[i];
return sum;
}
struct Sync_stdio {
Sync_stdio() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
}
} _sync_stdio;
vector<int> divisors(int n) {
int s = sqrt(n);
vector<int> div;
for (int i = (1); i < (s + 1); ++i) {
if (n % i == 0) {
div.push_back(i);
}
}
vector<int> div2;
for (auto i : div) {
div2.push_back(n / i);
}
if (div2.back() == div.back()) {
div2.pop_back();
}
reverse(div2.begin(), div2.end());
div.insert(div.end(), div2.begin(), div2.end());
return div;
}
int gcd_ext(int a, int b, int &x, int &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
int x1, y1;
int d = gcd_ext(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
long long inv(long long t, long long mod) {
if (t == 0) {
return 0;
}
int x;
int y;
gcd_ext(t, mod, x, y);
return (x % mod + mod) % mod;
}
vector<long long> fact(100002);
long long comb(int n, int k) {
if (n < k) {
return 0;
}
long long res = fact[n];
res *= inv(fact[k], MOD);
res %= MOD;
res *= inv(fact[n - k], MOD);
res %= MOD;
return res;
}
vector<vector<int>> divs(100002);
vector<int> m(100002);
int main() {
int q;
cin >> q;
fact[0] = 1;
for (int i = (1); i < (fact.size()); ++i) {
fact[i] = (fact[i - 1] * i) % MOD;
}
m[1] = 1;
for (int i = (2); i < (m.size()); ++i) {
int c = 0;
int t = i;
int sq = sqrt(t);
for (int j = (2); j < (sq + 1); ++j) {
if (t % (j * j) == 0) {
c = -10;
break;
}
while (t % j == 0) {
t /= j;
++c;
}
}
if (t != 1) {
++c;
}
if (c < 0) {
m[i] = 0;
continue;
}
if (c % 2) {
m[i] = -1;
} else {
m[i] = 1;
}
}
for (int i = (1); i < (100002); ++i) {
divs[i] = divisors(i);
}
for (int l = (0); l < (q); ++l) {
int n, f;
cin >> n >> f;
long long sum = 0;
for (auto i : divs[n]) {
sum += m[i] * comb(n / i - 1, f - 1);
sum += MOD;
sum %= MOD;
}
cout << sum << "\n";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int maxn = 120000;
constexpr LL mod = 1000000007;
LL power(LL a, LL r) {
LL res = 1;
for (; r; r >>= 1, a = a * a % mod)
if (r & 1) res = res * a % mod;
return res;
}
LL f[maxn], uf[maxn];
LL c(int m, int n) {
if (m < n) return 0;
return f[m] * uf[n] % mod * uf[m - n] % mod;
}
int mu[maxn];
vector<int> d[maxn];
int main() {
for (int i = 0; i < maxn; i += 1)
uf[i] = power(f[i] = i ? f[i - 1] * i % mod : 1, mod - 2);
mu[1] = 1;
for (int i = 1; i < maxn; i += 1) {
for (int j = 1; i * j < maxn; j += 1) d[i * j].push_back(i);
if (i > 1) {
if (i / d[i][1] % d[i][1])
mu[i] = mod - mu[i / d[i][1]];
else
mu[i] = 0;
}
}
int q;
cin >> q;
while (q--) {
int f, n;
cin >> n >> f;
LL ans = 0;
for (int x : d[n]) ans = (ans + mu[x] * c(n / x - 1, f - 1)) % mod;
cout << ans << endl;
}
}
|
### Prompt
Please create a solution in CPP to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int maxn = 120000;
constexpr LL mod = 1000000007;
LL power(LL a, LL r) {
LL res = 1;
for (; r; r >>= 1, a = a * a % mod)
if (r & 1) res = res * a % mod;
return res;
}
LL f[maxn], uf[maxn];
LL c(int m, int n) {
if (m < n) return 0;
return f[m] * uf[n] % mod * uf[m - n] % mod;
}
int mu[maxn];
vector<int> d[maxn];
int main() {
for (int i = 0; i < maxn; i += 1)
uf[i] = power(f[i] = i ? f[i - 1] * i % mod : 1, mod - 2);
mu[1] = 1;
for (int i = 1; i < maxn; i += 1) {
for (int j = 1; i * j < maxn; j += 1) d[i * j].push_back(i);
if (i > 1) {
if (i / d[i][1] % d[i][1])
mu[i] = mod - mu[i / d[i][1]];
else
mu[i] = 0;
}
}
int q;
cin >> q;
while (q--) {
int f, n;
cin >> n >> f;
LL ans = 0;
for (int x : d[n]) ans = (ans + mu[x] * c(n / x - 1, f - 1)) % mod;
cout << ans << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int nn = 100001;
int main(int argc, char* argv[]) {
int q;
vector<long long> inv(nn);
inv[1] = 1;
for (int i = 2; i < nn; ++i)
inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
vector<long long> f(nn), ivf(nn);
f[0] = ivf[0] = 1;
for (int i = (1); i <= (nn - 1); ++i) {
f[i] = (f[i - 1] * i) % mod;
ivf[i] = (ivf[i - 1] * inv[i]) % mod;
}
cin >> q;
vector<long long> ret;
for (int x = 0; x < (q); x++) {
long long ans = 0;
int n, ff;
cin >> n >> ff;
if (n == 1) {
if (ff == 1) ans = 1;
} else {
vector<int> d;
int m = n;
for (int p = 2; p * p <= m; ++p) {
if (m % p == 0) {
d.push_back(p);
while (m % p == 0) m /= p;
}
}
if (m > 1) d.push_back(m);
int sz = d.size();
for (int st = 0; st < ((1 << (sz))); st++) {
int ct = 0;
int y = n;
for (int i = 0; i < (sz); i++)
if ((1 << (i)) & st) {
++ct;
y /= d[i];
}
long long x = 0;
if (y >= ff) x = (((f[y - 1] * ivf[ff - 1]) % mod) * ivf[y - ff]) % mod;
if (ct % 2)
ans += mod - x;
else
ans += x;
}
ans %= mod;
}
ret.push_back(ans);
}
for (int i = 0; i < (q); i++) cout << ret[i] << "\n";
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int nn = 100001;
int main(int argc, char* argv[]) {
int q;
vector<long long> inv(nn);
inv[1] = 1;
for (int i = 2; i < nn; ++i)
inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
vector<long long> f(nn), ivf(nn);
f[0] = ivf[0] = 1;
for (int i = (1); i <= (nn - 1); ++i) {
f[i] = (f[i - 1] * i) % mod;
ivf[i] = (ivf[i - 1] * inv[i]) % mod;
}
cin >> q;
vector<long long> ret;
for (int x = 0; x < (q); x++) {
long long ans = 0;
int n, ff;
cin >> n >> ff;
if (n == 1) {
if (ff == 1) ans = 1;
} else {
vector<int> d;
int m = n;
for (int p = 2; p * p <= m; ++p) {
if (m % p == 0) {
d.push_back(p);
while (m % p == 0) m /= p;
}
}
if (m > 1) d.push_back(m);
int sz = d.size();
for (int st = 0; st < ((1 << (sz))); st++) {
int ct = 0;
int y = n;
for (int i = 0; i < (sz); i++)
if ((1 << (i)) & st) {
++ct;
y /= d[i];
}
long long x = 0;
if (y >= ff) x = (((f[y - 1] * ivf[ff - 1]) % mod) * ivf[y - ff]) % mod;
if (ct % 2)
ans += mod - x;
else
ans += x;
}
ans %= mod;
}
ret.push_back(ans);
}
for (int i = 0; i < (q); i++) cout << ret[i] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 50;
const long long p = 1E9 + 7;
int T, n, s, pcnt, vis[N], prim[N >> 1], miu[N];
long long ans, fac[N], inv[N];
long long Pow(long long a, long long b = p - 2) {
long long res = 1;
for (; b; b >>= 1, a = a * a % p)
if (b & 1) res = res * a % p;
return res;
}
inline long long C(int n, int m) {
if (n < m || n < 0 || m < 0) return 0;
return fac[n] * inv[m] % p * inv[n - m] % p;
}
inline void init(int n = 1E5) {
fac[0] = inv[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;
inv[n] = Pow(fac[n]);
for (int i = n - 1; i; --i) inv[i] = inv[i + 1] * (i + 1) % p;
miu[1] = 1;
for (int i = 2; i <= n; i++) {
if (!vis[i]) miu[i] = -1, prim[++pcnt] = i;
for (int j = 1; j <= pcnt && prim[j] * i <= n; j++) {
vis[i * prim[j]] = 1;
if (i % prim[j])
miu[i * prim[j]] = -miu[i];
else
break;
}
}
}
inline void calc(int u) {
if (!miu[u]) return;
ans = (ans + 1LL * miu[u] * C(s / u - 1, n - 1)) % p;
}
int main() {
init();
for (cin >> T; T--;) {
scanf("%d%d", &s, &n);
int k = sqrt(s);
ans = 0;
for (int i = 1; i <= k; i++)
if (s % i == 0) {
calc(i);
if (i != s / i) calc(s / i);
}
printf("%lld\n", (ans + p) % p);
}
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 50;
const long long p = 1E9 + 7;
int T, n, s, pcnt, vis[N], prim[N >> 1], miu[N];
long long ans, fac[N], inv[N];
long long Pow(long long a, long long b = p - 2) {
long long res = 1;
for (; b; b >>= 1, a = a * a % p)
if (b & 1) res = res * a % p;
return res;
}
inline long long C(int n, int m) {
if (n < m || n < 0 || m < 0) return 0;
return fac[n] * inv[m] % p * inv[n - m] % p;
}
inline void init(int n = 1E5) {
fac[0] = inv[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;
inv[n] = Pow(fac[n]);
for (int i = n - 1; i; --i) inv[i] = inv[i + 1] * (i + 1) % p;
miu[1] = 1;
for (int i = 2; i <= n; i++) {
if (!vis[i]) miu[i] = -1, prim[++pcnt] = i;
for (int j = 1; j <= pcnt && prim[j] * i <= n; j++) {
vis[i * prim[j]] = 1;
if (i % prim[j])
miu[i * prim[j]] = -miu[i];
else
break;
}
}
}
inline void calc(int u) {
if (!miu[u]) return;
ans = (ans + 1LL * miu[u] * C(s / u - 1, n - 1)) % p;
}
int main() {
init();
for (cin >> T; T--;) {
scanf("%d%d", &s, &n);
int k = sqrt(s);
ans = 0;
for (int i = 1; i <= k; i++)
if (s % i == 0) {
calc(i);
if (i != s / i) calc(s / i);
}
printf("%lld\n", (ans + p) % p);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
vector<long long int> fact(200005);
vector<long long int> invfact(200005);
bool sieve[200005];
int SPF[200005];
long long int binpow(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
pair<long long int, long long int> decompose(long long int n) {
int ctr = 0;
vector<long long int> p;
while (n > 1) {
int q = SPF[n];
p.push_back(q);
while (n % q == 0) n /= q;
}
long long int prod = 1;
for (auto i : p) prod *= i;
return {prod, p.size()};
}
long long int nck(long long int a, long long int b) {
if (a < b) return 0;
return fact[a] * invfact[b] % 1000000007 * invfact[a - b] % 1000000007;
}
void solve() {
int n, k;
cin >> n >> k;
if (k == 1) {
if (n == 1)
cout << 1 << "\n";
else
cout << 0 << "\n";
return;
}
long long int ans = nck(n - 1, k - 1);
set<pair<long long int, long long int>> reducedpf;
for (long long int i = 2; i * i <= n; i++) {
if (n % i == 0) {
reducedpf.insert(decompose(i));
reducedpf.insert(decompose(n / i));
}
}
for (auto i : reducedpf) {
if (i.second % 2)
ans = (ans - nck(n / i.first - 1, k - 1)) % 1000000007;
else
ans = (ans + nck(n / i.first - 1, k - 1)) % 1000000007;
}
ans += 1000000007;
ans %= 1000000007;
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
fact[0] = 1;
invfact[0] = 1;
for (long long int i = 2; i <= 100005; i++) {
if (!sieve[i]) {
SPF[i] = i;
for (long long int j = i * i; j <= 100005; j += i) {
if (SPF[j] == 0) SPF[j] = i;
SPF[j] = min(SPF[j], SPF[i]);
sieve[j] = true;
}
}
}
for (long long int i = 1; i <= 100005; i++) {
fact[i] = i * fact[i - 1] % 1000000007;
invfact[i] = binpow(fact[i], 1000000007 - 2);
}
int t;
cin >> t;
while (t--) solve();
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
vector<long long int> fact(200005);
vector<long long int> invfact(200005);
bool sieve[200005];
int SPF[200005];
long long int binpow(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
pair<long long int, long long int> decompose(long long int n) {
int ctr = 0;
vector<long long int> p;
while (n > 1) {
int q = SPF[n];
p.push_back(q);
while (n % q == 0) n /= q;
}
long long int prod = 1;
for (auto i : p) prod *= i;
return {prod, p.size()};
}
long long int nck(long long int a, long long int b) {
if (a < b) return 0;
return fact[a] * invfact[b] % 1000000007 * invfact[a - b] % 1000000007;
}
void solve() {
int n, k;
cin >> n >> k;
if (k == 1) {
if (n == 1)
cout << 1 << "\n";
else
cout << 0 << "\n";
return;
}
long long int ans = nck(n - 1, k - 1);
set<pair<long long int, long long int>> reducedpf;
for (long long int i = 2; i * i <= n; i++) {
if (n % i == 0) {
reducedpf.insert(decompose(i));
reducedpf.insert(decompose(n / i));
}
}
for (auto i : reducedpf) {
if (i.second % 2)
ans = (ans - nck(n / i.first - 1, k - 1)) % 1000000007;
else
ans = (ans + nck(n / i.first - 1, k - 1)) % 1000000007;
}
ans += 1000000007;
ans %= 1000000007;
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
fact[0] = 1;
invfact[0] = 1;
for (long long int i = 2; i <= 100005; i++) {
if (!sieve[i]) {
SPF[i] = i;
for (long long int j = i * i; j <= 100005; j += i) {
if (SPF[j] == 0) SPF[j] = i;
SPF[j] = min(SPF[j], SPF[i]);
sieve[j] = true;
}
}
}
for (long long int i = 1; i <= 100005; i++) {
fact[i] = i * fact[i - 1] % 1000000007;
invfact[i] = binpow(fact[i], 1000000007 - 2);
}
int t;
cin >> t;
while (t--) solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long fac[100001];
bool prime[100001];
int largest_p[100001];
int mu[100001];
int divisor_count[100001];
int divisors[100001][65];
long long modpow(long long b, long long e) {
if (e == 0) {
return 1;
} else {
long long x = modpow(b, e / 2);
if (e % 2 == 0) {
return x * x % 1000000007;
} else {
return x * x % 1000000007 * b % 1000000007;
}
}
}
long long modinv(long long x) { return modpow(x, 1000000007 - 2); }
int main() {
fac[0] = 1;
for (int i = 1; i < 100001; i++) {
fac[i] = (fac[i - 1] * i) % 1000000007;
}
for (int i = 1; i < 100001; i++) {
prime[i] = true;
largest_p[i] = 1;
}
for (int i = 2; i < 100001; i++) {
if (prime[i]) {
largest_p[i] = i;
for (int j = i; j < 100001; j += i) {
largest_p[j] = max(largest_p[j], i);
prime[j] = false;
}
}
}
mu[1] = 1;
for (int i = 2; i < 100001; i++) {
if (largest_p[i] == largest_p[i / largest_p[i]]) {
mu[i] = 0;
} else {
mu[i] = -mu[i / largest_p[i]];
}
}
memset(divisor_count, 0, sizeof divisor_count);
for (int i = 1; i < 100001; i++) {
if (mu[i] != 0) {
for (int j = i; j < 100001; j += i) {
divisors[j][divisor_count[j]++] = i;
}
}
}
int q;
scanf("%d\n", &q);
while (q-- > 0) {
int n, f;
scanf("%d %d\n", &n, &f);
long long ans = 0;
for (int i = 0; i < divisor_count[n]; i++) {
int divisor = divisors[n][i];
int m = n / divisor - 1;
int k = f - 1;
if (m >= k) {
ans = (ans +
mu[divisor] * fac[m] % 1000000007 * modinv(fac[m - k]) %
1000000007 * modinv(fac[k]) % 1000000007 +
1000000007) %
1000000007;
}
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long fac[100001];
bool prime[100001];
int largest_p[100001];
int mu[100001];
int divisor_count[100001];
int divisors[100001][65];
long long modpow(long long b, long long e) {
if (e == 0) {
return 1;
} else {
long long x = modpow(b, e / 2);
if (e % 2 == 0) {
return x * x % 1000000007;
} else {
return x * x % 1000000007 * b % 1000000007;
}
}
}
long long modinv(long long x) { return modpow(x, 1000000007 - 2); }
int main() {
fac[0] = 1;
for (int i = 1; i < 100001; i++) {
fac[i] = (fac[i - 1] * i) % 1000000007;
}
for (int i = 1; i < 100001; i++) {
prime[i] = true;
largest_p[i] = 1;
}
for (int i = 2; i < 100001; i++) {
if (prime[i]) {
largest_p[i] = i;
for (int j = i; j < 100001; j += i) {
largest_p[j] = max(largest_p[j], i);
prime[j] = false;
}
}
}
mu[1] = 1;
for (int i = 2; i < 100001; i++) {
if (largest_p[i] == largest_p[i / largest_p[i]]) {
mu[i] = 0;
} else {
mu[i] = -mu[i / largest_p[i]];
}
}
memset(divisor_count, 0, sizeof divisor_count);
for (int i = 1; i < 100001; i++) {
if (mu[i] != 0) {
for (int j = i; j < 100001; j += i) {
divisors[j][divisor_count[j]++] = i;
}
}
}
int q;
scanf("%d\n", &q);
while (q-- > 0) {
int n, f;
scanf("%d %d\n", &n, &f);
long long ans = 0;
for (int i = 0; i < divisor_count[n]; i++) {
int divisor = divisors[n][i];
int m = n / divisor - 1;
int k = f - 1;
if (m >= k) {
ans = (ans +
mu[divisor] * fac[m] % 1000000007 * modinv(fac[m - k]) %
1000000007 * modinv(fac[k]) % 1000000007 +
1000000007) %
1000000007;
}
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool smin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool smax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
const long long N = 1e5 + 10, mod = (long long)1e9 + 7;
long long t, n, pick, f[N], rev[N];
vector<long long> vc[N];
long long pw(long long x, long long y) {
x %= mod;
long long ret = 1;
for (; y; y >>= 1, (x *= x) %= mod)
if (y & 1) (ret *= x) %= mod;
return ret;
}
long long C(long long n, long long r) {
if (r > n) return 0;
return ((f[n] * rev[r] % mod) * (rev[n - r]) % mod) % mod;
}
void solve() {
cin >> n >> pick;
long long m = (long long)vc[n].size();
long long res = 0;
for (long long mask = 1; mask < (1 << m); ++mask) {
long long cur = 1, cnt = 0;
for (long long j = 0; j < m; ++j)
if (mask >> j & 1) cur *= vc[n][j], ++cnt;
if (cnt & 1) {
res += (C((n / cur) - 1, pick - 1));
} else {
res -= (C((n / cur) - 1, pick - 1));
}
if (res < 0) res += mod;
if (res >= mod) res -= mod;
}
long long possible = C(n - 1, pick - 1);
res = possible - res;
if (res < 0) res += mod;
if (res >= mod) res -= mod;
cout << res << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (long long i = 2; i < N; ++i) {
if (vc[i].empty())
for (long long j = i; j < N; j += i) vc[j].push_back(i);
}
f[0] = rev[0] = 1;
for (long long i = 1; i < N; ++i)
(f[i] = f[i - 1] * i) %= mod, rev[i] = pw(f[i], mod - 2);
cin >> t;
while (t--) solve();
}
|
### Prompt
Please formulate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool smin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool smax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
const long long N = 1e5 + 10, mod = (long long)1e9 + 7;
long long t, n, pick, f[N], rev[N];
vector<long long> vc[N];
long long pw(long long x, long long y) {
x %= mod;
long long ret = 1;
for (; y; y >>= 1, (x *= x) %= mod)
if (y & 1) (ret *= x) %= mod;
return ret;
}
long long C(long long n, long long r) {
if (r > n) return 0;
return ((f[n] * rev[r] % mod) * (rev[n - r]) % mod) % mod;
}
void solve() {
cin >> n >> pick;
long long m = (long long)vc[n].size();
long long res = 0;
for (long long mask = 1; mask < (1 << m); ++mask) {
long long cur = 1, cnt = 0;
for (long long j = 0; j < m; ++j)
if (mask >> j & 1) cur *= vc[n][j], ++cnt;
if (cnt & 1) {
res += (C((n / cur) - 1, pick - 1));
} else {
res -= (C((n / cur) - 1, pick - 1));
}
if (res < 0) res += mod;
if (res >= mod) res -= mod;
}
long long possible = C(n - 1, pick - 1);
res = possible - res;
if (res < 0) res += mod;
if (res >= mod) res -= mod;
cout << res << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (long long i = 2; i < N; ++i) {
if (vc[i].empty())
for (long long j = i; j < N; j += i) vc[j].push_back(i);
}
f[0] = rev[0] = 1;
for (long long i = 1; i < N; ++i)
(f[i] = f[i - 1] * i) %= mod, rev[i] = pw(f[i], mod - 2);
cin >> t;
while (t--) solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
static inline int Rint() {
struct X {
int dig[256];
X() {
for (int i = '0'; i <= '9'; ++i) dig[i] = 1;
dig['-'] = 1;
}
};
static X fuck;
int s = 1, v = 0, c;
for (; !fuck.dig[c = getchar()];)
;
if (c == '-')
s = 0;
else if (fuck.dig[c])
v = c ^ 48;
for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48))
;
return s ? v : -v;
}
template <typename T>
static inline void cmax(T& a, const T& b) {
if (b > a) a = b;
}
template <typename T>
static inline void cmin(T& a, const T& b) {
if (b < a) a = b;
}
template <long long P>
struct moder {
static long long inv(long long x) {
long long result = 1;
int n = P - 2;
x %= P;
for (; n; n >>= 1, x = x * x % P)
if (n & 1) result = result * x % P;
return result;
}
static long long* fac;
static void init_fac() {
fac = new long long[1048576];
fac[0] = 1;
for (int i = 1; i < 1048576; ++i) fac[i] = fac[i - 1] * i % P;
}
static void destroy_fac() { delete[] fac; }
static long long s(int x) { return x & 1 ? -1 : 1; }
static long long comb(long long m, long long n) {
if (n > m || n < 0) return 0;
long long pp = 0;
long long dist = m - n;
for (long long x = P; x <= m; x *= P) pp += m / x - n / x - dist / x;
if (pp) return 0;
long long l = 1, r = 1;
for (long long x = m; x; x /= P) l = l * s(x / P) * fac[x % P] % P;
for (long long x = n; x; x /= P) r = r * s(x / P) * fac[x % P] % P;
for (long long x = dist; x; x /= P) r = r * s(x / P) * fac[x % P] % P;
l = (l + P) % P;
r = (r + P) % P;
long long t = (inv(r) * l % P + P) % P;
return t;
}
};
const long long p = 1000000007;
const long long mod = p;
template <long long P>
long long* moder<P>::fac;
moder<p> md;
const int maxp = 200005;
static int plist[maxp / 3], pmask[maxp + 1];
int pcnt;
void init_primes() {
for (int i = 1; i <= maxp; ++i) pmask[i] = i;
for (int i = 2; i <= maxp; ++i) {
if (pmask[i] == i) {
plist[pcnt++] = i;
}
for (int j = 0; j < pcnt; ++j) {
const long long t = (long long)plist[j] * i;
if (t > maxp) break;
pmask[t] = plist[j];
if (i % plist[j] == 0) {
break;
}
}
}
}
static long long pv[1024];
int pc[1024];
static long long factors[655360];
static int factor_cnt;
void get_factors(int limit, long long value) {
factors[factor_cnt++] = value;
for (int i = 0; i < limit; ++i) {
long long tvalue = value;
const int p = pv[i];
for (int j = 1; j <= pc[i]; ++j) {
tvalue *= p;
get_factors(i, tvalue);
}
}
}
int factorize(long long n) {
int top = 0;
if (n <= maxp) {
while (n != 1) {
int now = pmask[n];
int c = 0;
while (n % now == 0) n /= now, ++c;
if (c) pv[top] = now, pc[top++] = c;
}
} else {
for (int i = 0; i < pcnt; ++i) {
const long long p = plist[i];
const long long test = p * p;
if (p > n) break;
int c = 0;
while (n % p == 0) n /= p, ++c;
if (c) pv[top] = p, pc[top++] = c;
}
if (n != 1) pv[top] = n, pc[top++] = 1;
}
return top;
}
static inline long long add(long long a, long long b) {
a += b;
if (a >= mod) a -= mod;
return a;
}
static inline long long sub(long long a, long long b) {
a -= b;
if (a < 0) a += mod;
return a;
}
long long dp[655360];
vector<int> adj[1005];
struct Query {
int n, f;
int id, ans;
};
int cmp_n(const Query& a, const Query& b) { return a.n < b.n; }
int cmp_id(const Query& a, const Query& b) { return a.id < b.id; }
Query data[100005];
int main() {
md.init_fac();
init_primes();
int q = Rint();
for (int i = (0); i < (q); ++i)
data[i].n = Rint(), data[i].f = Rint(), data[i].id = i;
sort(data, data + q, cmp_n);
for (int x = 0; x < q; ++x) {
const int n = data[x].n, f = data[x].f;
if (n == 1) {
data[x].ans = f == 1 ? 1 : 0;
continue;
}
if (f > n) {
data[x].ans = 0;
continue;
}
if (f == 1) {
data[x].ans = 0;
continue;
}
if (f == n) {
data[x].ans = 1;
continue;
}
factor_cnt = 0;
get_factors(factorize(n), 1);
sort(factors, factors + factor_cnt);
for (int i = 0; i < factor_cnt; ++i) adj[i].clear();
int t = 0;
for (int i = 0; i < factor_cnt; ++i)
for (int j = i + 1; j < factor_cnt; ++j)
if (factors[j] % factors[i] == 0) adj[i].push_back(j), ++t;
int j = x;
map<int, int> mem;
while (j < q && data[j].n == data[x].n) {
const int f = data[j].f;
if (mem.count(f) > 0) {
data[j++].ans = mem[f];
continue;
}
for (int i = 0; i < factor_cnt; ++i) dp[i] = 0;
for (int i = 0; i < factor_cnt; ++i) {
const long long val = factors[i];
if (val < f) {
dp[i] = 0;
continue;
}
const long long t = md.comb(f + val - f - 1, val - f);
dp[i] = add(dp[i], t);
for (int j = 0; j < adj[i].size(); ++j)
dp[adj[i][j]] = sub(dp[adj[i][j]], dp[i]);
}
data[j].ans = dp[factor_cnt - 1];
mem[f] = dp[factor_cnt - 1];
++j;
}
x = j - 1;
}
sort(data, data + q, cmp_id);
for (int i = 0; i < q; ++i) printf("%d\n", data[i].ans);
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
static inline int Rint() {
struct X {
int dig[256];
X() {
for (int i = '0'; i <= '9'; ++i) dig[i] = 1;
dig['-'] = 1;
}
};
static X fuck;
int s = 1, v = 0, c;
for (; !fuck.dig[c = getchar()];)
;
if (c == '-')
s = 0;
else if (fuck.dig[c])
v = c ^ 48;
for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48))
;
return s ? v : -v;
}
template <typename T>
static inline void cmax(T& a, const T& b) {
if (b > a) a = b;
}
template <typename T>
static inline void cmin(T& a, const T& b) {
if (b < a) a = b;
}
template <long long P>
struct moder {
static long long inv(long long x) {
long long result = 1;
int n = P - 2;
x %= P;
for (; n; n >>= 1, x = x * x % P)
if (n & 1) result = result * x % P;
return result;
}
static long long* fac;
static void init_fac() {
fac = new long long[1048576];
fac[0] = 1;
for (int i = 1; i < 1048576; ++i) fac[i] = fac[i - 1] * i % P;
}
static void destroy_fac() { delete[] fac; }
static long long s(int x) { return x & 1 ? -1 : 1; }
static long long comb(long long m, long long n) {
if (n > m || n < 0) return 0;
long long pp = 0;
long long dist = m - n;
for (long long x = P; x <= m; x *= P) pp += m / x - n / x - dist / x;
if (pp) return 0;
long long l = 1, r = 1;
for (long long x = m; x; x /= P) l = l * s(x / P) * fac[x % P] % P;
for (long long x = n; x; x /= P) r = r * s(x / P) * fac[x % P] % P;
for (long long x = dist; x; x /= P) r = r * s(x / P) * fac[x % P] % P;
l = (l + P) % P;
r = (r + P) % P;
long long t = (inv(r) * l % P + P) % P;
return t;
}
};
const long long p = 1000000007;
const long long mod = p;
template <long long P>
long long* moder<P>::fac;
moder<p> md;
const int maxp = 200005;
static int plist[maxp / 3], pmask[maxp + 1];
int pcnt;
void init_primes() {
for (int i = 1; i <= maxp; ++i) pmask[i] = i;
for (int i = 2; i <= maxp; ++i) {
if (pmask[i] == i) {
plist[pcnt++] = i;
}
for (int j = 0; j < pcnt; ++j) {
const long long t = (long long)plist[j] * i;
if (t > maxp) break;
pmask[t] = plist[j];
if (i % plist[j] == 0) {
break;
}
}
}
}
static long long pv[1024];
int pc[1024];
static long long factors[655360];
static int factor_cnt;
void get_factors(int limit, long long value) {
factors[factor_cnt++] = value;
for (int i = 0; i < limit; ++i) {
long long tvalue = value;
const int p = pv[i];
for (int j = 1; j <= pc[i]; ++j) {
tvalue *= p;
get_factors(i, tvalue);
}
}
}
int factorize(long long n) {
int top = 0;
if (n <= maxp) {
while (n != 1) {
int now = pmask[n];
int c = 0;
while (n % now == 0) n /= now, ++c;
if (c) pv[top] = now, pc[top++] = c;
}
} else {
for (int i = 0; i < pcnt; ++i) {
const long long p = plist[i];
const long long test = p * p;
if (p > n) break;
int c = 0;
while (n % p == 0) n /= p, ++c;
if (c) pv[top] = p, pc[top++] = c;
}
if (n != 1) pv[top] = n, pc[top++] = 1;
}
return top;
}
static inline long long add(long long a, long long b) {
a += b;
if (a >= mod) a -= mod;
return a;
}
static inline long long sub(long long a, long long b) {
a -= b;
if (a < 0) a += mod;
return a;
}
long long dp[655360];
vector<int> adj[1005];
struct Query {
int n, f;
int id, ans;
};
int cmp_n(const Query& a, const Query& b) { return a.n < b.n; }
int cmp_id(const Query& a, const Query& b) { return a.id < b.id; }
Query data[100005];
int main() {
md.init_fac();
init_primes();
int q = Rint();
for (int i = (0); i < (q); ++i)
data[i].n = Rint(), data[i].f = Rint(), data[i].id = i;
sort(data, data + q, cmp_n);
for (int x = 0; x < q; ++x) {
const int n = data[x].n, f = data[x].f;
if (n == 1) {
data[x].ans = f == 1 ? 1 : 0;
continue;
}
if (f > n) {
data[x].ans = 0;
continue;
}
if (f == 1) {
data[x].ans = 0;
continue;
}
if (f == n) {
data[x].ans = 1;
continue;
}
factor_cnt = 0;
get_factors(factorize(n), 1);
sort(factors, factors + factor_cnt);
for (int i = 0; i < factor_cnt; ++i) adj[i].clear();
int t = 0;
for (int i = 0; i < factor_cnt; ++i)
for (int j = i + 1; j < factor_cnt; ++j)
if (factors[j] % factors[i] == 0) adj[i].push_back(j), ++t;
int j = x;
map<int, int> mem;
while (j < q && data[j].n == data[x].n) {
const int f = data[j].f;
if (mem.count(f) > 0) {
data[j++].ans = mem[f];
continue;
}
for (int i = 0; i < factor_cnt; ++i) dp[i] = 0;
for (int i = 0; i < factor_cnt; ++i) {
const long long val = factors[i];
if (val < f) {
dp[i] = 0;
continue;
}
const long long t = md.comb(f + val - f - 1, val - f);
dp[i] = add(dp[i], t);
for (int j = 0; j < adj[i].size(); ++j)
dp[adj[i][j]] = sub(dp[adj[i][j]], dp[i]);
}
data[j].ans = dp[factor_cnt - 1];
mem[f] = dp[factor_cnt - 1];
++j;
}
x = j - 1;
}
sort(data, data + q, cmp_id);
for (int i = 0; i < q; ++i) printf("%d\n", data[i].ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long fact[100010];
long long ifact[100010];
long long lpf[200000];
long long prim[100010];
vector<long long> D[100010];
long long mu[100010];
long long power(long long a, long long b) {
if (a == 0) return 0;
if (a == 1) return 1;
if (b == 0) return 1;
if (b == 1) return a;
long long xx = power(a, b / 2);
long long yy = (xx * xx) % mod;
if (b % 2 == 0)
return yy;
else
return (yy * a) % mod;
}
long long inverse(long long a) { return power(a, mod - 2LL); }
long long nCr(long long a, long long b) {
if (b > a) return 0;
long long x = fact[a];
x = (x * ifact[b]) % mod;
x = (x * ifact[a - b]) % mod;
return x;
}
void seiv() {
lpf[1] = 1;
for (long long i = 2; i <= 1000000; i++) {
if (lpf[i] == 0) {
for (long long j = i; j <= 1000000; j += i) {
if (lpf[j] == 0) lpf[j] = i;
}
}
}
}
long long totient(long long n) {
if (n == 1) return 1;
long long vh = 1;
while (n > 1) {
long long x = lpf[n];
long long cnt = 0;
while (n % x == 0) {
n /= x;
cnt++;
}
vh *= (pow(x, cnt) - pow(x, cnt - 1));
}
return vh;
}
long long solve(long long n, long long d) {
long long sum = 0;
for (long long i = 0; i < D[n].size(); i++) {
long long p = D[n][i];
sum += nCr(p - 1, d - 1) * mu[n / p];
sum = (sum + mod) % mod;
}
return sum;
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
fact[0] = 1;
ifact[0] = 1;
for (long long i = 1; i < 100010; i++) {
fact[i] = (fact[i - 1] * i) % mod;
ifact[i] = inverse(fact[i]);
}
for (long long i = 0; i < 100010; i++) {
mu[i] = 1;
}
for (long long i = 2; i < 100010; i++) {
long long p = i * i;
for (long long j = p; j < 100010; j += p) {
mu[j] = 0;
}
if (prim[i] == 0) {
for (long long j = 2 * i; j < 100010; j += i) {
prim[j] = 1;
}
}
if (prim[i] == 0) {
for (long long j = i; j < 100010; j += i) {
mu[j] *= -1;
}
}
}
for (long long i = 1; i < 100010; i++) {
for (long long j = i; j < 100010; j += i) {
D[j].push_back(i);
}
}
long long t;
cin >> t;
while (t > 0) {
t--;
long long n, d;
cin >> n >> d;
if (d == 1 && n > 1) {
cout << 0 << endl;
continue;
}
cout << solve(n, d) << "\n";
}
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long fact[100010];
long long ifact[100010];
long long lpf[200000];
long long prim[100010];
vector<long long> D[100010];
long long mu[100010];
long long power(long long a, long long b) {
if (a == 0) return 0;
if (a == 1) return 1;
if (b == 0) return 1;
if (b == 1) return a;
long long xx = power(a, b / 2);
long long yy = (xx * xx) % mod;
if (b % 2 == 0)
return yy;
else
return (yy * a) % mod;
}
long long inverse(long long a) { return power(a, mod - 2LL); }
long long nCr(long long a, long long b) {
if (b > a) return 0;
long long x = fact[a];
x = (x * ifact[b]) % mod;
x = (x * ifact[a - b]) % mod;
return x;
}
void seiv() {
lpf[1] = 1;
for (long long i = 2; i <= 1000000; i++) {
if (lpf[i] == 0) {
for (long long j = i; j <= 1000000; j += i) {
if (lpf[j] == 0) lpf[j] = i;
}
}
}
}
long long totient(long long n) {
if (n == 1) return 1;
long long vh = 1;
while (n > 1) {
long long x = lpf[n];
long long cnt = 0;
while (n % x == 0) {
n /= x;
cnt++;
}
vh *= (pow(x, cnt) - pow(x, cnt - 1));
}
return vh;
}
long long solve(long long n, long long d) {
long long sum = 0;
for (long long i = 0; i < D[n].size(); i++) {
long long p = D[n][i];
sum += nCr(p - 1, d - 1) * mu[n / p];
sum = (sum + mod) % mod;
}
return sum;
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
fact[0] = 1;
ifact[0] = 1;
for (long long i = 1; i < 100010; i++) {
fact[i] = (fact[i - 1] * i) % mod;
ifact[i] = inverse(fact[i]);
}
for (long long i = 0; i < 100010; i++) {
mu[i] = 1;
}
for (long long i = 2; i < 100010; i++) {
long long p = i * i;
for (long long j = p; j < 100010; j += p) {
mu[j] = 0;
}
if (prim[i] == 0) {
for (long long j = 2 * i; j < 100010; j += i) {
prim[j] = 1;
}
}
if (prim[i] == 0) {
for (long long j = i; j < 100010; j += i) {
mu[j] *= -1;
}
}
}
for (long long i = 1; i < 100010; i++) {
for (long long j = i; j < 100010; j += i) {
D[j].push_back(i);
}
}
long long t;
cin >> t;
while (t > 0) {
t--;
long long n, d;
cin >> n >> d;
if (d == 1 && n > 1) {
cout << 0 << endl;
continue;
}
cout << solve(n, d) << "\n";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long ff[100100], inv_ff[100100];
int q, n, f, prime[100100], sqr[100100], mu[100100], ans;
vector<int> divi[100100];
long long inv_mod(long long x) {
long long ret = 1LL, y = MOD - 2;
while (y) {
if (y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD, y >>= 1;
}
return ret;
}
long long C(int x, int y) {
if (y > x) return 0;
return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD;
}
void precompute() {
inv_ff[0] = 1LL;
ff[0] = 1LL;
for (int i = 1; i <= 100099; i++) {
ff[i] = (ff[i - 1] * i) % MOD;
inv_ff[i] = inv_mod(ff[i]);
prime[i] = 1;
}
for (int i = 1; i <= 100099; i++) {
for (int j = i; j <= 100099; j += i) {
divi[j].push_back(i);
}
}
prime[0] = 0, prime[1] = 0;
for (int i = 2; i <= 100099; i++) {
if (prime[i] == 1) {
for (int j = i + i; j <= 100099; j += i) prime[j] = 0;
}
}
for (int i = 2; i * i <= 100099; i++) {
for (int j = (i * i); j <= 100099; j += (i * i)) sqr[j] = 1;
}
for (int i = 1; i <= 100099; i++) {
if (sqr[i]) continue;
int cnt = 0;
for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++)
if (prime[*d]) cnt++;
mu[i] = (cnt % 2 == 1) ? -1 : 1;
}
return;
}
int main() {
precompute();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
if (n == 1 and f == 1) {
printf("1\n");
continue;
}
if (n > 1 and f == 1) {
printf("0\n");
continue;
}
ans = 0;
for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end();
d++) {
ans = (ans + mu[*d] * C((n / (*d)) - 1, f - 1)) % MOD;
if (ans < 0)
ans += MOD;
else if (ans >= MOD)
ans -= MOD;
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long ff[100100], inv_ff[100100];
int q, n, f, prime[100100], sqr[100100], mu[100100], ans;
vector<int> divi[100100];
long long inv_mod(long long x) {
long long ret = 1LL, y = MOD - 2;
while (y) {
if (y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD, y >>= 1;
}
return ret;
}
long long C(int x, int y) {
if (y > x) return 0;
return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD;
}
void precompute() {
inv_ff[0] = 1LL;
ff[0] = 1LL;
for (int i = 1; i <= 100099; i++) {
ff[i] = (ff[i - 1] * i) % MOD;
inv_ff[i] = inv_mod(ff[i]);
prime[i] = 1;
}
for (int i = 1; i <= 100099; i++) {
for (int j = i; j <= 100099; j += i) {
divi[j].push_back(i);
}
}
prime[0] = 0, prime[1] = 0;
for (int i = 2; i <= 100099; i++) {
if (prime[i] == 1) {
for (int j = i + i; j <= 100099; j += i) prime[j] = 0;
}
}
for (int i = 2; i * i <= 100099; i++) {
for (int j = (i * i); j <= 100099; j += (i * i)) sqr[j] = 1;
}
for (int i = 1; i <= 100099; i++) {
if (sqr[i]) continue;
int cnt = 0;
for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++)
if (prime[*d]) cnt++;
mu[i] = (cnt % 2 == 1) ? -1 : 1;
}
return;
}
int main() {
precompute();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
if (n == 1 and f == 1) {
printf("1\n");
continue;
}
if (n > 1 and f == 1) {
printf("0\n");
continue;
}
ans = 0;
for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end();
d++) {
ans = (ans + mu[*d] * C((n / (*d)) - 1, f - 1)) % MOD;
if (ans < 0)
ans += MOD;
else if (ans >= MOD)
ans -= MOD;
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 1e5 + 10, mod = 1000000007;
vector<int> v[maxn];
int n, s, q;
long long fac[maxn];
long long p[maxn];
long long dp[maxn];
long long C[maxn];
long long tav(long long i, long long j) {
if (j <= 1) return (1 * (j == 0) + i * (j == 1)) % mod;
long long second = tav(i, j / 2);
second = (second * second) % mod;
second = (second * (1 + (i - 1) * (j % 2))) % mod;
return second;
}
long long ent(int i, int j) {
if (i > j || i < 0) return 0;
return (fac[j] * C[i] % mod * C[j - i] % mod) % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int y = 2; y < maxn; y++)
for (int x = y; x < maxn; x += y) v[x].push_back(y);
fac[0] = 1;
C[0] = 1;
for (long long y = 1; y < maxn; y++)
fac[y] = (fac[y - 1] * y) % mod, C[y] = tav(fac[y], mod - 2), dp[y] = -1;
scanf("%d", &q);
while (q--) {
scanf("%d", &s);
scanf("%d", &n);
for (int y = v[s].size() - 1; y > -1; y--) {
int x = s / (v[s][y]);
dp[x] = ent(n - 1, x - 1);
for (int i = 0; i < v[x].size(); i++) dp[x] -= dp[x / v[x][i]];
dp[x] = ((dp[x] % mod) + mod) % mod;
}
dp[s] = ent(n - 1, s - 1);
for (int y = 0; y < v[s].size(); y++) {
int x = s / (v[s][y]);
dp[s] -= dp[x];
}
dp[s] = ((dp[s] % mod) + mod) % mod;
printf("%d \n", dp[s]);
for (int y = 0; y < v[s].size(); y++) dp[s / v[s][y]] = -1;
dp[s] = -1;
}
}
|
### Prompt
Please formulate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 1e5 + 10, mod = 1000000007;
vector<int> v[maxn];
int n, s, q;
long long fac[maxn];
long long p[maxn];
long long dp[maxn];
long long C[maxn];
long long tav(long long i, long long j) {
if (j <= 1) return (1 * (j == 0) + i * (j == 1)) % mod;
long long second = tav(i, j / 2);
second = (second * second) % mod;
second = (second * (1 + (i - 1) * (j % 2))) % mod;
return second;
}
long long ent(int i, int j) {
if (i > j || i < 0) return 0;
return (fac[j] * C[i] % mod * C[j - i] % mod) % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int y = 2; y < maxn; y++)
for (int x = y; x < maxn; x += y) v[x].push_back(y);
fac[0] = 1;
C[0] = 1;
for (long long y = 1; y < maxn; y++)
fac[y] = (fac[y - 1] * y) % mod, C[y] = tav(fac[y], mod - 2), dp[y] = -1;
scanf("%d", &q);
while (q--) {
scanf("%d", &s);
scanf("%d", &n);
for (int y = v[s].size() - 1; y > -1; y--) {
int x = s / (v[s][y]);
dp[x] = ent(n - 1, x - 1);
for (int i = 0; i < v[x].size(); i++) dp[x] -= dp[x / v[x][i]];
dp[x] = ((dp[x] % mod) + mod) % mod;
}
dp[s] = ent(n - 1, s - 1);
for (int y = 0; y < v[s].size(); y++) {
int x = s / (v[s][y]);
dp[s] -= dp[x];
}
dp[s] = ((dp[s] % mod) + mod) % mod;
printf("%d \n", dp[s]);
for (int y = 0; y < v[s].size(); y++) dp[s / v[s][y]] = -1;
dp[s] = -1;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-12;
const int inf = 2000000000;
int MOD = 1000000007;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
return n | (1LL << i);
;
}
inline long long int resetBit(long long int n, long long int i) {
return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
a %= MOD;
(a < 0) && (a += MOD);
}
inline long long int modMul(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a * b) % MOD;
}
inline long long int modAdd(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a + b) % MOD;
}
inline long long int modSub(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long int modPow(long long int b, long long int p) {
long long int r = 1LL;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1LL;
}
return r;
}
inline long long int modDiv(long long int a, long long int b) {
return modMul(a, modPow(b, MOD - 2));
}
bool comp(const pair<long long int, pair<long long int, long long int> > &p1,
const pair<long long int, pair<long long int, long long int> > &p2) {
return p1.first > p2.first;
}
long long int converter(string a) {
long long int i, mul = 1, r, t, ans = 0LL;
if (a.length() == 0) return 0;
for (i = a.length() - 1; i >= 0; i--) {
t = a[i] - '0';
r = t % 10;
ans += (mul * r);
mul = mul * 10;
}
return ans;
}
const int MAX = 100005;
long long int n, inv[MAX];
long long int fact[MAX];
int mu[MAX];
void mobius() {
int i, j;
mu[1] = fact[0] = fact[1] = 1;
for (i = 1; i < MAX; i++) {
for (j = i + i; j < MAX; j += i) {
mu[j] -= mu[i];
}
fact[i] = (fact[i - 1] * i) % MOD;
}
}
long long int stars_bars(long long int nn, long long int kk) {
long long int cur = (inv[kk - 1] * inv[nn - kk]) % MOD;
cur = (fact[nn - 1] * cur) % MOD;
return cur;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int i, f, qu, div1, div2;
mobius();
for (i = 0; i < MAX; ++i) {
inv[i] = modDiv(1LL, fact[i]);
}
cin >> qu;
while (qu--) {
cin >> n >> f;
int lim = sqrt(n);
long long int tot = 0LL;
for (i = 1; i <= (lim); ++i) {
if (n % i != 0) continue;
div1 = n / i;
div2 = i;
if (div1 >= f) {
tot += (mu[div2] * stars_bars(div1, f));
if (tot >= MOD)
tot -= MOD;
else if (tot < 0)
tot += MOD;
}
if (div1 != div2) {
if (div2 >= f) {
tot += (mu[div1] * stars_bars(div2, f));
if (tot >= MOD)
tot -= MOD;
else if (tot < 0)
tot += MOD;
}
}
}
cout << tot << '\n';
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-12;
const int inf = 2000000000;
int MOD = 1000000007;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
return n | (1LL << i);
;
}
inline long long int resetBit(long long int n, long long int i) {
return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
a %= MOD;
(a < 0) && (a += MOD);
}
inline long long int modMul(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a * b) % MOD;
}
inline long long int modAdd(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a + b) % MOD;
}
inline long long int modSub(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long int modPow(long long int b, long long int p) {
long long int r = 1LL;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1LL;
}
return r;
}
inline long long int modDiv(long long int a, long long int b) {
return modMul(a, modPow(b, MOD - 2));
}
bool comp(const pair<long long int, pair<long long int, long long int> > &p1,
const pair<long long int, pair<long long int, long long int> > &p2) {
return p1.first > p2.first;
}
long long int converter(string a) {
long long int i, mul = 1, r, t, ans = 0LL;
if (a.length() == 0) return 0;
for (i = a.length() - 1; i >= 0; i--) {
t = a[i] - '0';
r = t % 10;
ans += (mul * r);
mul = mul * 10;
}
return ans;
}
const int MAX = 100005;
long long int n, inv[MAX];
long long int fact[MAX];
int mu[MAX];
void mobius() {
int i, j;
mu[1] = fact[0] = fact[1] = 1;
for (i = 1; i < MAX; i++) {
for (j = i + i; j < MAX; j += i) {
mu[j] -= mu[i];
}
fact[i] = (fact[i - 1] * i) % MOD;
}
}
long long int stars_bars(long long int nn, long long int kk) {
long long int cur = (inv[kk - 1] * inv[nn - kk]) % MOD;
cur = (fact[nn - 1] * cur) % MOD;
return cur;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int i, f, qu, div1, div2;
mobius();
for (i = 0; i < MAX; ++i) {
inv[i] = modDiv(1LL, fact[i]);
}
cin >> qu;
while (qu--) {
cin >> n >> f;
int lim = sqrt(n);
long long int tot = 0LL;
for (i = 1; i <= (lim); ++i) {
if (n % i != 0) continue;
div1 = n / i;
div2 = i;
if (div1 >= f) {
tot += (mu[div2] * stars_bars(div1, f));
if (tot >= MOD)
tot -= MOD;
else if (tot < 0)
tot += MOD;
}
if (div1 != div2) {
if (div2 >= f) {
tot += (mu[div1] * stars_bars(div2, f));
if (tot >= MOD)
tot -= MOD;
else if (tot < 0)
tot += MOD;
}
}
}
cout << tot << '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MOD = 1e9 + 7;
int fact[MAX_N], ive_fact[MAX_N];
std::vector<int> ver;
int pow(int a, int x) {
int res = 1;
while (x) {
if (x & 1) res = 1ll * res * a % MOD;
x >>= 1;
a = 1ll * a * a % MOD;
}
return res;
}
int calc(int n, int k) {
if (n < k) return 0;
int res = fact[n];
res = 1ll * res * ive_fact[k] % MOD;
res = 1ll * res * ive_fact[n - k] % MOD;
return res;
}
void Cnk_init() {
fact[0] = 1;
for (int i = 1; i < MAX_N; i++) fact[i] = 1ll * fact[i - 1] * i % MOD;
for (int i = 0; i < MAX_N; i++) ive_fact[i] = pow(fact[i], MOD - 2);
}
void get_division(int n) {
ver.clear();
int final = sqrt((double)n + 0.5);
for (int i = 2; i <= final; i++)
if ((n % i) == 0) {
ver.push_back(i);
while ((n % i) == 0) n /= i;
}
if (n > 1) ver.push_back(n);
}
int main() {
Cnk_init();
int cas;
scanf("%d", &cas);
while (cas--) {
int n, f;
scanf("%d%d", &n, &f);
get_division(n);
int ans = 0;
for (int i = 0; i < (1 << ver.size()); i++) {
int tmp = 1, sign = 1;
for (int j = 0; j < ver.size(); j++)
if (i & (1 << j)) {
sign = -sign;
tmp *= ver[j];
}
ans = (1ll + ans + MOD - 1 + sign * calc(n / tmp - 1, f - 1)) % MOD;
}
printf("%d\n", ans);
}
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MOD = 1e9 + 7;
int fact[MAX_N], ive_fact[MAX_N];
std::vector<int> ver;
int pow(int a, int x) {
int res = 1;
while (x) {
if (x & 1) res = 1ll * res * a % MOD;
x >>= 1;
a = 1ll * a * a % MOD;
}
return res;
}
int calc(int n, int k) {
if (n < k) return 0;
int res = fact[n];
res = 1ll * res * ive_fact[k] % MOD;
res = 1ll * res * ive_fact[n - k] % MOD;
return res;
}
void Cnk_init() {
fact[0] = 1;
for (int i = 1; i < MAX_N; i++) fact[i] = 1ll * fact[i - 1] * i % MOD;
for (int i = 0; i < MAX_N; i++) ive_fact[i] = pow(fact[i], MOD - 2);
}
void get_division(int n) {
ver.clear();
int final = sqrt((double)n + 0.5);
for (int i = 2; i <= final; i++)
if ((n % i) == 0) {
ver.push_back(i);
while ((n % i) == 0) n /= i;
}
if (n > 1) ver.push_back(n);
}
int main() {
Cnk_init();
int cas;
scanf("%d", &cas);
while (cas--) {
int n, f;
scanf("%d%d", &n, &f);
get_division(n);
int ans = 0;
for (int i = 0; i < (1 << ver.size()); i++) {
int tmp = 1, sign = 1;
for (int j = 0; j < ver.size(); j++)
if (i & (1 << j)) {
sign = -sign;
tmp *= ver[j];
}
ans = (1ll + ans + MOD - 1 + sign * calc(n / tmp - 1, f - 1)) % MOD;
}
printf("%d\n", ans);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int t;
int n, f;
int fact[N];
int ifact[N];
vector<int> v[N];
int nu[N];
bool pr[N];
void pre() {
fact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = (1LL * fact[i - 1] * i) % mod;
}
ifact[N - 1] = 931791584;
for (int i = N - 2; i >= 0; --i) {
ifact[i] = (1LL * ifact[i + 1] * (i + 1LL)) % mod;
}
for (int i = 0; i < N; ++i) {
nu[i] = 1;
}
for (int i = 2; i < N; ++i) {
if (!pr[i]) {
if (1LL * i * i < N) {
for (int j = i * i; j < N; j += i) {
pr[j] = 1;
}
for (int j = i * i; j < N; j += i * i) {
nu[j] = 0;
}
}
for (int j = i; j < N; j += i) {
nu[j] *= -1;
}
}
}
for (int i = 1; i < N; ++i) {
if (nu[i]) {
for (int j = i; j < N; j += i) {
v[j].emplace_back(i);
}
}
}
}
inline int ncr(int n, int r) {
return (n >= r) ? ((1LL * fact[n] * ifact[r]) % mod) * ifact[n - r] % mod : 0;
}
int main() {
pre();
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &f);
long long ans = 0;
for (int g : v[n]) {
ans += ncr(n / g - 1, f - 1) * nu[g];
}
printf("%lld\n", ((ans % mod) + mod) % mod);
}
}
|
### Prompt
Please formulate a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int t;
int n, f;
int fact[N];
int ifact[N];
vector<int> v[N];
int nu[N];
bool pr[N];
void pre() {
fact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = (1LL * fact[i - 1] * i) % mod;
}
ifact[N - 1] = 931791584;
for (int i = N - 2; i >= 0; --i) {
ifact[i] = (1LL * ifact[i + 1] * (i + 1LL)) % mod;
}
for (int i = 0; i < N; ++i) {
nu[i] = 1;
}
for (int i = 2; i < N; ++i) {
if (!pr[i]) {
if (1LL * i * i < N) {
for (int j = i * i; j < N; j += i) {
pr[j] = 1;
}
for (int j = i * i; j < N; j += i * i) {
nu[j] = 0;
}
}
for (int j = i; j < N; j += i) {
nu[j] *= -1;
}
}
}
for (int i = 1; i < N; ++i) {
if (nu[i]) {
for (int j = i; j < N; j += i) {
v[j].emplace_back(i);
}
}
}
}
inline int ncr(int n, int r) {
return (n >= r) ? ((1LL * fact[n] * ifact[r]) % mod) * ifact[n - r] % mod : 0;
}
int main() {
pre();
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &f);
long long ans = 0;
for (int g : v[n]) {
ans += ncr(n / g - 1, f - 1) * nu[g];
}
printf("%lld\n", ((ans % mod) + mod) % mod);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 77, Mod = 1000000007;
int Fact[N], InvFact[N], q, Ca[N], T[N], Num;
vector<int> Div[N];
int Pow(int X, int Y) {
if (Y == 0) {
return (1);
}
int C = Pow(X, Y >> 1);
C = (C * 1ll * C) % Mod;
if (Y & 1) {
C = (C * 1ll * X) % Mod;
}
return C;
}
int C(int X, int Y) {
if (X < 0 || X > Y) {
return 0;
}
return ((Fact[Y] * 1ll * InvFact[X] % Mod) * 1ll * InvFact[Y - X]) % Mod;
}
int F(int n, int f) {
if (f > n) return 0;
if (f == n) return 1;
if (T[n] == Num) return Ca[n];
T[n] = Num;
int A = C(f - 1, n - 1);
for (int x : Div[n]) {
A = A - F(x, f);
if (A < 0) A += Mod;
}
Ca[n] = A;
return A;
}
void solve(int n, int f) {
Num++;
int A = F(n, f);
printf("%d\n", A);
}
int main() {
Fact[0] = InvFact[0] = 1;
for (int i = 1; i < N; i++) {
Fact[i] = (Fact[i - 1] * 1ll * i) % Mod;
}
InvFact[N - 1] = Pow(Fact[N - 1], Mod - 2);
for (int i = N - 2; i > 0; i--) {
InvFact[i] = (InvFact[i + 1] * 1ll * (i + 1)) % Mod;
}
for (int i = 1; i < N; i++) {
for (int j = i + i; j < N; j += i) {
Div[j].push_back(i);
}
}
scanf("%d", &q);
while (q--) {
int f, n;
scanf("%d %d", &n, &f);
solve(n, f);
}
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 77, Mod = 1000000007;
int Fact[N], InvFact[N], q, Ca[N], T[N], Num;
vector<int> Div[N];
int Pow(int X, int Y) {
if (Y == 0) {
return (1);
}
int C = Pow(X, Y >> 1);
C = (C * 1ll * C) % Mod;
if (Y & 1) {
C = (C * 1ll * X) % Mod;
}
return C;
}
int C(int X, int Y) {
if (X < 0 || X > Y) {
return 0;
}
return ((Fact[Y] * 1ll * InvFact[X] % Mod) * 1ll * InvFact[Y - X]) % Mod;
}
int F(int n, int f) {
if (f > n) return 0;
if (f == n) return 1;
if (T[n] == Num) return Ca[n];
T[n] = Num;
int A = C(f - 1, n - 1);
for (int x : Div[n]) {
A = A - F(x, f);
if (A < 0) A += Mod;
}
Ca[n] = A;
return A;
}
void solve(int n, int f) {
Num++;
int A = F(n, f);
printf("%d\n", A);
}
int main() {
Fact[0] = InvFact[0] = 1;
for (int i = 1; i < N; i++) {
Fact[i] = (Fact[i - 1] * 1ll * i) % Mod;
}
InvFact[N - 1] = Pow(Fact[N - 1], Mod - 2);
for (int i = N - 2; i > 0; i--) {
InvFact[i] = (InvFact[i + 1] * 1ll * (i + 1)) % Mod;
}
for (int i = 1; i < N; i++) {
for (int j = i + i; j < N; j += i) {
Div[j].push_back(i);
}
}
scanf("%d", &q);
while (q--) {
int f, n;
scanf("%d %d", &n, &f);
solve(n, f);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXN = 1e5 + 1;
vector<long long> fact, _fact;
vector<vector<int>> fac;
vector<int> sign;
long long binp(long long a, long long p) {
if (!p) return 1;
if (p & 1) return a * binp(a, p - 1) % MOD;
long long x = binp(a, (p >> 1));
return x * x % MOD;
}
void init() {
fact.resize(MAXN), _fact.resize(MAXN);
fact[0] = _fact[0] = 1;
for (int i = 1; i < MAXN; i++) fact[i] = (fact[i - 1] * i) % MOD;
_fact[MAXN - 1] = binp(fact[MAXN - 1], MOD - 2);
for (int i = MAXN - 2; i >= 1; i--) _fact[i] = _fact[i + 1] * (i + 1) % MOD;
fac.resize(MAXN);
for (int i = 1; i < MAXN; i++)
for (int j = i; j < MAXN; j += i) fac[j].push_back(i);
sign.resize(MAXN);
for (int i = 1; i < MAXN; i++)
sign[i] = i > 1 ? i / fac[i][1] % fac[i][1] ? -sign[i / fac[i][1]] : 0 : 1;
}
long long cnk(long long n, long long k) {
if (n < 0 || k < 0 || n < k) return 0;
long long ans = 1;
ans = ans * fact[n] % MOD;
ans = ans * _fact[k] % MOD;
ans = ans * _fact[n - k] % MOD;
return ans;
}
long long solve(int n, long long f) {
long long ans = 0;
for (int x : fac[n]) ans = (ans + sign[x] * cnk(n / x - 1, f - 1)) % MOD;
if (ans < 0) ans += MOD;
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
init();
int q;
cin >> q;
while (q--) {
long long n, f;
cin >> n >> f;
cout << solve(n, f) << '\n';
}
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXN = 1e5 + 1;
vector<long long> fact, _fact;
vector<vector<int>> fac;
vector<int> sign;
long long binp(long long a, long long p) {
if (!p) return 1;
if (p & 1) return a * binp(a, p - 1) % MOD;
long long x = binp(a, (p >> 1));
return x * x % MOD;
}
void init() {
fact.resize(MAXN), _fact.resize(MAXN);
fact[0] = _fact[0] = 1;
for (int i = 1; i < MAXN; i++) fact[i] = (fact[i - 1] * i) % MOD;
_fact[MAXN - 1] = binp(fact[MAXN - 1], MOD - 2);
for (int i = MAXN - 2; i >= 1; i--) _fact[i] = _fact[i + 1] * (i + 1) % MOD;
fac.resize(MAXN);
for (int i = 1; i < MAXN; i++)
for (int j = i; j < MAXN; j += i) fac[j].push_back(i);
sign.resize(MAXN);
for (int i = 1; i < MAXN; i++)
sign[i] = i > 1 ? i / fac[i][1] % fac[i][1] ? -sign[i / fac[i][1]] : 0 : 1;
}
long long cnk(long long n, long long k) {
if (n < 0 || k < 0 || n < k) return 0;
long long ans = 1;
ans = ans * fact[n] % MOD;
ans = ans * _fact[k] % MOD;
ans = ans * _fact[n - k] % MOD;
return ans;
}
long long solve(int n, long long f) {
long long ans = 0;
for (int x : fac[n]) ans = (ans + sign[x] * cnk(n / x - 1, f - 1)) % MOD;
if (ans < 0) ans += MOD;
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
init();
int q;
cin >> q;
while (q--) {
long long n, f;
cin >> n >> f;
cout << solve(n, f) << '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int t, n, f;
long long facto[100000 + 5], mobinverse[100000 + 5];
int mobius[100000 + 5];
vector<int> primes;
bool sieve[100000 + 5];
long long pangkat(long long a, long long b) {
if (b == 0) return 1;
if (b == 1) return a;
long long temp = pangkat(a, b / 2);
return (((temp * temp) % 1000000007) * pangkat(a, b % 2)) % 1000000007;
}
long long combi(long long a, long long b) {
return (((facto[a] * mobinverse[a - b]) % 1000000007) * mobinverse[b]) %
1000000007;
}
int main() {
facto[0] = 1;
for (int i = 1; i <= 100000; i++) {
mobius[i] = 1;
}
for (int i = 2; i <= 100000; i++) {
if (sieve[i]) continue;
primes.push_back(i);
for (int j = i; j <= 100000; j += i) {
sieve[j] = true;
mobius[j] *= -1;
}
sieve[i] = false;
long long temp = (long long)i * i;
for (long long j = temp; j <= 100000; j += temp) {
mobius[j] = 0;
}
}
mobinverse[0] = 1;
for (int i = 1; i <= 100000; i++) {
facto[i] = facto[i - 1] * i;
facto[i] %= 1000000007;
mobinverse[i] = pangkat(facto[i], 1000000007 - 2);
}
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &f);
if (f == 1) {
if (n == 1)
printf("1\n");
else
printf("0\n");
continue;
}
int ctr = 1;
long long res = 0;
if (!sieve[n]) {
printf("%lld\n", combi(n - 1, f - 1));
continue;
}
while (ctr * ctr <= n) {
if (n % ctr == 0 and n / ctr >= f) {
res += mobius[ctr] * combi(n / ctr - 1, f - 1);
if (res < 0)
res += 1000000007;
else
res %= 1000000007;
}
if (n % ctr == 0 and ctr >= f and ctr * ctr != n) {
res += mobius[n / ctr] * combi(ctr - 1, f - 1);
if (res < 0)
res += 1000000007;
else
res %= 1000000007;
}
ctr++;
}
printf("%lld\n", res);
}
}
|
### Prompt
Create a solution in CPP for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int t, n, f;
long long facto[100000 + 5], mobinverse[100000 + 5];
int mobius[100000 + 5];
vector<int> primes;
bool sieve[100000 + 5];
long long pangkat(long long a, long long b) {
if (b == 0) return 1;
if (b == 1) return a;
long long temp = pangkat(a, b / 2);
return (((temp * temp) % 1000000007) * pangkat(a, b % 2)) % 1000000007;
}
long long combi(long long a, long long b) {
return (((facto[a] * mobinverse[a - b]) % 1000000007) * mobinverse[b]) %
1000000007;
}
int main() {
facto[0] = 1;
for (int i = 1; i <= 100000; i++) {
mobius[i] = 1;
}
for (int i = 2; i <= 100000; i++) {
if (sieve[i]) continue;
primes.push_back(i);
for (int j = i; j <= 100000; j += i) {
sieve[j] = true;
mobius[j] *= -1;
}
sieve[i] = false;
long long temp = (long long)i * i;
for (long long j = temp; j <= 100000; j += temp) {
mobius[j] = 0;
}
}
mobinverse[0] = 1;
for (int i = 1; i <= 100000; i++) {
facto[i] = facto[i - 1] * i;
facto[i] %= 1000000007;
mobinverse[i] = pangkat(facto[i], 1000000007 - 2);
}
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &f);
if (f == 1) {
if (n == 1)
printf("1\n");
else
printf("0\n");
continue;
}
int ctr = 1;
long long res = 0;
if (!sieve[n]) {
printf("%lld\n", combi(n - 1, f - 1));
continue;
}
while (ctr * ctr <= n) {
if (n % ctr == 0 and n / ctr >= f) {
res += mobius[ctr] * combi(n / ctr - 1, f - 1);
if (res < 0)
res += 1000000007;
else
res %= 1000000007;
}
if (n % ctr == 0 and ctr >= f and ctr * ctr != n) {
res += mobius[n / ctr] * combi(ctr - 1, f - 1);
if (res < 0)
res += 1000000007;
else
res %= 1000000007;
}
ctr++;
}
printf("%lld\n", res);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int size = 100007;
const long long modulo = 1000000007;
const long long oo = 7e18;
const double EPS = 1e-13;
const double PI = acos(-1.0);
long long fact[size], rev_fact[size];
vector<long long> divisors[size];
long long used[size], dp[size];
int query = 0;
long long power(long long base, long long expo) {
long long res = 1;
while (expo > 0) {
if (expo & 1) res = (res * base) % modulo;
base = (base * base) % modulo;
expo >>= 1;
}
return res;
}
long long C(long long n, long long k) {
long long res = fact[n] * rev_fact[k];
res %= modulo;
res *= rev_fact[n - k];
res %= modulo;
return res;
}
long long f(long long n, long long k) {
if (n < k || (k == 1 && n > 1))
return 0;
else if (n == k)
return 1;
else if (used[n] == query)
return dp[n];
else {
used[n] = query;
long long res = C(n - 1, k - 1);
for (long long i = 0; i < divisors[n].size(); i++) {
res = (res + modulo - f(divisors[n][i], k)) % modulo;
}
dp[n] = res;
return res;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
for (int i = 2; i < size; i++)
for (int j = 2 * i; j < size; j += i) divisors[j].push_back(i);
fact[0] = 1;
for (int i = 1; i < size; i++) {
fact[i] = (fact[i - 1] * i) % modulo;
rev_fact[i] = power(fact[i], modulo - 2);
}
int q;
cin >> q;
while (q--) {
long long n, k;
cin >> n >> k;
++query;
cout << f(n, k) << endl;
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int size = 100007;
const long long modulo = 1000000007;
const long long oo = 7e18;
const double EPS = 1e-13;
const double PI = acos(-1.0);
long long fact[size], rev_fact[size];
vector<long long> divisors[size];
long long used[size], dp[size];
int query = 0;
long long power(long long base, long long expo) {
long long res = 1;
while (expo > 0) {
if (expo & 1) res = (res * base) % modulo;
base = (base * base) % modulo;
expo >>= 1;
}
return res;
}
long long C(long long n, long long k) {
long long res = fact[n] * rev_fact[k];
res %= modulo;
res *= rev_fact[n - k];
res %= modulo;
return res;
}
long long f(long long n, long long k) {
if (n < k || (k == 1 && n > 1))
return 0;
else if (n == k)
return 1;
else if (used[n] == query)
return dp[n];
else {
used[n] = query;
long long res = C(n - 1, k - 1);
for (long long i = 0; i < divisors[n].size(); i++) {
res = (res + modulo - f(divisors[n][i], k)) % modulo;
}
dp[n] = res;
return res;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
for (int i = 2; i < size; i++)
for (int j = 2 * i; j < size; j += i) divisors[j].push_back(i);
fact[0] = 1;
for (int i = 1; i < size; i++) {
fact[i] = (fact[i - 1] * i) % modulo;
rev_fact[i] = power(fact[i], modulo - 2);
}
int q;
cin >> q;
while (q--) {
long long n, k;
cin >> n >> k;
++query;
cout << f(n, k) << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
vector<int> v1[100005 + 1];
long long fact[100005];
long long inverse[100005];
int mark[100005];
long long powmod(long long a) {
long long b = 1000000007LL - 2;
long long c = 1000000007LL;
long long ret = 1LL;
while (b) {
if (b & 1) ret = (ret * a) % c;
a = (a * a) % c;
b >>= 1;
}
return (ret) % c;
}
long long mInverse(long long a) {
if (a == 0LL) return 1LL;
return powmod(a);
}
void precompute() {
fact[0] = 1;
inverse[0] = 1;
for (int i = 1; i <= 100000; ++i) {
fact[i] = fact[i - 1] * i;
fact[i] %= 1000000007LL;
inverse[i] = mInverse(fact[i]);
}
}
void sieve() {
int i, j;
memset(mark, 0, sizeof(mark));
for (i = 2; i <= 100005; i += 2) {
v1[i].push_back(2);
mark[i] = 1;
}
for (i = 3; i <= 100005; i += 3)
if (mark[i] == 0) v1[i].push_back(3);
for (i = 5; i * i <= 100005; i += 2) {
if (mark[i] == 0) {
mark[i] = 1;
v1[i].push_back(i);
for (j = i; j * i <= 100005; j += 2) {
if (mark[i * j] == 0) {
mark[i * j] == 1;
v1[i * j].push_back(i);
}
}
}
}
for (i = sqrt(100005); i <= 100005; ++i)
if (mark[i] == 0) v1[i].push_back(i);
}
long long nck(int n, int k) {
if (k > n)
return 0;
else if (k == 0 || k == n)
return 1;
long long res = fact[n];
res = res * inverse[k];
res %= 1000000007LL;
res = res * inverse[n - k];
res %= 1000000007LL;
return res;
}
int main() {
precompute();
sieve();
int q, n, k, i, j;
scanf("%d", &q);
while (q--) {
v.clear();
scanf("%d", &n);
scanf("%d", &k);
long long ans = nck(n - 1, k - 1);
int div = n;
int prev = -1;
while (div > 1) {
if (v1[div][0] != prev) v.push_back(v1[div][0]);
prev = v1[div][0];
div /= v1[div][0];
}
for (i = 1; i < (1 << v.size()); ++i) {
int setbit = __builtin_popcount(i);
int val = 1;
for (j = 0; j < v.size(); ++j) {
if (i & (1 << j)) val = val * v[j];
}
if (setbit & 1)
ans -= nck(n / val - 1, k - 1);
else
ans += nck(n / val - 1, k - 1);
ans = (ans % 1000000007LL + 2 * 1000000007LL) % 1000000007LL;
}
printf("%lld\n", ans);
}
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
vector<int> v1[100005 + 1];
long long fact[100005];
long long inverse[100005];
int mark[100005];
long long powmod(long long a) {
long long b = 1000000007LL - 2;
long long c = 1000000007LL;
long long ret = 1LL;
while (b) {
if (b & 1) ret = (ret * a) % c;
a = (a * a) % c;
b >>= 1;
}
return (ret) % c;
}
long long mInverse(long long a) {
if (a == 0LL) return 1LL;
return powmod(a);
}
void precompute() {
fact[0] = 1;
inverse[0] = 1;
for (int i = 1; i <= 100000; ++i) {
fact[i] = fact[i - 1] * i;
fact[i] %= 1000000007LL;
inverse[i] = mInverse(fact[i]);
}
}
void sieve() {
int i, j;
memset(mark, 0, sizeof(mark));
for (i = 2; i <= 100005; i += 2) {
v1[i].push_back(2);
mark[i] = 1;
}
for (i = 3; i <= 100005; i += 3)
if (mark[i] == 0) v1[i].push_back(3);
for (i = 5; i * i <= 100005; i += 2) {
if (mark[i] == 0) {
mark[i] = 1;
v1[i].push_back(i);
for (j = i; j * i <= 100005; j += 2) {
if (mark[i * j] == 0) {
mark[i * j] == 1;
v1[i * j].push_back(i);
}
}
}
}
for (i = sqrt(100005); i <= 100005; ++i)
if (mark[i] == 0) v1[i].push_back(i);
}
long long nck(int n, int k) {
if (k > n)
return 0;
else if (k == 0 || k == n)
return 1;
long long res = fact[n];
res = res * inverse[k];
res %= 1000000007LL;
res = res * inverse[n - k];
res %= 1000000007LL;
return res;
}
int main() {
precompute();
sieve();
int q, n, k, i, j;
scanf("%d", &q);
while (q--) {
v.clear();
scanf("%d", &n);
scanf("%d", &k);
long long ans = nck(n - 1, k - 1);
int div = n;
int prev = -1;
while (div > 1) {
if (v1[div][0] != prev) v.push_back(v1[div][0]);
prev = v1[div][0];
div /= v1[div][0];
}
for (i = 1; i < (1 << v.size()); ++i) {
int setbit = __builtin_popcount(i);
int val = 1;
for (j = 0; j < v.size(); ++j) {
if (i & (1 << j)) val = val * v[j];
}
if (setbit & 1)
ans -= nck(n / val - 1, k - 1);
else
ans += nck(n / val - 1, k - 1);
ans = (ans % 1000000007LL + 2 * 1000000007LL) % 1000000007LL;
}
printf("%lld\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const double PI = acos(-1.0);
long long poww(long long a, long long n, long long m) {
long long ans = 1;
long long mul = a;
while (n != 0) {
if (n % 2) {
ans = (ans * mul) % m;
}
mul = (mul * mul) % m;
n /= 2;
}
return ans;
}
const int SZ = 2e5 + 100;
long long factorial[SZ];
void precompute() {
factorial[0] = 1;
for (int i = 1; i < SZ; i++) {
factorial[i] = (factorial[i - 1] * i) % 1000000007;
}
}
long long InverseEuler(long long n, long long m) { return poww(n, m - 2, m); }
long long C(long long n, long long r, long long m) {
return (factorial[n] *
((InverseEuler(factorial[r], m) * InverseEuler(factorial[n - r], m)) %
m)) %
m;
}
const int N = 1e5 + 10;
vector<int> factors[N];
map<pair<int, int>, int> dp;
int get(int n, int f) {
if (dp.count(make_pair(n, f))) {
return dp[make_pair(n, f)];
}
if (n < f) {
return 0;
}
long long ans = C(n - 1, f - 1, 1000000007);
for (int i = 0; i < factors[n].size(); i++) {
ans -= get(n / factors[n][i], f);
ans += 1000000007;
ans %= 1000000007;
}
dp[make_pair(n, f)] = ans;
return ans;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
precompute();
for (int i = 2; i < N; i++) {
for (int j = i; j < N; j += i) {
factors[j].push_back(i);
}
}
int t;
cin >> t;
while (t--) {
int n, f;
cin >> n >> f;
cout << get(n, f) << "\n";
}
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const double PI = acos(-1.0);
long long poww(long long a, long long n, long long m) {
long long ans = 1;
long long mul = a;
while (n != 0) {
if (n % 2) {
ans = (ans * mul) % m;
}
mul = (mul * mul) % m;
n /= 2;
}
return ans;
}
const int SZ = 2e5 + 100;
long long factorial[SZ];
void precompute() {
factorial[0] = 1;
for (int i = 1; i < SZ; i++) {
factorial[i] = (factorial[i - 1] * i) % 1000000007;
}
}
long long InverseEuler(long long n, long long m) { return poww(n, m - 2, m); }
long long C(long long n, long long r, long long m) {
return (factorial[n] *
((InverseEuler(factorial[r], m) * InverseEuler(factorial[n - r], m)) %
m)) %
m;
}
const int N = 1e5 + 10;
vector<int> factors[N];
map<pair<int, int>, int> dp;
int get(int n, int f) {
if (dp.count(make_pair(n, f))) {
return dp[make_pair(n, f)];
}
if (n < f) {
return 0;
}
long long ans = C(n - 1, f - 1, 1000000007);
for (int i = 0; i < factors[n].size(); i++) {
ans -= get(n / factors[n][i], f);
ans += 1000000007;
ans %= 1000000007;
}
dp[make_pair(n, f)] = ans;
return ans;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
precompute();
for (int i = 2; i < N; i++) {
for (int j = i; j < N; j += i) {
factors[j].push_back(i);
}
}
int t;
cin >> t;
while (t--) {
int n, f;
cin >> n >> f;
cout << get(n, f) << "\n";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int ABS(long long int a) {
if (a < 0) return (-a);
return a;
}
void input() {}
long long int pow(long long int a, long long int b, long long int mod) {
long long int x = 1, y = a;
while (b > 0) {
if (b & 1) {
x = (x * y);
if (x >= mod) x %= mod;
}
y = (y * y);
if (y >= mod) y %= mod;
b = (b >> 1);
}
return x;
}
long long int modInverse(long long int a, long long int m) {
return pow(a, m - 2, m);
}
vector<int> v[100005];
long long int F[100005], invF[100005];
void pre() {
for (int i = 1; i < 100005; ++i) {
for (int j = i; j < 100005; j += i) {
v[j].push_back(i);
}
}
F[0] = 1;
for (int i = (1); i < (100005); ++i) F[i] = (F[i - 1] * i) % 1000000007;
for (int i = (0); i < (100005); ++i) invF[i] = modInverse(F[i], 1000000007);
}
long long int dp[100005];
long long int ff(int sweets, int friends) {
if (friends > sweets) return 0;
if (friends == 1) return (sweets == 1 ? 1 : 0);
if (dp[sweets] != -1) return dp[sweets];
long long int res = (F[sweets - 1] * invF[friends - 1]) % 1000000007;
res = (res * invF[sweets - friends]) % 1000000007;
for (int i = (0); i < (v[sweets].size()); ++i) {
if (v[sweets][i] == 1) continue;
res -= ff(sweets / v[sweets][i], friends);
}
res = ((res % 1000000007) + 1000000007) % 1000000007;
dp[sweets] = res;
return res;
}
int main() {
input();
pre();
int t;
scanf("%d", &t);
memset(dp, -1, sizeof(dp));
while (t--) {
int friends, sweets;
scanf("%d", &sweets);
scanf("%d", &friends);
printf("%lld\n", ff(sweets, friends));
for (int i = (0); i < (v[sweets].size()); ++i) dp[v[sweets][i]] = -1;
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int ABS(long long int a) {
if (a < 0) return (-a);
return a;
}
void input() {}
long long int pow(long long int a, long long int b, long long int mod) {
long long int x = 1, y = a;
while (b > 0) {
if (b & 1) {
x = (x * y);
if (x >= mod) x %= mod;
}
y = (y * y);
if (y >= mod) y %= mod;
b = (b >> 1);
}
return x;
}
long long int modInverse(long long int a, long long int m) {
return pow(a, m - 2, m);
}
vector<int> v[100005];
long long int F[100005], invF[100005];
void pre() {
for (int i = 1; i < 100005; ++i) {
for (int j = i; j < 100005; j += i) {
v[j].push_back(i);
}
}
F[0] = 1;
for (int i = (1); i < (100005); ++i) F[i] = (F[i - 1] * i) % 1000000007;
for (int i = (0); i < (100005); ++i) invF[i] = modInverse(F[i], 1000000007);
}
long long int dp[100005];
long long int ff(int sweets, int friends) {
if (friends > sweets) return 0;
if (friends == 1) return (sweets == 1 ? 1 : 0);
if (dp[sweets] != -1) return dp[sweets];
long long int res = (F[sweets - 1] * invF[friends - 1]) % 1000000007;
res = (res * invF[sweets - friends]) % 1000000007;
for (int i = (0); i < (v[sweets].size()); ++i) {
if (v[sweets][i] == 1) continue;
res -= ff(sweets / v[sweets][i], friends);
}
res = ((res % 1000000007) + 1000000007) % 1000000007;
dp[sweets] = res;
return res;
}
int main() {
input();
pre();
int t;
scanf("%d", &t);
memset(dp, -1, sizeof(dp));
while (t--) {
int friends, sweets;
scanf("%d", &sweets);
scanf("%d", &friends);
printf("%lld\n", ff(sweets, friends));
for (int i = (0); i < (v[sweets].size()); ++i) dp[v[sweets][i]] = -1;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int size = 111111;
long long mod = 1000000007;
int n, q, f;
long long ans[size];
int a[size], num;
long long jc[size], r[size];
long long mul(long long a, int p) {
if (p == 0) return 1;
if (p == 1) return a;
long long x = mul(a, p / 2);
x = (x * x) % mod;
if (p & 1) x = (x * a) % mod;
return x;
}
long long divv(int p, int f) {
if (f > p) return 0;
long long x = jc[p - 1] * r[f - 1] % mod;
x = (x * r[p - f]) % mod;
return x;
}
int main() {
cin >> q;
jc[0] = 1;
r[0] = 1;
for (int i = 1; i < size; i++) {
jc[i] = (jc[i - 1] * i % mod);
r[i] = mul(jc[i], mod - 2);
}
while (q--) {
cin >> n >> f;
int m = n;
num = 0;
for (int i = 2; i * i <= m; i++)
if (m % i == 0) {
a[num++] = i;
while (m % i == 0) m /= i;
}
if (m > 1) a[num++] = m;
sort(a, a + num);
long long res = 0;
for (int i = 0; i < (1 << num); i++) {
int ct = 0, tmp = 1;
for (int j = 0; j < num; j++)
if (i & (1 << j)) {
ct++;
tmp *= a[j];
}
ans[i] = divv(n / tmp, f);
if (ct & 1) ans[i] *= -1;
res = (res + ans[i]) % mod;
res = (res + mod) % mod;
}
cout << res << endl;
}
}
|
### Prompt
Develop a solution in CPP to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int size = 111111;
long long mod = 1000000007;
int n, q, f;
long long ans[size];
int a[size], num;
long long jc[size], r[size];
long long mul(long long a, int p) {
if (p == 0) return 1;
if (p == 1) return a;
long long x = mul(a, p / 2);
x = (x * x) % mod;
if (p & 1) x = (x * a) % mod;
return x;
}
long long divv(int p, int f) {
if (f > p) return 0;
long long x = jc[p - 1] * r[f - 1] % mod;
x = (x * r[p - f]) % mod;
return x;
}
int main() {
cin >> q;
jc[0] = 1;
r[0] = 1;
for (int i = 1; i < size; i++) {
jc[i] = (jc[i - 1] * i % mod);
r[i] = mul(jc[i], mod - 2);
}
while (q--) {
cin >> n >> f;
int m = n;
num = 0;
for (int i = 2; i * i <= m; i++)
if (m % i == 0) {
a[num++] = i;
while (m % i == 0) m /= i;
}
if (m > 1) a[num++] = m;
sort(a, a + num);
long long res = 0;
for (int i = 0; i < (1 << num); i++) {
int ct = 0, tmp = 1;
for (int j = 0; j < num; j++)
if (i & (1 << j)) {
ct++;
tmp *= a[j];
}
ans[i] = divv(n / tmp, f);
if (ct & 1) ans[i] *= -1;
res = (res + ans[i]) % mod;
res = (res + mod) % mod;
}
cout << res << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y) {
if (y == 0) return 1;
long long int d = power(x, y / 2) % 1000000007;
if (y % 2 == 0)
return (d * d) % 1000000007;
else
return (((x * d) % 1000000007) * d) % 1000000007;
}
long long int divmod(long long int num, long long int den) {
return (num * (power(den, 1000000007 - 2))) % 1000000007;
}
int mobius[100001];
int mob(int x) {
int cnt = 0;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
x = x / i;
cnt++;
}
if (x % i == 0) {
return 0;
}
}
if (x > 1) {
cnt++;
}
if (cnt & 1) return -1;
return 1;
}
long long int nmod(long long int x) {
return (x % 1000000007 + 1000000007) % 1000000007;
}
long long int fact[100001];
long long int ncr(long long int n, long long int r) {
long long int num = fact[n];
long long int den = fact[r] * fact[n - r];
den %= 1000000007;
return divmod(num, den);
}
long long int calc(long long int n, long long int k, long long int x) {
n = n / x;
n = n - k;
if (n < 0) return 0;
if (n == 0) return 1;
return ncr(n + k - 1, k - 1);
}
void solve() {
int n, k;
scanf("%d", &n);
scanf("%d", &k);
vector<int> f;
for (int i = 1; i * i <= n; i++) {
if (i * i == n) {
f.push_back(i);
break;
}
if (n % i == 0) {
f.push_back(i);
f.push_back(n / i);
}
}
long long int ans = 0;
for (int i = 0; i < f.size(); i++) {
if (mobius[f[i]] == 0) continue;
ans = ans + mobius[f[i]] * calc(n, k, f[i]);
}
ans = nmod(ans);
printf("%lld\n", ans);
}
int main() {
fact[0] = 1;
for (int i = 1; i <= 100000; i++) fact[i] = (i * fact[i - 1]) % 1000000007;
mobius[1] = 1;
for (int i = 2; i <= 100000; i++) {
mobius[i] = mob(i);
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) solve();
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y) {
if (y == 0) return 1;
long long int d = power(x, y / 2) % 1000000007;
if (y % 2 == 0)
return (d * d) % 1000000007;
else
return (((x * d) % 1000000007) * d) % 1000000007;
}
long long int divmod(long long int num, long long int den) {
return (num * (power(den, 1000000007 - 2))) % 1000000007;
}
int mobius[100001];
int mob(int x) {
int cnt = 0;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
x = x / i;
cnt++;
}
if (x % i == 0) {
return 0;
}
}
if (x > 1) {
cnt++;
}
if (cnt & 1) return -1;
return 1;
}
long long int nmod(long long int x) {
return (x % 1000000007 + 1000000007) % 1000000007;
}
long long int fact[100001];
long long int ncr(long long int n, long long int r) {
long long int num = fact[n];
long long int den = fact[r] * fact[n - r];
den %= 1000000007;
return divmod(num, den);
}
long long int calc(long long int n, long long int k, long long int x) {
n = n / x;
n = n - k;
if (n < 0) return 0;
if (n == 0) return 1;
return ncr(n + k - 1, k - 1);
}
void solve() {
int n, k;
scanf("%d", &n);
scanf("%d", &k);
vector<int> f;
for (int i = 1; i * i <= n; i++) {
if (i * i == n) {
f.push_back(i);
break;
}
if (n % i == 0) {
f.push_back(i);
f.push_back(n / i);
}
}
long long int ans = 0;
for (int i = 0; i < f.size(); i++) {
if (mobius[f[i]] == 0) continue;
ans = ans + mobius[f[i]] * calc(n, k, f[i]);
}
ans = nmod(ans);
printf("%lld\n", ans);
}
int main() {
fact[0] = 1;
for (int i = 1; i <= 100000; i++) fact[i] = (i * fact[i - 1]) % 1000000007;
mobius[1] = 1;
for (int i = 2; i <= 100000; i++) {
mobius[i] = mob(i);
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
int pm(int a, int e) {
int r = 1;
while (e) {
if (e & 1) r = (1LL * r * a) % 1000000007;
e >>= 1;
a = (1LL * a * a) % 1000000007;
}
return r;
}
int f[100005], fi[100005];
int c(int n, int k) {
if (k < 0 || k > n) return 0;
return 1LL * f[n] * fi[k] % 1000000007 * fi[n - k] % 1000000007;
}
vector<int> p[100005];
int main() {
f[0] = fi[0] = 1;
for (int i = 1, ThxDem = 100005; i < ThxDem; ++i)
f[i] = (1LL * f[i - 1] * i) % 1000000007, fi[i] = pm(f[i], 1000000007 - 2);
for (int i = 2, ThxDem = 100005; i < ThxDem; ++i)
if (!((int)p[i].size()))
for (int j = i; j < 100005; j += i) p[j].push_back(i);
int q;
scanf("%d", &q);
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
int r = 0;
for (int m = 0, ThxDem = 1 << ((int)p[n].size()); m < ThxDem; ++m) {
int d = 1, q = 0;
for (int i = 0, ThxDem = ((int)p[n].size()); i < ThxDem; ++i)
if (m & (1 << i)) d *= p[n][i], q ^= 1;
int a = c(n / d - 1, f - 1);
if (q)
r += 1000000007 - a;
else
r += a;
r %= 1000000007;
}
printf("%d\n", r);
}
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int pm(int a, int e) {
int r = 1;
while (e) {
if (e & 1) r = (1LL * r * a) % 1000000007;
e >>= 1;
a = (1LL * a * a) % 1000000007;
}
return r;
}
int f[100005], fi[100005];
int c(int n, int k) {
if (k < 0 || k > n) return 0;
return 1LL * f[n] * fi[k] % 1000000007 * fi[n - k] % 1000000007;
}
vector<int> p[100005];
int main() {
f[0] = fi[0] = 1;
for (int i = 1, ThxDem = 100005; i < ThxDem; ++i)
f[i] = (1LL * f[i - 1] * i) % 1000000007, fi[i] = pm(f[i], 1000000007 - 2);
for (int i = 2, ThxDem = 100005; i < ThxDem; ++i)
if (!((int)p[i].size()))
for (int j = i; j < 100005; j += i) p[j].push_back(i);
int q;
scanf("%d", &q);
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
int r = 0;
for (int m = 0, ThxDem = 1 << ((int)p[n].size()); m < ThxDem; ++m) {
int d = 1, q = 0;
for (int i = 0, ThxDem = ((int)p[n].size()); i < ThxDem; ++i)
if (m & (1 << i)) d *= p[n][i], q ^= 1;
int a = c(n / d - 1, f - 1);
if (q)
r += 1000000007 - a;
else
r += a;
r %= 1000000007;
}
printf("%d\n", r);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 2e5 + 10;
const int Inf = 0x7f7f7f7f;
const long long Inf_ll = 1ll * Inf * Inf;
const int Mod = 1e9 + 7;
const double eps = 1e-7;
void r(int &p) {
char c = getchar();
int x = 0, fh = 0;
while (c < '0' || c > '9') {
fh |= c == '-';
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
p = fh ? -x : x;
}
long long inv[Maxn], fac[Maxn], finv[Maxn];
void Init(int n) {
inv[0] = inv[1] = fac[0] = finv[0] = 1;
for (int i = 2; i <= n; i++)
inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % Mod, finv[i] = finv[i - 1] * inv[i] % Mod;
}
int prim[Maxn], tol, mu[Maxn], phi[Maxn];
bool vis[Maxn];
void Get(int n) {
mu[1] = phi[1] = 1;
for (int i = 2; i <= n; i++) {
if (!vis[i]) prim[++tol] = i, mu[i] = -1, phi[i] = i - 1;
for (int j = 1; j <= tol && i * prim[j] <= n; j++) {
vis[i * prim[j]] = true;
if (i % prim[j] == 0) {
phi[i * prim[j]] = phi[i] * prim[j];
break;
}
mu[i * prim[j]] = -mu[i];
phi[i * prim[j]] = phi[i] * (prim[j] - 1);
}
}
}
long long C(int n, int m) {
if (n < m) return 0;
return fac[n] * finv[m] % Mod * finv[n - m] % Mod;
}
long long F(int n, int i, int k) { return C(n / i - 1, k - 1); }
int main() {
Get(Maxn - 10);
Init(Maxn - 10);
int T;
scanf("%d", &T);
for (int _ = 1; _ <= T; _++) {
int n, k;
scanf("%d %d", &n, &k);
if (k == 1 && n != 1) {
puts("0");
continue;
}
long long ans = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = (ans + mu[i] * F(n, i, k) % Mod + Mod) % Mod;
if (i * i != n)
ans = (ans + mu[n / i] * F(n, n / i, k) % Mod + Mod) % Mod;
}
}
printf("%lld\n", ans % Mod);
}
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 2e5 + 10;
const int Inf = 0x7f7f7f7f;
const long long Inf_ll = 1ll * Inf * Inf;
const int Mod = 1e9 + 7;
const double eps = 1e-7;
void r(int &p) {
char c = getchar();
int x = 0, fh = 0;
while (c < '0' || c > '9') {
fh |= c == '-';
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
p = fh ? -x : x;
}
long long inv[Maxn], fac[Maxn], finv[Maxn];
void Init(int n) {
inv[0] = inv[1] = fac[0] = finv[0] = 1;
for (int i = 2; i <= n; i++)
inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % Mod, finv[i] = finv[i - 1] * inv[i] % Mod;
}
int prim[Maxn], tol, mu[Maxn], phi[Maxn];
bool vis[Maxn];
void Get(int n) {
mu[1] = phi[1] = 1;
for (int i = 2; i <= n; i++) {
if (!vis[i]) prim[++tol] = i, mu[i] = -1, phi[i] = i - 1;
for (int j = 1; j <= tol && i * prim[j] <= n; j++) {
vis[i * prim[j]] = true;
if (i % prim[j] == 0) {
phi[i * prim[j]] = phi[i] * prim[j];
break;
}
mu[i * prim[j]] = -mu[i];
phi[i * prim[j]] = phi[i] * (prim[j] - 1);
}
}
}
long long C(int n, int m) {
if (n < m) return 0;
return fac[n] * finv[m] % Mod * finv[n - m] % Mod;
}
long long F(int n, int i, int k) { return C(n / i - 1, k - 1); }
int main() {
Get(Maxn - 10);
Init(Maxn - 10);
int T;
scanf("%d", &T);
for (int _ = 1; _ <= T; _++) {
int n, k;
scanf("%d %d", &n, &k);
if (k == 1 && n != 1) {
puts("0");
continue;
}
long long ans = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = (ans + mu[i] * F(n, i, k) % Mod + Mod) % Mod;
if (i * i != n)
ans = (ans + mu[n / i] * F(n, n / i, k) % Mod + Mod) % Mod;
}
}
printf("%lld\n", ans % Mod);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct node {
int nxt, t;
} A[1166900];
int head[100010], node_cnt;
void add_edge(int x, int y) {
A[node_cnt] = (node){head[x], y};
head[x] = node_cnt++;
}
int pri[100010], pri_cnt, mu[100010];
bool mark[100010];
void sieve() {
int i, j;
mu[1] = 1;
for (i = 2; i < 100010; i++) {
if (!mark[i]) {
pri[++pri_cnt] = i;
mu[i] = -1;
}
for (j = 1; j <= pri_cnt; j++) {
int t = i * pri[j];
if (t >= 100010) {
break;
}
mark[t] = 1;
if ((i % pri[j]) == 0) {
mu[t] = 0;
break;
}
mu[t] = -mu[i];
}
}
memset(head, -1, sizeof(head));
for (i = 1; i < 100010; i++) {
for (j = i; j < 100010; j += i) {
add_edge(j, i);
}
}
}
int Fac[100010], Inv[100010], InvFac[100010];
void init() {
sieve();
int i;
Fac[0] = 1;
for (i = 1; i < 100010; i++) {
Fac[i] = 1LL * Fac[i - 1] * i % 1000000007;
}
Inv[1] = 1;
for (i = 2; i < 100010; i++) {
Inv[i] =
1LL * (1000000007 - 1000000007 / i) * Inv[1000000007 % i] % 1000000007;
}
InvFac[0] = 1;
for (i = 1; i < 100010; i++) {
InvFac[i] = 1LL * InvFac[i - 1] * Inv[i] % 1000000007;
}
}
int C(int n, int m) {
if (n < m) {
return 0;
}
return 1LL * Fac[n] * InvFac[n - m] % 1000000007 * InvFac[m] % 1000000007;
}
int main() {
init();
int Case;
scanf("%d", &Case);
while (Case--) {
int i, n, m, Ans = 0;
scanf("%d%d", &n, &m);
for (i = head[n]; ~i; i = A[i].nxt) {
int x = A[i].t, y = n / x;
Ans = (Ans + mu[x] * C(y - 1, m - 1)) % 1000000007;
}
if (Ans < 0) {
Ans += 1000000007;
}
printf("%d\n", Ans);
}
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct node {
int nxt, t;
} A[1166900];
int head[100010], node_cnt;
void add_edge(int x, int y) {
A[node_cnt] = (node){head[x], y};
head[x] = node_cnt++;
}
int pri[100010], pri_cnt, mu[100010];
bool mark[100010];
void sieve() {
int i, j;
mu[1] = 1;
for (i = 2; i < 100010; i++) {
if (!mark[i]) {
pri[++pri_cnt] = i;
mu[i] = -1;
}
for (j = 1; j <= pri_cnt; j++) {
int t = i * pri[j];
if (t >= 100010) {
break;
}
mark[t] = 1;
if ((i % pri[j]) == 0) {
mu[t] = 0;
break;
}
mu[t] = -mu[i];
}
}
memset(head, -1, sizeof(head));
for (i = 1; i < 100010; i++) {
for (j = i; j < 100010; j += i) {
add_edge(j, i);
}
}
}
int Fac[100010], Inv[100010], InvFac[100010];
void init() {
sieve();
int i;
Fac[0] = 1;
for (i = 1; i < 100010; i++) {
Fac[i] = 1LL * Fac[i - 1] * i % 1000000007;
}
Inv[1] = 1;
for (i = 2; i < 100010; i++) {
Inv[i] =
1LL * (1000000007 - 1000000007 / i) * Inv[1000000007 % i] % 1000000007;
}
InvFac[0] = 1;
for (i = 1; i < 100010; i++) {
InvFac[i] = 1LL * InvFac[i - 1] * Inv[i] % 1000000007;
}
}
int C(int n, int m) {
if (n < m) {
return 0;
}
return 1LL * Fac[n] * InvFac[n - m] % 1000000007 * InvFac[m] % 1000000007;
}
int main() {
init();
int Case;
scanf("%d", &Case);
while (Case--) {
int i, n, m, Ans = 0;
scanf("%d%d", &n, &m);
for (i = head[n]; ~i; i = A[i].nxt) {
int x = A[i].t, y = n / x;
Ans = (Ans + mu[x] * C(y - 1, m - 1)) % 1000000007;
}
if (Ans < 0) {
Ans += 1000000007;
}
printf("%d\n", Ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793;
const long long P = 1e9 + 7;
class mint {
public:
static int init_siz;
static vector<mint> inverse, factorial;
static int mintP;
int n;
static mint exp(mint a, long long e) {
mint res = 1;
while (e) {
if (e & 1) res *= a;
a *= a;
e >>= 1;
}
return res;
}
static void init(int n) {
init_siz = n;
inverse.resize(n + 1);
factorial.resize(n + 1);
factorial[0] = 1;
for (int i = 1; i <= n; i++) factorial[i] = factorial[i - 1] * i;
inverse[n] = exp(factorial[n], mintP - 2);
for (int i = n - 1; i >= 0; i--) inverse[i] = inverse[i + 1] * (i + 1);
}
static mint inv(mint const& a) {
return (init_siz >= a.n ? inverse[a.n] * (a.n > 1 ? fac(a - 1) : 1)
: exp(a, mintP - 2));
}
static mint cinv(mint const& a) {
return (init_siz >= a.n ? inverse[a.n] : exp(fac(a), mintP - 2));
}
static mint fac(mint const& a) {
if (init_siz >= a.n)
return factorial[a.n];
else {
mint out = 1;
for (int i = 0; i < a.n; i++) out *= (i + 1);
return out;
}
}
mint(long long val = 0) {
n = val % mintP;
if (n < 0) n += mintP;
}
mint& operator+=(mint const& b) {
n += b.n;
if (n >= mintP) n -= mintP;
return *this;
}
mint& operator-=(mint const& b) {
n -= b.n;
if (n < 0) n += mintP;
return *this;
}
mint& operator*=(mint const& b) {
n = (long long)n * b.n % mintP;
return *this;
}
mint exp(long long e) { return exp(*this, e); }
mint inv() { return inv(*this); }
mint& operator/=(mint const& b) { return *this *= inv(b); }
friend std::ostream& operator<<(std::ostream& os, mint const& a) {
return os << (a.n < 0 ? a.n + mintP : a.n);
}
friend mint operator+(mint a, mint const b) { return a += b; }
friend mint operator-(mint a, mint const b) { return a -= b; }
friend mint operator-(mint const a) { return 0 - a; }
friend mint operator*(mint a, mint const b) { return a *= b; }
friend mint operator/(mint a, mint const b) { return a /= b; }
friend bool operator==(mint const& a, mint const& b) { return a.n == b.n; }
friend bool operator!=(mint const& a, mint const& b) { return a.n != b.n; }
};
int mint::init_siz = 0;
int mint::mintP = P;
vector<mint> mint::factorial, mint::inverse;
mint comb(int n, int r) {
if (n < r)
return 0;
else {
return (mint::fac(n) * mint::cinv(r) * mint::cinv(n - r));
}
}
const int mx = 1e5 + 1;
int n, k;
bool nprime[mx] = {};
int mob[mx] = {};
void init() {
fill(mob, mob + mx, 1);
for (int i = 2; i < mx; i++) {
if (nprime[i]) continue;
for (int j = i; j < mx; j += i) {
if (j % (i * i) == 0)
mob[j] = 0;
else if (j % i == 0)
mob[j] *= -1;
nprime[j] = 1;
}
}
}
void solve() {
cin >> n >> k;
mint ans = 0;
for (int p = 1; p * p <= n; p++) {
if (n % p) continue;
ans += (mob[p] * comb(n / p - 1, k - 1));
if (p * p != n) {
ans += (mob[n / p] * comb(p - 1, k - 1));
}
}
cout << ans << '\n';
}
int main() {
mint::init(mx);
init();
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
cin >> T;
for (int c = 0; c < T; c++) {
solve();
}
}
|
### Prompt
Create a solution in cpp for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793;
const long long P = 1e9 + 7;
class mint {
public:
static int init_siz;
static vector<mint> inverse, factorial;
static int mintP;
int n;
static mint exp(mint a, long long e) {
mint res = 1;
while (e) {
if (e & 1) res *= a;
a *= a;
e >>= 1;
}
return res;
}
static void init(int n) {
init_siz = n;
inverse.resize(n + 1);
factorial.resize(n + 1);
factorial[0] = 1;
for (int i = 1; i <= n; i++) factorial[i] = factorial[i - 1] * i;
inverse[n] = exp(factorial[n], mintP - 2);
for (int i = n - 1; i >= 0; i--) inverse[i] = inverse[i + 1] * (i + 1);
}
static mint inv(mint const& a) {
return (init_siz >= a.n ? inverse[a.n] * (a.n > 1 ? fac(a - 1) : 1)
: exp(a, mintP - 2));
}
static mint cinv(mint const& a) {
return (init_siz >= a.n ? inverse[a.n] : exp(fac(a), mintP - 2));
}
static mint fac(mint const& a) {
if (init_siz >= a.n)
return factorial[a.n];
else {
mint out = 1;
for (int i = 0; i < a.n; i++) out *= (i + 1);
return out;
}
}
mint(long long val = 0) {
n = val % mintP;
if (n < 0) n += mintP;
}
mint& operator+=(mint const& b) {
n += b.n;
if (n >= mintP) n -= mintP;
return *this;
}
mint& operator-=(mint const& b) {
n -= b.n;
if (n < 0) n += mintP;
return *this;
}
mint& operator*=(mint const& b) {
n = (long long)n * b.n % mintP;
return *this;
}
mint exp(long long e) { return exp(*this, e); }
mint inv() { return inv(*this); }
mint& operator/=(mint const& b) { return *this *= inv(b); }
friend std::ostream& operator<<(std::ostream& os, mint const& a) {
return os << (a.n < 0 ? a.n + mintP : a.n);
}
friend mint operator+(mint a, mint const b) { return a += b; }
friend mint operator-(mint a, mint const b) { return a -= b; }
friend mint operator-(mint const a) { return 0 - a; }
friend mint operator*(mint a, mint const b) { return a *= b; }
friend mint operator/(mint a, mint const b) { return a /= b; }
friend bool operator==(mint const& a, mint const& b) { return a.n == b.n; }
friend bool operator!=(mint const& a, mint const& b) { return a.n != b.n; }
};
int mint::init_siz = 0;
int mint::mintP = P;
vector<mint> mint::factorial, mint::inverse;
mint comb(int n, int r) {
if (n < r)
return 0;
else {
return (mint::fac(n) * mint::cinv(r) * mint::cinv(n - r));
}
}
const int mx = 1e5 + 1;
int n, k;
bool nprime[mx] = {};
int mob[mx] = {};
void init() {
fill(mob, mob + mx, 1);
for (int i = 2; i < mx; i++) {
if (nprime[i]) continue;
for (int j = i; j < mx; j += i) {
if (j % (i * i) == 0)
mob[j] = 0;
else if (j % i == 0)
mob[j] *= -1;
nprime[j] = 1;
}
}
}
void solve() {
cin >> n >> k;
mint ans = 0;
for (int p = 1; p * p <= n; p++) {
if (n % p) continue;
ans += (mob[p] * comb(n / p - 1, k - 1));
if (p * p != n) {
ans += (mob[n / p] * comb(p - 1, k - 1));
}
}
cout << ans << '\n';
}
int main() {
mint::init(mx);
init();
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
cin >> T;
for (int c = 0; c < T; c++) {
solve();
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long inv[100005];
long long invFact[100005];
long long fact[100005];
long long numPart(int n, int f) {
n = n - f;
if (n < 0) return 0;
long long ret = fact[n + f - 1] * invFact[n] % 1000000007;
ret = ret * invFact[f - 1] % 1000000007;
return ret;
}
int main() {
int t;
cin >> t;
inv[1] = 1;
for (int i = 2; i < 100005; i++) {
inv[i] =
(1000000007 - (1000000007 / i) * inv[1000000007 % i] % 1000000007) %
1000000007;
}
fact[0] = 1;
invFact[0] = 1;
for (int i = 1; i < 100005; i++) {
fact[i] = fact[i - 1] * (i) % 1000000007;
invFact[i] = invFact[i - 1] * inv[i] % 1000000007;
}
while (t--) {
int n, f;
cin >> n >> f;
long long x = n;
vector<int> primes;
for (int i = 2; i * i <= x; i++) {
if (n % i == 0) {
while (n % i == 0) n = n / i;
if (i < x) primes.push_back(i);
}
}
if (n != 1) primes.push_back(n);
int numPrimes = primes.size();
long long ans = 0;
for (int i = 1; i < (1 << numPrimes); i++) {
int numBits = __builtin_popcount(i);
long long g = 1;
for (int j = 0; j < numPrimes; j++) {
if (i & (1 << j)) g = g * primes[j];
}
if (numBits % 2 == 1) {
ans = ans + numPart(x / g, f);
} else {
ans = ans + 1000000007 - numPart(x / g, f);
}
ans = ans % 1000000007;
}
ans = (numPart(x, f) - ans + 1000000007) % 1000000007;
cout << ans << endl;
}
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long inv[100005];
long long invFact[100005];
long long fact[100005];
long long numPart(int n, int f) {
n = n - f;
if (n < 0) return 0;
long long ret = fact[n + f - 1] * invFact[n] % 1000000007;
ret = ret * invFact[f - 1] % 1000000007;
return ret;
}
int main() {
int t;
cin >> t;
inv[1] = 1;
for (int i = 2; i < 100005; i++) {
inv[i] =
(1000000007 - (1000000007 / i) * inv[1000000007 % i] % 1000000007) %
1000000007;
}
fact[0] = 1;
invFact[0] = 1;
for (int i = 1; i < 100005; i++) {
fact[i] = fact[i - 1] * (i) % 1000000007;
invFact[i] = invFact[i - 1] * inv[i] % 1000000007;
}
while (t--) {
int n, f;
cin >> n >> f;
long long x = n;
vector<int> primes;
for (int i = 2; i * i <= x; i++) {
if (n % i == 0) {
while (n % i == 0) n = n / i;
if (i < x) primes.push_back(i);
}
}
if (n != 1) primes.push_back(n);
int numPrimes = primes.size();
long long ans = 0;
for (int i = 1; i < (1 << numPrimes); i++) {
int numBits = __builtin_popcount(i);
long long g = 1;
for (int j = 0; j < numPrimes; j++) {
if (i & (1 << j)) g = g * primes[j];
}
if (numBits % 2 == 1) {
ans = ans + numPart(x / g, f);
} else {
ans = ans + 1000000007 - numPart(x / g, f);
}
ans = ans % 1000000007;
}
ans = (numPart(x, f) - ans + 1000000007) % 1000000007;
cout << ans << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 7;
const int MXN = (int)1e5 + 7;
int n, k, t;
int m[MXN];
long long fac[MXN], rev[MXN];
long long binpow(long long x, long long y) {
long long res = 1;
while (y) {
if (y & 1) res = (1LL * res * x) % INF;
y >>= 1;
x = (1LL * x * x) % INF;
}
return res;
}
long long c(long long x, long long y) {
if (y > x) return 0;
long long res = fac[x];
res = (res * rev[x - y]) % INF;
res = (res * rev[y]) % INF;
return res;
}
long long f(long long d) {
long long res = c(n / d - 1, k - 1);
return res;
}
int main() {
ios_base::sync_with_stdio(0);
fac[0] = 1;
rev[0] = 1;
for (int i = 1; i < MXN; i++) fac[i] = (fac[i - 1] * 1LL * i) % INF;
for (int i = 1; i < MXN; i++) {
m[i] = 1;
int cur = i;
for (int d = 2; d * d <= cur; d++) {
if (cur % d != 0) continue;
int cnt = 0;
while (cur % d == 0) {
cur /= d;
cnt++;
}
if (cnt >= 2) {
m[i] = 0;
break;
}
m[i] = -m[i];
}
if (cur > 1) m[i] = -m[i];
rev[i] = binpow(fac[i], INF - 2);
}
cin >> t;
while (t--) {
cin >> n >> k;
long long ans = 0;
for (int d = 1; d * d <= n; d++) {
if (n % d != 0) continue;
ans = (ans + m[d] * f(d)) % INF;
if (n / d != d) ans = (ans + m[n / d] * f(n / d)) % INF;
if (ans < 0) ans += INF;
}
cout << ans << "\n";
}
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 7;
const int MXN = (int)1e5 + 7;
int n, k, t;
int m[MXN];
long long fac[MXN], rev[MXN];
long long binpow(long long x, long long y) {
long long res = 1;
while (y) {
if (y & 1) res = (1LL * res * x) % INF;
y >>= 1;
x = (1LL * x * x) % INF;
}
return res;
}
long long c(long long x, long long y) {
if (y > x) return 0;
long long res = fac[x];
res = (res * rev[x - y]) % INF;
res = (res * rev[y]) % INF;
return res;
}
long long f(long long d) {
long long res = c(n / d - 1, k - 1);
return res;
}
int main() {
ios_base::sync_with_stdio(0);
fac[0] = 1;
rev[0] = 1;
for (int i = 1; i < MXN; i++) fac[i] = (fac[i - 1] * 1LL * i) % INF;
for (int i = 1; i < MXN; i++) {
m[i] = 1;
int cur = i;
for (int d = 2; d * d <= cur; d++) {
if (cur % d != 0) continue;
int cnt = 0;
while (cur % d == 0) {
cur /= d;
cnt++;
}
if (cnt >= 2) {
m[i] = 0;
break;
}
m[i] = -m[i];
}
if (cur > 1) m[i] = -m[i];
rev[i] = binpow(fac[i], INF - 2);
}
cin >> t;
while (t--) {
cin >> n >> k;
long long ans = 0;
for (int d = 1; d * d <= n; d++) {
if (n % d != 0) continue;
ans = (ans + m[d] * f(d)) % INF;
if (n / d != d) ans = (ans + m[n / d] * f(n / d)) % INF;
if (ans < 0) ans += INF;
}
cout << ans << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
vector<int> p;
int a[100010];
int fa[200010], inv[200010], ifa[200010];
int go(int n, int f) {
if (n < f) return 0;
if (f == 1) return 1;
n -= f;
--f;
long long res = fa[n + f];
res = res * ifa[f] % mod;
res = res * ifa[n] % mod;
return res;
}
int main() {
int q;
scanf("%d", &q);
for (int i = 2; i < 100010; i++) {
if (a[i]) continue;
p.push_back(i);
for (int j = i + i; j < 100010; j += i) {
a[j] = i;
}
}
fa[0] = fa[1] = 1;
for (int i = 2; i < 200010; i++) {
fa[i] = (long long)fa[i - 1] * i % mod;
}
inv[1] = 1;
ifa[0] = ifa[1] = 1;
for (int i = 2; i < 200010; i++) {
inv[i] = mod - (long long)inv[mod % i] * (mod / i) % mod;
ifa[i] = (long long)ifa[i - 1] * inv[i] % mod;
}
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
if (n == 1) {
if (f == 1) {
printf("1\n");
} else {
printf("0\n");
}
continue;
}
int ans = 0, n_ = n;
vector<int> ps;
for (int i = 2; i * i <= n_; i++) {
if (n_ % i == 0) {
ps.push_back(i);
while (n_ % i == 0) {
n_ /= i;
}
}
}
if (n_ != 1) {
ps.push_back(n_);
}
for (int i = 0; i < 1 << ps.size(); i++) {
int t = 1, s = 1, r;
for (int j = 0; j < ps.size(); j++) {
if (i >> j & 1) {
t *= ps[j];
s = -s;
}
}
r = go(n / t, f);
if (s == -1) {
r = mod - r;
}
ans = (ans + r) % mod;
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
vector<int> p;
int a[100010];
int fa[200010], inv[200010], ifa[200010];
int go(int n, int f) {
if (n < f) return 0;
if (f == 1) return 1;
n -= f;
--f;
long long res = fa[n + f];
res = res * ifa[f] % mod;
res = res * ifa[n] % mod;
return res;
}
int main() {
int q;
scanf("%d", &q);
for (int i = 2; i < 100010; i++) {
if (a[i]) continue;
p.push_back(i);
for (int j = i + i; j < 100010; j += i) {
a[j] = i;
}
}
fa[0] = fa[1] = 1;
for (int i = 2; i < 200010; i++) {
fa[i] = (long long)fa[i - 1] * i % mod;
}
inv[1] = 1;
ifa[0] = ifa[1] = 1;
for (int i = 2; i < 200010; i++) {
inv[i] = mod - (long long)inv[mod % i] * (mod / i) % mod;
ifa[i] = (long long)ifa[i - 1] * inv[i] % mod;
}
while (q--) {
int n, f;
scanf("%d%d", &n, &f);
if (n == 1) {
if (f == 1) {
printf("1\n");
} else {
printf("0\n");
}
continue;
}
int ans = 0, n_ = n;
vector<int> ps;
for (int i = 2; i * i <= n_; i++) {
if (n_ % i == 0) {
ps.push_back(i);
while (n_ % i == 0) {
n_ /= i;
}
}
}
if (n_ != 1) {
ps.push_back(n_);
}
for (int i = 0; i < 1 << ps.size(); i++) {
int t = 1, s = 1, r;
for (int j = 0; j < ps.size(); j++) {
if (i >> j & 1) {
t *= ps[j];
s = -s;
}
}
r = go(n / t, f);
if (s == -1) {
r = mod - r;
}
ans = (ans + r) % mod;
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
static inline int Rint() {
struct X {
int dig[256];
X() {
for (int i = '0'; i <= '9'; ++i) dig[i] = 1;
dig['-'] = 1;
}
};
static X fuck;
int s = 1, v = 0, c;
for (; !fuck.dig[c = getchar()];)
;
if (c == '-')
s = 0;
else if (fuck.dig[c])
v = c ^ 48;
for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48))
;
return s ? v : -v;
}
template <typename T>
static inline void cmax(T& a, const T& b) {
if (b > a) a = b;
}
template <typename T>
static inline void cmin(T& a, const T& b) {
if (b < a) a = b;
}
template <long long P>
struct moder {
static long long inv(long long x) {
long long result = 1;
int n = P - 2;
x %= P;
for (; n; n >>= 1, x = x * x % P)
if (n & 1) result = result * x % P;
return result;
}
static long long* fac;
static void init_fac() {
fac = new long long[1048576];
fac[0] = 1;
for (int i = 1; i < 1048576; ++i) fac[i] = fac[i - 1] * i % P;
}
static void destroy_fac() { delete[] fac; }
static long long s(int x) { return x & 1 ? -1 : 1; }
static long long comb(long long m, long long n) {
if (n > m || n < 0) return 0;
long long pp = 0;
long long dist = m - n;
for (long long x = P; x <= m; x *= P) pp += m / x - n / x - dist / x;
if (pp) return 0;
long long l = 1, r = 1;
for (long long x = m; x; x /= P) l = l * s(x / P) * fac[x % P] % P;
for (long long x = n; x; x /= P) r = r * s(x / P) * fac[x % P] % P;
for (long long x = dist; x; x /= P) r = r * s(x / P) * fac[x % P] % P;
l = (l + P) % P;
r = (r + P) % P;
long long t = (inv(r) * l % P + P) % P;
return t;
}
};
const long long p = 1000000007;
const long long mod = p;
template <long long P>
long long* moder<P>::fac;
moder<p> md;
const int maxp = 200005;
static int plist[maxp / 3], pmask[maxp + 1];
int pcnt;
void init_primes() {
for (int i = 1; i <= maxp; ++i) pmask[i] = i;
for (int i = 2; i <= maxp; ++i) {
if (pmask[i] == i) {
plist[pcnt++] = i;
}
for (int j = 0; j < pcnt; ++j) {
const long long t = (long long)plist[j] * i;
if (t > maxp) break;
pmask[t] = plist[j];
if (i % plist[j] == 0) {
break;
}
}
}
}
static long long pv[1024];
int pc[1024];
static long long factors[655360];
static int factor_cnt;
void get_factors(int limit, long long value) {
factors[factor_cnt++] = value;
for (int i = 0; i < limit; ++i) {
long long tvalue = value;
const int p = pv[i];
for (int j = 1; j <= pc[i]; ++j) {
tvalue *= p;
get_factors(i, tvalue);
}
}
}
int factorize(long long n) {
int top = 0;
if (n <= maxp) {
while (n != 1) {
int now = pmask[n];
int c = 0;
while (n % now == 0) n /= now, ++c;
if (c) pv[top] = now, pc[top++] = c;
}
} else {
for (int i = 0; i < pcnt; ++i) {
const long long p = plist[i];
const long long test = p * p;
if (p > n) break;
int c = 0;
while (n % p == 0) n /= p, ++c;
if (c) pv[top] = p, pc[top++] = c;
}
if (n != 1) pv[top] = n, pc[top++] = 1;
}
return top;
}
static inline long long add(long long a, long long b) {
a += b;
if (a >= mod) a -= mod;
return a;
}
static inline long long sub(long long a, long long b) {
a -= b;
if (a < 0) a += mod;
return a;
}
long long dp[655360];
vector<int> adj[1005];
struct Query {
int n, f;
int id, ans;
};
int cmp_n(const Query& a, const Query& b) {
if (a.n != b.n) return a.n < b.n;
return a.f < b.f;
}
int cmp_id(const Query& a, const Query& b) { return a.id < b.id; }
Query data[100005];
int main() {
md.init_fac();
init_primes();
int q = Rint();
for (int i = (0); i < (q); ++i)
data[i].n = Rint(), data[i].f = Rint(), data[i].id = i;
sort(data, data + q, cmp_n);
for (int x = 0; x < q; ++x) {
const int n = data[x].n, f = data[x].f;
if (n == 1) {
data[x].ans = f == 1 ? 1 : 0;
continue;
}
if (f > n) {
data[x].ans = 0;
continue;
}
if (f == 1) {
data[x].ans = 0;
continue;
}
if (f == n) {
data[x].ans = 1;
continue;
}
factor_cnt = 0;
get_factors(factorize(n), 1);
sort(factors, factors + factor_cnt);
for (int i = 0; i < factor_cnt; ++i) adj[i].clear();
int t = 0;
for (int i = 0; i < factor_cnt; ++i)
for (int j = i + 1; j < factor_cnt; ++j)
if (factors[j] % factors[i] == 0) adj[i].push_back(j), ++t;
int j = x;
while (j < q && data[j].n == data[x].n) {
const int f = data[j].f;
if (j > x && f == data[j - 1].f) {
data[j].ans = data[j - 1].ans;
++j;
continue;
}
for (int i = 0; i < factor_cnt; ++i) dp[i] = 0;
for (int i = 0; i < factor_cnt; ++i) {
const long long val = factors[i];
if (val < f) {
dp[i] = 0;
continue;
}
const long long t = md.comb(f + val - f - 1, val - f);
dp[i] = add(dp[i], t);
for (int j = 0; j < adj[i].size(); ++j)
dp[adj[i][j]] = sub(dp[adj[i][j]], dp[i]);
}
data[j].ans = dp[factor_cnt - 1];
++j;
}
x = j - 1;
}
sort(data, data + q, cmp_id);
for (int i = 0; i < q; ++i) printf("%d\n", data[i].ans);
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
static inline int Rint() {
struct X {
int dig[256];
X() {
for (int i = '0'; i <= '9'; ++i) dig[i] = 1;
dig['-'] = 1;
}
};
static X fuck;
int s = 1, v = 0, c;
for (; !fuck.dig[c = getchar()];)
;
if (c == '-')
s = 0;
else if (fuck.dig[c])
v = c ^ 48;
for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48))
;
return s ? v : -v;
}
template <typename T>
static inline void cmax(T& a, const T& b) {
if (b > a) a = b;
}
template <typename T>
static inline void cmin(T& a, const T& b) {
if (b < a) a = b;
}
template <long long P>
struct moder {
static long long inv(long long x) {
long long result = 1;
int n = P - 2;
x %= P;
for (; n; n >>= 1, x = x * x % P)
if (n & 1) result = result * x % P;
return result;
}
static long long* fac;
static void init_fac() {
fac = new long long[1048576];
fac[0] = 1;
for (int i = 1; i < 1048576; ++i) fac[i] = fac[i - 1] * i % P;
}
static void destroy_fac() { delete[] fac; }
static long long s(int x) { return x & 1 ? -1 : 1; }
static long long comb(long long m, long long n) {
if (n > m || n < 0) return 0;
long long pp = 0;
long long dist = m - n;
for (long long x = P; x <= m; x *= P) pp += m / x - n / x - dist / x;
if (pp) return 0;
long long l = 1, r = 1;
for (long long x = m; x; x /= P) l = l * s(x / P) * fac[x % P] % P;
for (long long x = n; x; x /= P) r = r * s(x / P) * fac[x % P] % P;
for (long long x = dist; x; x /= P) r = r * s(x / P) * fac[x % P] % P;
l = (l + P) % P;
r = (r + P) % P;
long long t = (inv(r) * l % P + P) % P;
return t;
}
};
const long long p = 1000000007;
const long long mod = p;
template <long long P>
long long* moder<P>::fac;
moder<p> md;
const int maxp = 200005;
static int plist[maxp / 3], pmask[maxp + 1];
int pcnt;
void init_primes() {
for (int i = 1; i <= maxp; ++i) pmask[i] = i;
for (int i = 2; i <= maxp; ++i) {
if (pmask[i] == i) {
plist[pcnt++] = i;
}
for (int j = 0; j < pcnt; ++j) {
const long long t = (long long)plist[j] * i;
if (t > maxp) break;
pmask[t] = plist[j];
if (i % plist[j] == 0) {
break;
}
}
}
}
static long long pv[1024];
int pc[1024];
static long long factors[655360];
static int factor_cnt;
void get_factors(int limit, long long value) {
factors[factor_cnt++] = value;
for (int i = 0; i < limit; ++i) {
long long tvalue = value;
const int p = pv[i];
for (int j = 1; j <= pc[i]; ++j) {
tvalue *= p;
get_factors(i, tvalue);
}
}
}
int factorize(long long n) {
int top = 0;
if (n <= maxp) {
while (n != 1) {
int now = pmask[n];
int c = 0;
while (n % now == 0) n /= now, ++c;
if (c) pv[top] = now, pc[top++] = c;
}
} else {
for (int i = 0; i < pcnt; ++i) {
const long long p = plist[i];
const long long test = p * p;
if (p > n) break;
int c = 0;
while (n % p == 0) n /= p, ++c;
if (c) pv[top] = p, pc[top++] = c;
}
if (n != 1) pv[top] = n, pc[top++] = 1;
}
return top;
}
static inline long long add(long long a, long long b) {
a += b;
if (a >= mod) a -= mod;
return a;
}
static inline long long sub(long long a, long long b) {
a -= b;
if (a < 0) a += mod;
return a;
}
long long dp[655360];
vector<int> adj[1005];
struct Query {
int n, f;
int id, ans;
};
int cmp_n(const Query& a, const Query& b) {
if (a.n != b.n) return a.n < b.n;
return a.f < b.f;
}
int cmp_id(const Query& a, const Query& b) { return a.id < b.id; }
Query data[100005];
int main() {
md.init_fac();
init_primes();
int q = Rint();
for (int i = (0); i < (q); ++i)
data[i].n = Rint(), data[i].f = Rint(), data[i].id = i;
sort(data, data + q, cmp_n);
for (int x = 0; x < q; ++x) {
const int n = data[x].n, f = data[x].f;
if (n == 1) {
data[x].ans = f == 1 ? 1 : 0;
continue;
}
if (f > n) {
data[x].ans = 0;
continue;
}
if (f == 1) {
data[x].ans = 0;
continue;
}
if (f == n) {
data[x].ans = 1;
continue;
}
factor_cnt = 0;
get_factors(factorize(n), 1);
sort(factors, factors + factor_cnt);
for (int i = 0; i < factor_cnt; ++i) adj[i].clear();
int t = 0;
for (int i = 0; i < factor_cnt; ++i)
for (int j = i + 1; j < factor_cnt; ++j)
if (factors[j] % factors[i] == 0) adj[i].push_back(j), ++t;
int j = x;
while (j < q && data[j].n == data[x].n) {
const int f = data[j].f;
if (j > x && f == data[j - 1].f) {
data[j].ans = data[j - 1].ans;
++j;
continue;
}
for (int i = 0; i < factor_cnt; ++i) dp[i] = 0;
for (int i = 0; i < factor_cnt; ++i) {
const long long val = factors[i];
if (val < f) {
dp[i] = 0;
continue;
}
const long long t = md.comb(f + val - f - 1, val - f);
dp[i] = add(dp[i], t);
for (int j = 0; j < adj[i].size(); ++j)
dp[adj[i][j]] = sub(dp[adj[i][j]], dp[i]);
}
data[j].ans = dp[factor_cnt - 1];
++j;
}
x = j - 1;
}
sort(data, data + q, cmp_id);
for (int i = 0; i < q; ++i) printf("%d\n", data[i].ans);
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int mod = 1000000007;
int fac[100001], inv[100001], k, f[100001];
vector<int> e[100001];
bool v[100001];
int cnt, p[20001], mu[100001];
inline int ksm(long long base, int expo) {
long long ret = 1;
for (; expo; (base *= base) %= mod, expo >>= 1)
if (expo & 1) (ret *= base) %= mod;
return ret;
}
inline int C(int n, int m) {
return n < m ? 0 : (int)((long long)fac[n] * inv[m] % mod * inv[n - m] % mod);
}
int main() {
inv[0] = fac[0] = 1;
for (int i = 1; i <= 100000; i++)
e[i].push_back(1), fac[i] = (long long)fac[i - 1] * i % mod;
inv[100000] = ksm(fac[100000], mod - 2);
for (int i = 99999; i >= 0; i--)
inv[i] = (long long)inv[i + 1] * (i + 1) % mod;
mu[1] = 1;
for (int i = 2; i <= 100000; i++) {
if (!v[i]) p[++cnt] = i, mu[i] = -1;
if (mu[i]) {
for (int j = i; j <= 100000; j += i) e[j].push_back(i);
}
for (int j = 1; j <= cnt && i * p[j] <= 100000; j++) {
v[i * p[j]] = 1;
if (i % p[j] == 0) break;
mu[i * p[j]] = -mu[i];
}
}
int q;
scanf("%d", &q);
while (q--) {
int n;
scanf("%d%d", &n, &k);
memset(f, 0, sizeof f);
int ans = 0;
for (int i : e[n]) (ans += C(n / i - 1, k - 1) * mu[i]) %= mod;
if (ans < 0) ans += mod;
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int mod = 1000000007;
int fac[100001], inv[100001], k, f[100001];
vector<int> e[100001];
bool v[100001];
int cnt, p[20001], mu[100001];
inline int ksm(long long base, int expo) {
long long ret = 1;
for (; expo; (base *= base) %= mod, expo >>= 1)
if (expo & 1) (ret *= base) %= mod;
return ret;
}
inline int C(int n, int m) {
return n < m ? 0 : (int)((long long)fac[n] * inv[m] % mod * inv[n - m] % mod);
}
int main() {
inv[0] = fac[0] = 1;
for (int i = 1; i <= 100000; i++)
e[i].push_back(1), fac[i] = (long long)fac[i - 1] * i % mod;
inv[100000] = ksm(fac[100000], mod - 2);
for (int i = 99999; i >= 0; i--)
inv[i] = (long long)inv[i + 1] * (i + 1) % mod;
mu[1] = 1;
for (int i = 2; i <= 100000; i++) {
if (!v[i]) p[++cnt] = i, mu[i] = -1;
if (mu[i]) {
for (int j = i; j <= 100000; j += i) e[j].push_back(i);
}
for (int j = 1; j <= cnt && i * p[j] <= 100000; j++) {
v[i * p[j]] = 1;
if (i % p[j] == 0) break;
mu[i * p[j]] = -mu[i];
}
}
int q;
scanf("%d", &q);
while (q--) {
int n;
scanf("%d%d", &n, &k);
memset(f, 0, sizeof f);
int ans = 0;
for (int i : e[n]) (ans += C(n / i - 1, k - 1) * mu[i]) %= mod;
if (ans < 0) ans += mod;
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int setBit(int n, int pos) { return n = n | (1 << pos); }
int resetBit(int n, int pos) { return n = n & ~(1 << pos); }
bool checkBit(long long n, long long pos) { return (bool)(n & (1LL << pos)); }
const int MAX = 100010;
int mu[MAX], inv[MAX];
vector<int> divs[MAX];
const int MOD = 1000000007;
void Mobius(int N) {
int i, j;
mu[1] = 1;
for (i = 1; i <= N; i++) {
if (mu[i]) {
for (j = i + i; j <= N; j += i) {
mu[j] -= mu[i];
divs[j].push_back(i);
}
}
}
}
pair<long long, long long> extEuclid(long long a, long long b) {
if (b == 0LL) return make_pair(1LL, 0LL);
pair<long long, long long> ret, got;
got = extEuclid(b, a % b);
ret = make_pair(got.second, got.first - (a / b) * got.second);
return ret;
}
long long modularInverse(long long a, long long m) {
long long x, y, inv;
pair<long long, long long> sol = extEuclid(a, m);
inv = (sol.first + m) % m;
return inv;
}
long long fact[MAX];
void pre() {
Mobius(1e5);
fact[0] = 1;
for (int i = 1; i <= 1e5; i++) {
fact[i] = (fact[i - 1] * i) % MOD;
}
for (int i = 0; i <= 100000; i++) inv[i] = modularInverse(fact[i], MOD);
}
int nCr(int n, int r) {
if (n < r) return 0;
long long ret = fact[n];
ret *= inv[r];
ret %= MOD;
ret *= inv[n - r];
ret %= MOD;
return (int)ret;
}
int Solve(int n, int f) {
if (n == f)
return 1;
else if (f == 1 & n > 1)
return 0;
long long ans = 0;
for (int i = 0, x; i < divs[n].size(); i++) {
x = divs[n][i];
ans += mu[x] * nCr((n / x) - 1, f - 1);
if (ans > MOD)
ans -= MOD;
else if (ans <= -MOD)
ans += MOD;
}
return (ans + MOD) % MOD;
}
int main() {
pre();
int Q, n, f;
scanf("%d", &Q);
for (int q = 1; q <= Q; q++) {
scanf("%d %d", &n, &f);
printf("%d\n", Solve(n, f));
}
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int setBit(int n, int pos) { return n = n | (1 << pos); }
int resetBit(int n, int pos) { return n = n & ~(1 << pos); }
bool checkBit(long long n, long long pos) { return (bool)(n & (1LL << pos)); }
const int MAX = 100010;
int mu[MAX], inv[MAX];
vector<int> divs[MAX];
const int MOD = 1000000007;
void Mobius(int N) {
int i, j;
mu[1] = 1;
for (i = 1; i <= N; i++) {
if (mu[i]) {
for (j = i + i; j <= N; j += i) {
mu[j] -= mu[i];
divs[j].push_back(i);
}
}
}
}
pair<long long, long long> extEuclid(long long a, long long b) {
if (b == 0LL) return make_pair(1LL, 0LL);
pair<long long, long long> ret, got;
got = extEuclid(b, a % b);
ret = make_pair(got.second, got.first - (a / b) * got.second);
return ret;
}
long long modularInverse(long long a, long long m) {
long long x, y, inv;
pair<long long, long long> sol = extEuclid(a, m);
inv = (sol.first + m) % m;
return inv;
}
long long fact[MAX];
void pre() {
Mobius(1e5);
fact[0] = 1;
for (int i = 1; i <= 1e5; i++) {
fact[i] = (fact[i - 1] * i) % MOD;
}
for (int i = 0; i <= 100000; i++) inv[i] = modularInverse(fact[i], MOD);
}
int nCr(int n, int r) {
if (n < r) return 0;
long long ret = fact[n];
ret *= inv[r];
ret %= MOD;
ret *= inv[n - r];
ret %= MOD;
return (int)ret;
}
int Solve(int n, int f) {
if (n == f)
return 1;
else if (f == 1 & n > 1)
return 0;
long long ans = 0;
for (int i = 0, x; i < divs[n].size(); i++) {
x = divs[n][i];
ans += mu[x] * nCr((n / x) - 1, f - 1);
if (ans > MOD)
ans -= MOD;
else if (ans <= -MOD)
ans += MOD;
}
return (ans + MOD) % MOD;
}
int main() {
pre();
int Q, n, f;
scanf("%d", &Q);
for (int q = 1; q <= Q; q++) {
scanf("%d %d", &n, &f);
printf("%d\n", Solve(n, f));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long sq(long long x) { return (x * x) % MOD; }
long long modP(long long a, long long b) {
return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}
long long fact[maxN], inv[maxN];
int mob[maxN];
vector<int> D[maxN];
long long choose(long long n, long long k) {
if (k < 0 || k > n) return 0;
return (((fact[n] * inv[k]) % MOD) * inv[n - k]) % MOD;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
fact[0] = 1;
for (long long i = 1; i < maxN; i++) {
fact[i] = (fact[i - 1] * i) % MOD;
}
inv[maxN - 1] = modP(fact[maxN - 1], MOD - 2);
for (long long i = maxN - 2; i > -1; i--) {
inv[i] = (inv[i + 1] * (i + 1)) % MOD;
}
for (int i = 1; i < maxN; i++) {
if (i == 1) mob[i] = 1;
D[i].push_back(i);
for (int j = 2; j * i < maxN; j++) {
mob[j * i] -= mob[i];
D[j * i].push_back(i);
}
}
int q;
cin >> q;
while (q--) {
int n, f;
cin >> n >> f;
long long ans = 0;
for (auto d : D[n]) {
ans = (ans + mob[d] * choose(n / d - 1, f - 1)) % MOD;
}
while (ans < 0) ans += MOD;
cout << ans << endl;
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long sq(long long x) { return (x * x) % MOD; }
long long modP(long long a, long long b) {
return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}
long long fact[maxN], inv[maxN];
int mob[maxN];
vector<int> D[maxN];
long long choose(long long n, long long k) {
if (k < 0 || k > n) return 0;
return (((fact[n] * inv[k]) % MOD) * inv[n - k]) % MOD;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
fact[0] = 1;
for (long long i = 1; i < maxN; i++) {
fact[i] = (fact[i - 1] * i) % MOD;
}
inv[maxN - 1] = modP(fact[maxN - 1], MOD - 2);
for (long long i = maxN - 2; i > -1; i--) {
inv[i] = (inv[i + 1] * (i + 1)) % MOD;
}
for (int i = 1; i < maxN; i++) {
if (i == 1) mob[i] = 1;
D[i].push_back(i);
for (int j = 2; j * i < maxN; j++) {
mob[j * i] -= mob[i];
D[j * i].push_back(i);
}
}
int q;
cin >> q;
while (q--) {
int n, f;
cin >> n >> f;
long long ans = 0;
for (auto d : D[n]) {
ans = (ans + mob[d] * choose(n / d - 1, f - 1)) % MOD;
}
while (ans < 0) ans += MOD;
cout << ans << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100007;
const int maxv = 100000;
long long fac[maxn], fac2[maxn];
map<pair<int, int>, int> save;
int cnt;
long long mypow(int base, int n) {
if (n == 0) return 1;
long long t = mypow(base, n / 2);
t = (t * t) % 1000000007;
if (n & 1)
return (t * base) % 1000000007;
else
return t;
}
long long C(int n, int k) {
long long res = fac[n] * fac2[k] % 1000000007;
res = res * fac2[n - k] % 1000000007;
return res;
}
long long solve(int f, int n) {
if (f == 1) return (n == 1);
if (f == n) return 1;
if (f > n) return 0;
if (save.count(pair<int, int>(f, n))) return save[pair<int, int>(f, n)];
long long res = 0;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) {
int x = i;
res = (res + solve(f, n / x)) % 1000000007;
if (i * i == n) continue;
x = n / i;
res = (res + solve(f, n / x)) % 1000000007;
}
save[pair<int, int>(f, n)] =
(C(n - 1, f - 1) - res + 1000000007) % 1000000007;
return save[pair<int, int>(f, n)];
}
int main() {
fac[0] = 1;
for (int i = 1, _c = maxv; i <= _c; i++) fac[i] = fac[i - 1] * i % 1000000007;
for (int v = 0, _c = maxv; v <= _c; v++)
fac2[v] = mypow(fac[v], 1000000007 - 2);
int q, f, n;
scanf("%d", &q);
for (int i = 0, _a = (q); i < _a; ++i) {
scanf("%d%d", &n, &f);
printf("%I64d\n", solve(f, n));
}
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100007;
const int maxv = 100000;
long long fac[maxn], fac2[maxn];
map<pair<int, int>, int> save;
int cnt;
long long mypow(int base, int n) {
if (n == 0) return 1;
long long t = mypow(base, n / 2);
t = (t * t) % 1000000007;
if (n & 1)
return (t * base) % 1000000007;
else
return t;
}
long long C(int n, int k) {
long long res = fac[n] * fac2[k] % 1000000007;
res = res * fac2[n - k] % 1000000007;
return res;
}
long long solve(int f, int n) {
if (f == 1) return (n == 1);
if (f == n) return 1;
if (f > n) return 0;
if (save.count(pair<int, int>(f, n))) return save[pair<int, int>(f, n)];
long long res = 0;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) {
int x = i;
res = (res + solve(f, n / x)) % 1000000007;
if (i * i == n) continue;
x = n / i;
res = (res + solve(f, n / x)) % 1000000007;
}
save[pair<int, int>(f, n)] =
(C(n - 1, f - 1) - res + 1000000007) % 1000000007;
return save[pair<int, int>(f, n)];
}
int main() {
fac[0] = 1;
for (int i = 1, _c = maxv; i <= _c; i++) fac[i] = fac[i - 1] * i % 1000000007;
for (int v = 0, _c = maxv; v <= _c; v++)
fac2[v] = mypow(fac[v], 1000000007 - 2);
int q, f, n;
scanf("%d", &q);
for (int i = 0, _a = (q); i < _a; ++i) {
scanf("%d%d", &n, &f);
printf("%I64d\n", solve(f, n));
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
long long int fact[maxn * 2 + 1];
long long int ifact[maxn * 2 + 1];
int n, f;
long long int fastex(long long int a, long long int b) {
long long int ret = 1;
while (b) {
if (b & 1) {
ret = (ret * a) % 1000000007;
}
a = (a * a) % 1000000007;
b = b / 2;
}
return ret;
}
long long int ways(int n1, int k) {
return (fact[n1 + k - 1] * ((ifact[n1 - 1] * ifact[k]) % 1000000007)) %
1000000007;
}
vector<long long int> primes;
long long int inex() {
long long int ret = 0;
for (int i = 1; i < 1 << primes.size(); i++) {
if (__builtin_popcount(i) % 2) {
long long int fact = 1;
for (int j = 0; j < primes.size(); j++) {
if (i & (1 << j)) {
fact *= primes[j];
}
}
if ((n / fact - f) >= 0) ret = (ret + ways(f, n / fact - f)) % 1000000007;
} else {
long long int fact = 1;
for (int j = 0; j < primes.size(); j++) {
if (i & (1 << j)) {
fact *= primes[j];
}
}
if ((n / fact - f) >= 0)
ret = (ret + 1000000007 - ways(f, n / fact - f)) % 1000000007;
}
}
return ret % 1000000007;
}
int main() {
int cases;
cin >> cases;
fact[0] = 1;
ifact[0] = fastex(1, 1000000007 - 2);
for (int i = 1; i <= maxn * 2; i++) {
fact[i] = (fact[i - 1] * i) % 1000000007;
ifact[i] = fastex(fact[i], 1000000007 - 2) % 1000000007;
}
while (cases--) {
cin >> n >> f;
int m = n;
for (int i = 2; i <= m / i; i++) {
if (m % i == 0) {
while (m % i == 0) m /= i;
primes.push_back(i);
}
}
if (m != 1) primes.push_back(m);
cout << (ways(f, n - f) + 1000000007 - inex()) % 1000000007 << endl;
primes.clear();
}
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
long long int fact[maxn * 2 + 1];
long long int ifact[maxn * 2 + 1];
int n, f;
long long int fastex(long long int a, long long int b) {
long long int ret = 1;
while (b) {
if (b & 1) {
ret = (ret * a) % 1000000007;
}
a = (a * a) % 1000000007;
b = b / 2;
}
return ret;
}
long long int ways(int n1, int k) {
return (fact[n1 + k - 1] * ((ifact[n1 - 1] * ifact[k]) % 1000000007)) %
1000000007;
}
vector<long long int> primes;
long long int inex() {
long long int ret = 0;
for (int i = 1; i < 1 << primes.size(); i++) {
if (__builtin_popcount(i) % 2) {
long long int fact = 1;
for (int j = 0; j < primes.size(); j++) {
if (i & (1 << j)) {
fact *= primes[j];
}
}
if ((n / fact - f) >= 0) ret = (ret + ways(f, n / fact - f)) % 1000000007;
} else {
long long int fact = 1;
for (int j = 0; j < primes.size(); j++) {
if (i & (1 << j)) {
fact *= primes[j];
}
}
if ((n / fact - f) >= 0)
ret = (ret + 1000000007 - ways(f, n / fact - f)) % 1000000007;
}
}
return ret % 1000000007;
}
int main() {
int cases;
cin >> cases;
fact[0] = 1;
ifact[0] = fastex(1, 1000000007 - 2);
for (int i = 1; i <= maxn * 2; i++) {
fact[i] = (fact[i - 1] * i) % 1000000007;
ifact[i] = fastex(fact[i], 1000000007 - 2) % 1000000007;
}
while (cases--) {
cin >> n >> f;
int m = n;
for (int i = 2; i <= m / i; i++) {
if (m % i == 0) {
while (m % i == 0) m /= i;
primes.push_back(i);
}
}
if (m != 1) primes.push_back(m);
cout << (ways(f, n - f) + 1000000007 - inex()) % 1000000007 << endl;
primes.clear();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1, M = 1e9 + 7;
int n, m, q, dp[N], vis[N], vs, fact[N], ifact[N];
vector<int> d[N];
int Pow(int a, int b) {
int res = 1;
while (b != 0) {
if (b & 1) res = ((long long)res * a) % M;
a = ((long long)a * a) % M;
b >>= 1;
}
return res;
}
inline int nCk(int n, int k) {
if (k > n) return 0;
return (((long long)fact[n] * ifact[k]) % M * ifact[n - k]) % M;
}
int calc(int at) {
int &res = dp[at];
if (vis[at] == vs) return res;
vis[at] = vs;
res = nCk(at - 1, m - 1);
for (int i = 1; i < (int)d[at].size(); ++i) {
res -= calc(at / d[at][i]);
if (res < 0) res += M;
}
return res;
}
int main() {
fact[0] = ifact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = ((long long)fact[i - 1] * i) % M;
ifact[i] = Pow(fact[i], M - 2);
for (int j = i; j < N; j += i) d[j].push_back(i);
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &m);
++vs;
printf("%d\n", calc(n));
}
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1, M = 1e9 + 7;
int n, m, q, dp[N], vis[N], vs, fact[N], ifact[N];
vector<int> d[N];
int Pow(int a, int b) {
int res = 1;
while (b != 0) {
if (b & 1) res = ((long long)res * a) % M;
a = ((long long)a * a) % M;
b >>= 1;
}
return res;
}
inline int nCk(int n, int k) {
if (k > n) return 0;
return (((long long)fact[n] * ifact[k]) % M * ifact[n - k]) % M;
}
int calc(int at) {
int &res = dp[at];
if (vis[at] == vs) return res;
vis[at] = vs;
res = nCk(at - 1, m - 1);
for (int i = 1; i < (int)d[at].size(); ++i) {
res -= calc(at / d[at][i]);
if (res < 0) res += M;
}
return res;
}
int main() {
fact[0] = ifact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = ((long long)fact[i - 1] * i) % M;
ifact[i] = Pow(fact[i], M - 2);
for (int j = i; j < N; j += i) d[j].push_back(i);
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &m);
++vs;
printf("%d\n", calc(n));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1001000;
const int MOD = 1e9 + 7;
int cnt;
int a[maxn], Count[maxn];
long long Pow[maxn], INV[maxn];
bool check[maxn];
int prime[maxn], mu[maxn], tot;
void Moblus() {
memset(check, false, sizeof(check));
mu[1] = 1, tot = 0;
for (int i = 2; i < maxn; i++) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
check[i * prime[j]] = true;
if (i % prime[j])
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
}
void solve(int n) {
int num = sqrt(n + 1);
for (int i = 1; i <= num; i++) {
if (n % i == 0) {
a[cnt++] = i;
if (i * i != n) a[cnt++] = n / i;
}
}
}
long long inv(long long a, long long m) {
if (a == 1) return 1;
return inv(m % a, m) * (m - m / a) % m;
}
int main() {
Moblus();
int _, n, f;
Pow[0] = 1;
for (int i = 1; i < maxn; i++) Pow[i] = Pow[i - 1] * i % MOD;
for (int i = 0; i < maxn; i++) INV[i] = inv(Pow[i], MOD);
scanf("%d", &_);
while (_--) {
scanf("%d%d", &n, &f);
cnt = 0;
solve(n);
sort(a, a + cnt);
long long ans = 0;
for (int i = 0; i < cnt; i++) {
if (n / a[i] < f) break;
Count[a[i]] =
Pow[n / a[i] - 1] * INV[n / a[i] - f] % MOD * INV[f - 1] % MOD;
ans = (ans + 1LL * Count[a[i]] * mu[a[i]]) % MOD;
}
printf("%lld\n", (ans + MOD) % MOD);
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1001000;
const int MOD = 1e9 + 7;
int cnt;
int a[maxn], Count[maxn];
long long Pow[maxn], INV[maxn];
bool check[maxn];
int prime[maxn], mu[maxn], tot;
void Moblus() {
memset(check, false, sizeof(check));
mu[1] = 1, tot = 0;
for (int i = 2; i < maxn; i++) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
check[i * prime[j]] = true;
if (i % prime[j])
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
}
void solve(int n) {
int num = sqrt(n + 1);
for (int i = 1; i <= num; i++) {
if (n % i == 0) {
a[cnt++] = i;
if (i * i != n) a[cnt++] = n / i;
}
}
}
long long inv(long long a, long long m) {
if (a == 1) return 1;
return inv(m % a, m) * (m - m / a) % m;
}
int main() {
Moblus();
int _, n, f;
Pow[0] = 1;
for (int i = 1; i < maxn; i++) Pow[i] = Pow[i - 1] * i % MOD;
for (int i = 0; i < maxn; i++) INV[i] = inv(Pow[i], MOD);
scanf("%d", &_);
while (_--) {
scanf("%d%d", &n, &f);
cnt = 0;
solve(n);
sort(a, a + cnt);
long long ans = 0;
for (int i = 0; i < cnt; i++) {
if (n / a[i] < f) break;
Count[a[i]] =
Pow[n / a[i] - 1] * INV[n / a[i] - f] % MOD * INV[f - 1] % MOD;
ans = (ans + 1LL * Count[a[i]] * mu[a[i]]) % MOD;
}
printf("%lld\n", (ans + MOD) % MOD);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long ff[100100], inv_ff[100100];
int q, n, f, prime[100100], sqr[100100], mu[100100], ans;
vector<int> divi[100100];
long long inv_mod(long long x) {
long long ret = 1LL, y = MOD - 2;
while (y) {
if (y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD, y >>= 1;
}
return ret;
}
long long C(int x, int y) {
if (y > x) return 0;
return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD;
}
void precompute() {
inv_ff[0] = 1LL;
ff[0] = 1LL;
for (int i = 1; i <= 100099; i++) {
ff[i] = (ff[i - 1] * i) % MOD;
inv_ff[i] = inv_mod(ff[i]);
prime[i] = 1;
}
for (int i = 1; i <= 100099; i++) {
for (int j = i; j <= 100099; j += i) {
divi[j].push_back(i);
}
}
prime[0] = 0, prime[1] = 0;
for (int i = 2; i <= 100099; i++) {
if (prime[i] == 1) {
for (int j = i + i; j <= 100099; j += i) prime[j] = 0;
}
}
for (int i = 2; i * i <= 100099; i++) {
for (int j = (i * i); j <= 100099; j += (i * i)) sqr[j] = 1;
}
for (int i = 1; i <= 100099; i++) {
if (sqr[i] == 1) continue;
int cnt = 0;
for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++)
if (prime[*d] == 1) cnt++;
mu[i] = (cnt % 2 == 1) ? -1 : 1;
}
return;
}
int main() {
precompute();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
if (n == 1 and f == 1) {
printf("1\n");
continue;
}
if (n > 1 and f == 1) {
printf("0\n");
continue;
}
ans = 0;
for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end();
d++) {
ans = (ans + mu[*d] * C((n / (*d)) - 1, f - 1)) % MOD;
if (ans < 0)
ans += MOD;
else if (ans >= MOD)
ans -= MOD;
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long ff[100100], inv_ff[100100];
int q, n, f, prime[100100], sqr[100100], mu[100100], ans;
vector<int> divi[100100];
long long inv_mod(long long x) {
long long ret = 1LL, y = MOD - 2;
while (y) {
if (y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD, y >>= 1;
}
return ret;
}
long long C(int x, int y) {
if (y > x) return 0;
return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD;
}
void precompute() {
inv_ff[0] = 1LL;
ff[0] = 1LL;
for (int i = 1; i <= 100099; i++) {
ff[i] = (ff[i - 1] * i) % MOD;
inv_ff[i] = inv_mod(ff[i]);
prime[i] = 1;
}
for (int i = 1; i <= 100099; i++) {
for (int j = i; j <= 100099; j += i) {
divi[j].push_back(i);
}
}
prime[0] = 0, prime[1] = 0;
for (int i = 2; i <= 100099; i++) {
if (prime[i] == 1) {
for (int j = i + i; j <= 100099; j += i) prime[j] = 0;
}
}
for (int i = 2; i * i <= 100099; i++) {
for (int j = (i * i); j <= 100099; j += (i * i)) sqr[j] = 1;
}
for (int i = 1; i <= 100099; i++) {
if (sqr[i] == 1) continue;
int cnt = 0;
for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++)
if (prime[*d] == 1) cnt++;
mu[i] = (cnt % 2 == 1) ? -1 : 1;
}
return;
}
int main() {
precompute();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &n, &f);
if (n == 1 and f == 1) {
printf("1\n");
continue;
}
if (n > 1 and f == 1) {
printf("0\n");
continue;
}
ans = 0;
for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end();
d++) {
ans = (ans + mu[*d] * C((n / (*d)) - 1, f - 1)) % MOD;
if (ans < 0)
ans += MOD;
else if (ans >= MOD)
ans -= MOD;
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100500;
const int mod = 1000000007;
long long binpow(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = (ret * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ret;
}
long long f[N], inv[N];
long long get(int n, int k) {
if (n < k) return 0;
long long ret = f[n];
ret = (ret * inv[n - k]) % mod;
ret = (ret * inv[k]) % mod;
return ret;
}
int lp[N], pr[N], sz = 0, M[N];
int t, n, k;
long long ans = 0;
void calc(int d) {
ans += (get(n / d - 1, k - 1) * 1ll * M[d]) % mod;
if (ans > mod) ans -= mod;
if (ans < 0) ans += mod;
}
int main() {
f[0] = 1;
inv[0] = 1;
for (int i = 1; i <= 100000; ++i) {
f[i] = (f[i - 1] * i) % mod;
inv[i] = binpow(f[i], mod - 2);
}
lp[0] = 1;
lp[1] = 1;
for (int i = 2; i <= 100000; ++i) {
if (!lp[i]) {
lp[i] = i;
pr[++sz] = i;
}
for (int j = 1; j <= sz && pr[j] <= lp[i]; ++j) {
if (pr[j] * 1ll * i > 100000) break;
lp[pr[j] * i] = pr[j];
}
}
M[1] = 1;
for (int i = 2; i <= 100000; ++i) {
int tt = 0;
int x = i;
int last = 0;
bool ok = 1;
while (x > 1) {
if (last == lp[x]) {
ok = 0;
break;
}
last = lp[x];
x /= lp[x];
++tt;
}
if (!ok) {
M[i] = 0;
} else {
if (tt & 1)
M[i] = -1;
else
M[i] = 1;
}
}
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &k);
ans = 0;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
calc(i);
if (n / i != i) calc(n / i);
}
}
printf("%I64d\n", (ans % mod + mod) % mod);
}
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100500;
const int mod = 1000000007;
long long binpow(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = (ret * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ret;
}
long long f[N], inv[N];
long long get(int n, int k) {
if (n < k) return 0;
long long ret = f[n];
ret = (ret * inv[n - k]) % mod;
ret = (ret * inv[k]) % mod;
return ret;
}
int lp[N], pr[N], sz = 0, M[N];
int t, n, k;
long long ans = 0;
void calc(int d) {
ans += (get(n / d - 1, k - 1) * 1ll * M[d]) % mod;
if (ans > mod) ans -= mod;
if (ans < 0) ans += mod;
}
int main() {
f[0] = 1;
inv[0] = 1;
for (int i = 1; i <= 100000; ++i) {
f[i] = (f[i - 1] * i) % mod;
inv[i] = binpow(f[i], mod - 2);
}
lp[0] = 1;
lp[1] = 1;
for (int i = 2; i <= 100000; ++i) {
if (!lp[i]) {
lp[i] = i;
pr[++sz] = i;
}
for (int j = 1; j <= sz && pr[j] <= lp[i]; ++j) {
if (pr[j] * 1ll * i > 100000) break;
lp[pr[j] * i] = pr[j];
}
}
M[1] = 1;
for (int i = 2; i <= 100000; ++i) {
int tt = 0;
int x = i;
int last = 0;
bool ok = 1;
while (x > 1) {
if (last == lp[x]) {
ok = 0;
break;
}
last = lp[x];
x /= lp[x];
++tt;
}
if (!ok) {
M[i] = 0;
} else {
if (tt & 1)
M[i] = -1;
else
M[i] = 1;
}
}
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &k);
ans = 0;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
calc(i);
if (n / i != i) calc(n / i);
}
}
printf("%I64d\n", (ans % mod + mod) % mod);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 101000;
const int MOD = 1e9 + 7;
int cnt;
int a[maxn], Count[maxn];
long long Pow[maxn], INV[maxn];
bool check[maxn];
int prime[maxn], mu[maxn], tot;
void Moblus() {
memset(check, false, sizeof(check));
mu[1] = 1, tot = 0;
for (int i = 2; i < maxn; i++) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
check[i * prime[j]] = true;
if (i % prime[j])
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
}
void solve(int n) {
int num = sqrt(n + 1);
for (int i = 1; i <= num; i++) {
if (n % i == 0) {
a[cnt++] = i;
if (i * i != n) a[cnt++] = n / i;
}
}
}
long long inv(long long a, long long m) {
if (a == 1) return 1;
return inv(m % a, m) * (m - m / a) % m;
}
int main() {
Moblus();
int _, n, f;
Pow[0] = 1;
for (int i = 1; i < maxn; i++) Pow[i] = Pow[i - 1] * i % MOD;
for (int i = 0; i < maxn; i++) INV[i] = inv(Pow[i], MOD);
scanf("%d", &_);
while (_--) {
scanf("%d%d", &n, &f);
cnt = 0;
solve(n);
sort(a, a + cnt);
long long ans = 0;
for (int i = 0; i < cnt; i++) {
if (n / a[i] < f) break;
Count[a[i]] =
Pow[n / a[i] - 1] * INV[n / a[i] - f] % MOD * INV[f - 1] % MOD;
ans = (ans + 1LL * Count[a[i]] * mu[a[i]]) % MOD;
}
printf("%lld\n", (ans + MOD) % MOD);
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 101000;
const int MOD = 1e9 + 7;
int cnt;
int a[maxn], Count[maxn];
long long Pow[maxn], INV[maxn];
bool check[maxn];
int prime[maxn], mu[maxn], tot;
void Moblus() {
memset(check, false, sizeof(check));
mu[1] = 1, tot = 0;
for (int i = 2; i < maxn; i++) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
check[i * prime[j]] = true;
if (i % prime[j])
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
}
void solve(int n) {
int num = sqrt(n + 1);
for (int i = 1; i <= num; i++) {
if (n % i == 0) {
a[cnt++] = i;
if (i * i != n) a[cnt++] = n / i;
}
}
}
long long inv(long long a, long long m) {
if (a == 1) return 1;
return inv(m % a, m) * (m - m / a) % m;
}
int main() {
Moblus();
int _, n, f;
Pow[0] = 1;
for (int i = 1; i < maxn; i++) Pow[i] = Pow[i - 1] * i % MOD;
for (int i = 0; i < maxn; i++) INV[i] = inv(Pow[i], MOD);
scanf("%d", &_);
while (_--) {
scanf("%d%d", &n, &f);
cnt = 0;
solve(n);
sort(a, a + cnt);
long long ans = 0;
for (int i = 0; i < cnt; i++) {
if (n / a[i] < f) break;
Count[a[i]] =
Pow[n / a[i] - 1] * INV[n / a[i] - f] % MOD * INV[f - 1] % MOD;
ans = (ans + 1LL * Count[a[i]] * mu[a[i]]) % MOD;
}
printf("%lld\n", (ans + MOD) % MOD);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 256;
const char nxtl = '\n';
const int mod = (int)1e9 + 7;
const double eps = (double)1e-9;
template <typename T>
inline bool updmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool updmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
int f[MAXN], inv[MAXN];
inline int binpow(int x, int y) {
int res = 1;
for (; y > 0; y >>= 1) {
if (y & 1) res = (res * 1ll * x) % mod;
x = (x * 1ll * x) % mod;
}
return res;
}
inline int cnk(int x, int y) {
return (f[x] * 1ll * inv[y]) % mod * 1ll * inv[x - y] % mod;
}
inline void prec() {
f[0] = 1;
inv[0] = 1;
for (int i = 1; i <= (int)1e5; ++i) {
f[i] = f[i - 1] * 1ll * i % mod;
inv[i] = binpow(f[i], mod - 2);
}
}
map<pair<int, int>, int> dp;
int solve(int n, int k) {
if (n < k) return 0;
if (k == 1) return (n == 1);
if (dp.count(make_pair(n, k))) return dp[make_pair(n, k)];
int res = cnk(n - 1, k - 1);
for (int i = 2; i * i <= n; ++i) {
if (n % i > 0) continue;
res -= solve(n / i, k);
if (res < 0) res += mod;
if (i * i != n) res -= solve(n / (n / i), k);
if (res < 0) res += mod;
}
return dp[make_pair(n, k)] = res;
}
int main() {
prec();
int q;
scanf("%d", &q);
while (q-- > 0) {
int n, k;
scanf("%d%d", &n, &k);
printf("%d\n", solve(n, k));
}
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 256;
const char nxtl = '\n';
const int mod = (int)1e9 + 7;
const double eps = (double)1e-9;
template <typename T>
inline bool updmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool updmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
int f[MAXN], inv[MAXN];
inline int binpow(int x, int y) {
int res = 1;
for (; y > 0; y >>= 1) {
if (y & 1) res = (res * 1ll * x) % mod;
x = (x * 1ll * x) % mod;
}
return res;
}
inline int cnk(int x, int y) {
return (f[x] * 1ll * inv[y]) % mod * 1ll * inv[x - y] % mod;
}
inline void prec() {
f[0] = 1;
inv[0] = 1;
for (int i = 1; i <= (int)1e5; ++i) {
f[i] = f[i - 1] * 1ll * i % mod;
inv[i] = binpow(f[i], mod - 2);
}
}
map<pair<int, int>, int> dp;
int solve(int n, int k) {
if (n < k) return 0;
if (k == 1) return (n == 1);
if (dp.count(make_pair(n, k))) return dp[make_pair(n, k)];
int res = cnk(n - 1, k - 1);
for (int i = 2; i * i <= n; ++i) {
if (n % i > 0) continue;
res -= solve(n / i, k);
if (res < 0) res += mod;
if (i * i != n) res -= solve(n / (n / i), k);
if (res < 0) res += mod;
}
return dp[make_pair(n, k)] = res;
}
int main() {
prec();
int q;
scanf("%d", &q);
while (q-- > 0) {
int n, k;
scanf("%d%d", &n, &k);
printf("%d\n", solve(n, k));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 10;
const int mod = (int)1e9 + 7;
int add(int a, int b) { return (a + b) % mod; }
int mult(long long int a, long long int b) { return (a * b) % mod; }
int bin_pow(long long int a, long long int b) {
int ans = 1;
while (b) {
if (b % 2 == 0) {
a = mult(a, a);
b /= 2;
} else {
ans = mult(ans, a);
b--;
}
}
return ans;
}
int inv(int n) { return bin_pow(n, mod - 2); }
vector<int> primes[N];
int fact[N], ifact[N];
int n, f;
int choose(int n, int k) { return mult(fact[n], mult(ifact[k], ifact[n - k])); }
int go(int n) {
if (n < f) return 0;
int stars = n, bars = f - 1;
stars -= f;
int tot = choose(stars + bars, bars);
return tot;
}
void solve() {
cin >> n >> f;
vector<int>& cur = primes[n];
int ans = 0;
for (int mask = 0; mask < (1 << cur.size()); ++mask) {
int gcd = 1;
int bits = 0;
for (int i = 0; i < cur.size(); ++i) {
if ((mask & (1 << i)) > 0) {
++bits;
gcd *= cur[i];
}
}
int ways = go(n / gcd);
int sign = bits % 2 == 1 ? -1 : 1;
ans += sign * ways;
ans %= mod;
ans += mod;
ans %= mod;
}
cout << ans << endl;
}
void precomp() {
vector<bool> isprime(N, true);
isprime[0] = isprime[1] = false;
for (int i = 2; i < N; ++i) {
if (!isprime[i]) continue;
primes[i].push_back(i);
for (int j = i + i; j < N; j += i) {
isprime[j] = false;
primes[j].push_back(i);
}
}
fact[0] = ifact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = mult(fact[i - 1], i);
ifact[i] = inv(fact[i]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(20);
cout << fixed;
precomp();
int q;
cin >> q;
while (q--) solve();
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 10;
const int mod = (int)1e9 + 7;
int add(int a, int b) { return (a + b) % mod; }
int mult(long long int a, long long int b) { return (a * b) % mod; }
int bin_pow(long long int a, long long int b) {
int ans = 1;
while (b) {
if (b % 2 == 0) {
a = mult(a, a);
b /= 2;
} else {
ans = mult(ans, a);
b--;
}
}
return ans;
}
int inv(int n) { return bin_pow(n, mod - 2); }
vector<int> primes[N];
int fact[N], ifact[N];
int n, f;
int choose(int n, int k) { return mult(fact[n], mult(ifact[k], ifact[n - k])); }
int go(int n) {
if (n < f) return 0;
int stars = n, bars = f - 1;
stars -= f;
int tot = choose(stars + bars, bars);
return tot;
}
void solve() {
cin >> n >> f;
vector<int>& cur = primes[n];
int ans = 0;
for (int mask = 0; mask < (1 << cur.size()); ++mask) {
int gcd = 1;
int bits = 0;
for (int i = 0; i < cur.size(); ++i) {
if ((mask & (1 << i)) > 0) {
++bits;
gcd *= cur[i];
}
}
int ways = go(n / gcd);
int sign = bits % 2 == 1 ? -1 : 1;
ans += sign * ways;
ans %= mod;
ans += mod;
ans %= mod;
}
cout << ans << endl;
}
void precomp() {
vector<bool> isprime(N, true);
isprime[0] = isprime[1] = false;
for (int i = 2; i < N; ++i) {
if (!isprime[i]) continue;
primes[i].push_back(i);
for (int j = i + i; j < N; j += i) {
isprime[j] = false;
primes[j].push_back(i);
}
}
fact[0] = ifact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = mult(fact[i - 1], i);
ifact[i] = inv(fact[i]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(20);
cout << fixed;
precomp();
int q;
cin >> q;
while (q--) solve();
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:20000000")
using namespace std;
double __begin;
template <typename T1, typename T2, typename T3>
struct triple {
T1 a;
T2 b;
T3 c;
triple(){};
triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <typename T1, typename T2, typename T3>
bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) {
if (t1.a != t2.a)
return t1.a < t2.a;
else
return t1.b < t2.b;
}
template <typename T1, typename T2, typename T3>
inline std::ostream &operator<<(std::ostream &os, const triple<T1, T2, T3> &t) {
return os << "(" << t.a << ", " << t.b << ", " << t.c << ")";
}
inline int bits_count(int v) {
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline int bits_count(long long v) {
int t = v >> 32;
int p = (v & ((1LL << 32) - 1));
return bits_count(t) + bits_count(p);
}
unsigned int reverse_bits(register unsigned int x) {
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16));
}
inline int sign(int x) { return (x >> 31) | (-x >> 31); }
inline bool ispow2(int x) { return (x != 0 && (x & (x - 1)) == 0); }
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
bool first = true;
os << "[";
for (unsigned int i = 0; i < v.size(); i++) {
if (!first) os << ", ";
os << v[i];
first = false;
}
return os << "]";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {
bool first = true;
os << "[";
for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end();
++ii) {
if (!first) os << ", ";
os << *ii;
first = false;
}
return os << "]";
}
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {
bool first = true;
os << "[";
for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();
++ii) {
if (!first) os << ", ";
os << *ii;
first = false;
}
return os << "]";
}
template <typename T, typename T2>
void printarray(T a[], T2 sz, T2 beg = 0) {
for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
cout << endl;
}
inline long long binpow(long long x, long long n) {
long long res = 1;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
inline long long powmod(long long x, long long n, long long _mod) {
long long res = 1;
while (n) {
if (n & 1) res = (res * x) % _mod;
x = (x * x) % _mod;
n >>= 1;
}
return res;
}
inline long long gcd(long long a, long long b) {
long long t;
while (b) {
a = a % b;
t = a;
a = b;
b = t;
}
return a;
}
inline int gcd(int a, int b) {
int t;
while (b) {
a = a % b;
t = a;
a = b;
b = t;
}
return a;
}
inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
inline long long lcm(long long a, long long b, long long c) {
return lcm(lcm(a, b), c);
}
inline long long lcm(int a, int b, int c) {
return lcm(lcm(a, b), (long long)c);
}
inline long long max(long long a, long long b) { return (a > b) ? a : b; }
inline int max(int a, int b) { return (a > b) ? a : b; }
inline double max(double a, double b) { return (a > b) ? a : b; }
inline long long max(long long a, long long b, long long c) {
return max(a, max(b, c));
}
inline int max(int a, int b, int c) { return max(a, max(b, c)); }
inline double max(double a, double b, double c) { return max(a, max(b, c)); }
inline long long min(long long a, long long b) { return (a < b) ? a : b; }
inline int min(int a, int b) { return (a < b) ? a : b; }
inline double min(double a, double b) { return (a < b) ? a : b; }
inline long long min(long long a, long long b, long long c) {
return min(a, min(b, c));
}
inline int min(int a, int b, int c) { return min(a, min(b, c)); }
inline double min(double a, double b, double c) { return min(a, min(b, c)); }
template <class T>
inline void getar(T a, int n, int m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; ++j) {
scanf("%d", &a[i][j]);
}
}
inline void getar(int *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%d", a + ii);
}
}
inline void getar(pair<int, int> *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%d%d", &a[ii].first, &a[ii].second);
}
}
inline void getar(long long *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%I64d", a + ii);
}
}
int fact[100010];
int revf[100010];
bool e[100010];
int C(int n, int m) {
if (n < m) return 0;
return (((fact[n] * 1ll * revf[m]) % 1000000007) * revf[n - m]) % 1000000007;
}
vector<int> divs[100010];
map<pair<int, int>, int> m;
int prime[100010];
vector<int> primes;
void prime_sieve(int n) {
int t = (n * 1.2) / log(1.0 * n);
primes.reserve(n < 5000 ? 1000 : t);
for (int i = 2; i <= n; ++i) {
if (prime[i] == 0) {
primes.push_back(i);
prime[i] = i;
}
for (int j = 0;
j < primes.size() && i * primes[j] <= n && primes[j] <= prime[i];
++j) {
prime[i * primes[j]] = primes[j];
}
}
}
int foo(int n, int f) { return (fact[n - 1] * 1ll * revf[n - f]) % 1000000007; }
int powmod(long long x) {
int n = 1000000007 - 2;
long long res = 1;
while (n) {
if (n & 1) {
res = (res * x) % 1000000007;
}
x = (x * x) % 1000000007;
n >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
prime_sieve(100000);
fact[0] = 1;
revf[0] = 1;
for (int i = 1; i <= 100000; ++i) {
fact[i] = (fact[i - 1] * 1ll * i) % 1000000007;
revf[i] = powmod(fact[i]);
}
for (int i = 2; i <= 100000; ++i) {
e[i] = !e[i / prime[i]];
if (prime[i] != i) {
int p = prime[i];
int t = i / p;
if (p == prime[t]) {
divs[i] = divs[t];
} else {
divs[i].reserve(divs[t].size() * 2 + 1);
divs[i].push_back(p);
for (int d : divs[t]) {
divs[i].push_back(d);
divs[i].push_back(d * p);
}
}
} else {
divs[i].push_back(i);
}
}
int q;
cin >> q;
while (q--) {
int f, n;
cin >> n >> f;
if (f == 1) {
cout << (n == 1) << '\n';
continue;
}
int ans = foo(n, f);
for (int p : divs[n]) {
if (p == n) continue;
int q = n / p;
if (q < f) continue;
int t = foo(q, f);
if (e[p]) {
ans -= t;
if (ans < 0) ans += 1000000007;
} else {
ans += t;
if (ans >= 1000000007) ans -= 1000000007;
}
}
ans = (ans * 1ll * revf[f - 1]) % 1000000007;
cout << ans << '\n';
}
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
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.
Examples
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.
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:20000000")
using namespace std;
double __begin;
template <typename T1, typename T2, typename T3>
struct triple {
T1 a;
T2 b;
T3 c;
triple(){};
triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <typename T1, typename T2, typename T3>
bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) {
if (t1.a != t2.a)
return t1.a < t2.a;
else
return t1.b < t2.b;
}
template <typename T1, typename T2, typename T3>
inline std::ostream &operator<<(std::ostream &os, const triple<T1, T2, T3> &t) {
return os << "(" << t.a << ", " << t.b << ", " << t.c << ")";
}
inline int bits_count(int v) {
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline int bits_count(long long v) {
int t = v >> 32;
int p = (v & ((1LL << 32) - 1));
return bits_count(t) + bits_count(p);
}
unsigned int reverse_bits(register unsigned int x) {
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16));
}
inline int sign(int x) { return (x >> 31) | (-x >> 31); }
inline bool ispow2(int x) { return (x != 0 && (x & (x - 1)) == 0); }
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
bool first = true;
os << "[";
for (unsigned int i = 0; i < v.size(); i++) {
if (!first) os << ", ";
os << v[i];
first = false;
}
return os << "]";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {
bool first = true;
os << "[";
for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end();
++ii) {
if (!first) os << ", ";
os << *ii;
first = false;
}
return os << "]";
}
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {
bool first = true;
os << "[";
for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();
++ii) {
if (!first) os << ", ";
os << *ii;
first = false;
}
return os << "]";
}
template <typename T, typename T2>
void printarray(T a[], T2 sz, T2 beg = 0) {
for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
cout << endl;
}
inline long long binpow(long long x, long long n) {
long long res = 1;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
inline long long powmod(long long x, long long n, long long _mod) {
long long res = 1;
while (n) {
if (n & 1) res = (res * x) % _mod;
x = (x * x) % _mod;
n >>= 1;
}
return res;
}
inline long long gcd(long long a, long long b) {
long long t;
while (b) {
a = a % b;
t = a;
a = b;
b = t;
}
return a;
}
inline int gcd(int a, int b) {
int t;
while (b) {
a = a % b;
t = a;
a = b;
b = t;
}
return a;
}
inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
inline long long lcm(long long a, long long b, long long c) {
return lcm(lcm(a, b), c);
}
inline long long lcm(int a, int b, int c) {
return lcm(lcm(a, b), (long long)c);
}
inline long long max(long long a, long long b) { return (a > b) ? a : b; }
inline int max(int a, int b) { return (a > b) ? a : b; }
inline double max(double a, double b) { return (a > b) ? a : b; }
inline long long max(long long a, long long b, long long c) {
return max(a, max(b, c));
}
inline int max(int a, int b, int c) { return max(a, max(b, c)); }
inline double max(double a, double b, double c) { return max(a, max(b, c)); }
inline long long min(long long a, long long b) { return (a < b) ? a : b; }
inline int min(int a, int b) { return (a < b) ? a : b; }
inline double min(double a, double b) { return (a < b) ? a : b; }
inline long long min(long long a, long long b, long long c) {
return min(a, min(b, c));
}
inline int min(int a, int b, int c) { return min(a, min(b, c)); }
inline double min(double a, double b, double c) { return min(a, min(b, c)); }
template <class T>
inline void getar(T a, int n, int m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; ++j) {
scanf("%d", &a[i][j]);
}
}
inline void getar(int *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%d", a + ii);
}
}
inline void getar(pair<int, int> *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%d%d", &a[ii].first, &a[ii].second);
}
}
inline void getar(long long *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%I64d", a + ii);
}
}
int fact[100010];
int revf[100010];
bool e[100010];
int C(int n, int m) {
if (n < m) return 0;
return (((fact[n] * 1ll * revf[m]) % 1000000007) * revf[n - m]) % 1000000007;
}
vector<int> divs[100010];
map<pair<int, int>, int> m;
int prime[100010];
vector<int> primes;
void prime_sieve(int n) {
int t = (n * 1.2) / log(1.0 * n);
primes.reserve(n < 5000 ? 1000 : t);
for (int i = 2; i <= n; ++i) {
if (prime[i] == 0) {
primes.push_back(i);
prime[i] = i;
}
for (int j = 0;
j < primes.size() && i * primes[j] <= n && primes[j] <= prime[i];
++j) {
prime[i * primes[j]] = primes[j];
}
}
}
int foo(int n, int f) { return (fact[n - 1] * 1ll * revf[n - f]) % 1000000007; }
int powmod(long long x) {
int n = 1000000007 - 2;
long long res = 1;
while (n) {
if (n & 1) {
res = (res * x) % 1000000007;
}
x = (x * x) % 1000000007;
n >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
prime_sieve(100000);
fact[0] = 1;
revf[0] = 1;
for (int i = 1; i <= 100000; ++i) {
fact[i] = (fact[i - 1] * 1ll * i) % 1000000007;
revf[i] = powmod(fact[i]);
}
for (int i = 2; i <= 100000; ++i) {
e[i] = !e[i / prime[i]];
if (prime[i] != i) {
int p = prime[i];
int t = i / p;
if (p == prime[t]) {
divs[i] = divs[t];
} else {
divs[i].reserve(divs[t].size() * 2 + 1);
divs[i].push_back(p);
for (int d : divs[t]) {
divs[i].push_back(d);
divs[i].push_back(d * p);
}
}
} else {
divs[i].push_back(i);
}
}
int q;
cin >> q;
while (q--) {
int f, n;
cin >> n >> f;
if (f == 1) {
cout << (n == 1) << '\n';
continue;
}
int ans = foo(n, f);
for (int p : divs[n]) {
if (p == n) continue;
int q = n / p;
if (q < f) continue;
int t = foo(q, f);
if (e[p]) {
ans -= t;
if (ans < 0) ans += 1000000007;
} else {
ans += t;
if (ans >= 1000000007) ans -= 1000000007;
}
}
ans = (ans * 1ll * revf[f - 1]) % 1000000007;
cout << ans << '\n';
}
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.